Author |
Message |
zigbert

Joined: 29 Mar 2006 Posts: 6629 Location: Valåmoen, Norway
|
Posted: Thu 07 Jun 2012, 18:10 Post subject:
Bash question [Solved] Subject description: I don't want terminal output |
|
This small example shows how I often make tests in my scripts Code: | #!/bin/bash
echo true > /tmp/test1
[ "$(</tmp/test2)" = "true" ] && echo true
| As you see, the file test2 won't hold 'true', and bash gives the annoying terminal output: Code: | line 3: /tmp/test2: No such file or directory | Sure it's true there is no file, but I don't won't to see the message.
For variable content it is enough to use qoutes to skip message. Code: | [ "$TEST" = "true" ] && echo true | For content of files I can use cat and send errors to /dev/null Code: | [ "`cat /tmp/test2 2> /dev/null`" = "true" ] && echo true | But in the end I would like to use bash.... and can't find the correct syntax.
I know some of you know it
Thank you
Sigmund
_________________ Stardust resources
Last edited by zigbert on Sat 09 Jun 2012, 01:05; edited 1 time in total
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 5089 Location: Arizona, U.S.A.
|
Posted: Thu 07 Jun 2012, 18:34 Post subject:
|
|
Hi zigbert; I assume that you want just this error removed?
" No such file or directory "
I was going to suggest grep, but I understand what you`re looking for.
Lit me see what I can find... Terry
|
Back to top
|
|
 |
Karl Godt

Joined: 20 Jun 2010 Posts: 4208 Location: Kiel,Germany
|
Posted: Thu 07 Jun 2012, 19:15 Post subject:
|
|
Code: | [ -f /tmp/test1 ] && { [ "$(</tmp/test2)" = "true" ] && echo true; }
|
OR
Code: | exec 2>/dev/null;[ "$(</tmp/test2)" = "true" ] && echo true;exec 2>/dev/stderr |
would come into mind
Note : exec 2>/dev/null works in script but not in rxvt as in /etc/rc.d/rc.shutdown
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 5089 Location: Arizona, U.S.A.
|
Posted: Thu 07 Jun 2012, 23:37 Post subject:
|
|
I tried all sorts of Bashisms, but they all failed to quell the error output.
Here`s the best idea I came up with:
Code: | sh-4.1# [ -e /tmp/test1 ]&& echo $(</tmp/test1)
true
sh-4.1# [ -e /tmp/test2 ]&& echo $(</tmp/test2)
sh-4.1# |
Last edited by sunburnt on Fri 08 Jun 2012, 15:42; edited 1 time in total
|
Back to top
|
|
 |
Bruce B
Joined: 18 May 2005 Posts: 11488 Location: The Peoples Republic of California
|
Posted: Fri 08 Jun 2012, 02:05 Post subject:
|
|
I think this is what I used to supress terminal output
command &>/dev/null
~
_________________ New! Puppy Linux Links Page
|
Back to top
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 2647
|
Posted: Fri 08 Jun 2012, 02:18 Post subject:
|
|
'1> /dev/null' quietens normal output. '2> /dev/null' quietens error output. '&> /dev/null' quietens them both.
|
Back to top
|
|
 |
jamesbond
Joined: 26 Feb 2007 Posts: 3475 Location: The Blue Marble
|
Posted: Fri 08 Jun 2012, 08:25 Post subject:
Re: Bash question Subject description: I don't want terminal output |
|
This seems to do the trick
Code: | #!/bin/bash
echo true > /tmp/test1
exec 2> /dev/null
[ "$(</tmp/test2)" = "true" ] && echo true
|
But note that you will lose all errors after the exec line (ie all errors goes to /dev/null) after that.
_________________ Fatdog64 forum links: Latest version | Contributed packages | ISO builder
|
Back to top
|
|
 |
jamesbond
Joined: 26 Feb 2007 Posts: 3475 Location: The Blue Marble
|
Posted: Fri 08 Jun 2012, 08:33 Post subject:
|
|
Surprisingly, in bash at least, in UTF-8 locale at least (that's my test environment), testing for presence for file first before reading it is faster than blindly read the file and suppress the error.
This code: Code: | #!/bin/bash
func1() {
2> /dev/null read p < /etc/xxx
}
func2() {
[ -e /etc/xxx ] && read p < /etc/xxx
}
p=5
echo -n func1
time for a in $(seq 1 10000); do func1; done
echo
echo -n func2
time for a in $(seq 1 10000); do func2; done
echo
echo p is $p
exec 2> /dev/null
[ "$(</etc/passwd)" ] && echo passwd there
[ "$(</etc/passwdxxx)" ] && echo passwdxxx there
|
gives this surprising results:
Code: | func1
real 0m0.331s
user 0m0.270s
sys 0m0.057s
func2
real 0m0.155s
user 0m0.137s
sys 0m0.017s
p is 5
passwd there
|
I have always been under the impression that the "test" commands are slow and to be avoided (use "case" instead), but apparently it is not so slow after all ... benchmark is king isn't it
_________________ Fatdog64 forum links: Latest version | Contributed packages | ISO builder
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 5089 Location: Arizona, U.S.A.
|
Posted: Fri 08 Jun 2012, 15:47 Post subject:
|
|
jamesbond; Yep, it`s surprising what you find, like Bash takes 0ms.
Like tests on copying an SFS file`s contents showed it`s faster than
copying the loose files on the partition. That`s just sooo weird...
Try it some time!
|
Back to top
|
|
 |
zigbert

Joined: 29 Mar 2006 Posts: 6629 Location: Valåmoen, Norway
|
Posted: Sat 09 Jun 2012, 01:04 Post subject:
|
|
Thank you for all feedback
I didn't realize that I could chain several tests. This works Code: | #!/bin/bash
echo true > /tmp/test1
[ -e /tmp/test2 ] && [ "$(</tmp/test2)" = "true" ] && echo true |
Sigmund
_________________ Stardust resources
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 5089 Location: Arizona, U.S.A.
|
Posted: Sun 10 Jun 2012, 00:09 Post subject:
|
|
.
Glad to see the community coming together for a good cause...
|
Back to top
|
|
 |
|