Bash question [Solved]

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

Bash question [Solved]

#1 Post by zigbert »

This small example shows how I often make tests in my scripts

Code: Select all

#!/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: Select all

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: Select all

[ "$TEST" = "true" ] && echo true
For content of files I can use cat and send errors to /dev/null

Code: Select all

[ "`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 :wink:


Thank you
Sigmund
Last edited by zigbert on Sat 09 Jun 2012, 05:05, edited 1 time in total.

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#2 Post by sunburnt »

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

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#3 Post by Karl Godt »

Code: Select all

[ -f /tmp/test1 ] && { [ "$(</tmp/test2)" = "true" ] && echo true; } 
OR

Code: Select all

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

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#4 Post by sunburnt »

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: Select all

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, 19:42, edited 1 time in total.

Bruce B

#5 Post by Bruce B »

I think this is what I used to supress terminal output


command &>/dev/null


~

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#6 Post by amigo »

'1> /dev/null' quietens normal output. '2> /dev/null' quietens error output. '&> /dev/null' quietens them both.

jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

Re: Bash question

#7 Post by jamesbond »

This seems to do the trick

Code: Select all

#!/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: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]

jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

#8 Post by jamesbond »

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: Select all

#!/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: Select all

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 :oops:
Fatdog64 forum links: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#9 Post by sunburnt »

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!

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#10 Post by zigbert »

Thank you for all feedback
I didn't realize that I could chain several tests. This works

Code: Select all

#!/bin/bash
echo true > /tmp/test1
[ -e /tmp/test2 ] && [ "$(</tmp/test2)" = "true" ] && echo true
Sigmund

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#11 Post by sunburnt »

.
Glad to see the community coming together for a good cause...

Post Reply