LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-15-2006, 11:34 AM   #1
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Rep: Reputation: 15
Test output from bash script


Hi experts!

I need to execute a binary program in a bash script and evaluate the program output before mounting or not a partition.

How can I test a program output within an "if - fi" instruction?

Thanks in advance,

Daniel.
 
Old 11-15-2006, 12:29 PM   #2
Nathanael
Member
 
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940

Rep: Reputation: 32
it would help to know what command you are wanting to run and kind of what the output looks like and what you are looking for in the output - as there might be a few different ways of doing this.
 
Old 11-15-2006, 02:35 PM   #3
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Code:
if COMMAND; then
    mount
else
    echo error
fi
or you can just execute the program and then check the value of $?. If that might get overwritten, you can assign it to a variable that won't get clobbered and check that. Or do a 'COMMAND || fail' or 'COMMAND && success'. Etc. The bash man page is very good.
 
Old 11-16-2006, 01:38 AM   #4
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Original Poster
Rep: Reputation: 15
Thanks for your replies.

I want to execute a custom command that will output "1" or "0" depending on a pushbutton state.

Daniel.
 
Old 11-16-2006, 01:59 PM   #5
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
'Output' 0/1 literally, like to stdout, or it will return 0/1? If return, the conditional I gave earlier should do. If to stdout, then snag it in a variable:
Code:
var=$(command)
if (( $var == 0 )); then
    mount
else
    echo error
fi
 
Old 11-16-2006, 04:55 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,239

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
Incidentally (in bash), numeric comparisons use 'lettered' operators ie is_equal is '-eq' and vice versa ie string comparisons use '==' etc.
 
Old 11-16-2006, 09:01 PM   #7
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Not always. == is also an equality operator under the rules for arithmetic evaluation, which apply to the (( command and some other cases.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script 'test' question linmix Programming 5 11-29-2005 11:49 AM
Bash Script test for empty dir uopjohnson Linux - Software 3 10-07-2005 06:45 PM
Simple webserver test script (bash) somedudeppf Programming 3 01-14-2004 10:30 AM
bash script test file operators... bulliver Programming 3 10-17-2003 12:06 PM
benchmark test bash script PTBmilo Programming 3 02-14-2003 04:37 PM


All times are GMT -5. The time now is 04:28 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration