LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Test output from bash script (http://www.linuxquestions.org/questions/programming-9/test-output-from-bash-script-501876/)

estratos 11-15-2006 11:34 AM

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.

Nathanael 11-15-2006 12:29 PM

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.

slakmagik 11-15-2006 02:35 PM

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.

estratos 11-16-2006 01:38 AM

Thanks for your replies.

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

Daniel.

slakmagik 11-16-2006 01:59 PM

'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


chrism01 11-16-2006 04:55 PM

Incidentally (in bash), numeric comparisons use 'lettered' operators ie is_equal is '-eq' and vice versa ie string comparisons use '==' etc.

slakmagik 11-16-2006 09:01 PM

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


All times are GMT -5. The time now is 10:40 AM.