Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Thu 28 Aug 2014, 09:44
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Bash question [Solved]
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 1 Posts_count  
Author Message
zigbert


Joined: 29 Mar 2006
Posts: 5684
Location: Valåmoen, Norway

PostPosted: Thu 07 Jun 2012, 18:10    Post_subject:  Bash question [Solved]
Sub_title: 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 Wink


Thank you
Sigmund

_________________
Stardust resources

Edited_time_total
Back to top
View user's profile Send_private_message Visit_website 
sunburnt


Joined: 08 Jun 2005
Posts: 5016
Location: Arizona, U.S.A.

PostPosted: 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
View user's profile Send_private_message 
Karl Godt


Joined: 20 Jun 2010
Posts: 3964
Location: Kiel,Germany

PostPosted: 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
View user's profile Send_private_message Visit_website 
sunburnt


Joined: 08 Jun 2005
Posts: 5016
Location: Arizona, U.S.A.

PostPosted: 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#

Edited_time_total
Back to top
View user's profile Send_private_message 
Bruce B


Joined: 18 May 2005
Posts: 11089
Location: The Peoples Republic of California

PostPosted: 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
View user's profile Send_private_message 
amigo

Joined: 02 Apr 2007
Posts: 2236

PostPosted: 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
View user's profile Send_private_message 
jamesbond

Joined: 26 Feb 2007
Posts: 2120
Location: The Blue Marble

PostPosted: Fri 08 Jun 2012, 08:25    Post_subject: Re: Bash question
Sub_title: 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, Slacko and Puppeee user. Puppy user since 2.13.
Contributed Fatdog64 packages thread
Back to top
View user's profile Send_private_message 
jamesbond

Joined: 26 Feb 2007
Posts: 2120
Location: The Blue Marble

PostPosted: 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 Embarassed

_________________
Fatdog64, Slacko and Puppeee user. Puppy user since 2.13.
Contributed Fatdog64 packages thread
Back to top
View user's profile Send_private_message 
sunburnt


Joined: 08 Jun 2005
Posts: 5016
Location: Arizona, U.S.A.

PostPosted: 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
View user's profile Send_private_message 
zigbert


Joined: 29 Mar 2006
Posts: 5684
Location: Valåmoen, Norway

PostPosted: 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
View user's profile Send_private_message Visit_website 
sunburnt


Joined: 08 Jun 2005
Posts: 5016
Location: Arizona, U.S.A.

PostPosted: Sun 10 Jun 2012, 00:09    Post_subject:  

.
Glad to see the community coming together for a good cause...
Back to top
View user's profile Send_private_message 
Display_posts:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0686s ][ Queries: 12 (0.0032s) ][ GZIP on ]