two questions: and's inside if's and checking the return code of a program

two questions: and's inside if's and checking the return code of a program

Post by tucker.jer » Thu, 29 Dec 2005 02:35:19


Hello all:

I have two questions concerning sh shell programming:
1. is there a way to test a conditional containing and's and or's (such
as if ( cond1 && cond2 ) ) other than using two if statements?

2. is there a way to check the return code of a c program (i.e.I have a
c program that returns ints based on whether or not it fails. I want
to take action based on these codes)

Thanks,
Jeremy
 
 
 

two questions: and's inside if's and checking the return code of a program

Post by Kevin Coll » Thu, 29 Dec 2005 04:20:47

In article < XXXX@XXXXX.COM >,


With a real Bourne shell, I don't believe you can combine conditionals. With
most modern shells, like ksh, bash, etc, you can. For example, with ksh:

if [[ $cond1 = 1 && ($cond2 = 2 || $cond3 > 2) ]]
...


Again, shell dependant, but I believe all the sh-derived shells use $? to store
the return code of the previous command.

Kevin

--
Unix Guy Consulting, LLC
Unix and Linux Automation, Shell, Perl and CGI scripting
http://www.yqcomputer.com/

 
 
 

two questions: and's inside if's and checking the return code of a program

Post by Dale Haggl » Thu, 29 Dec 2005 05:30:26


>> 1. is there a way to test a conditional containing and's and or's (such
>> as if ( cond1 && cond2 ) ) other than using two if statements?

Kevin> With a real Bourne shell, I don't believe you can combine
Kevin> conditionals.

Actually, you can.

if [ $cond1 -eq 1 -a \( $cond2 -eq 2 -o $cond3 -gt 2 \) ]
then
echo condition is true
fi

Note that there's no magic shell syntax here whatsoever in a classic
bourne shell: "[" is looked up like any other command, and it happens
to be a synonym for test(1). Modern bourne-type shells, of course,
treat "[" as a builtin, intead of actually exec'ing an external
command.

Dale.
 
 
 

two questions: and's inside if's and checking the return code of a program

Post by Bill Marcu » Thu, 29 Dec 2005 06:46:56

On 27 Dec 2005 09:35:19 -0800, XXXX@XXXXX.COM

$?
or
if your_program
or
your_program &&
"if" and "&&" can only distinguish between zero and non-zero return
codes. "case $?" is more flexible.


--
Happiness is having a scratch for every itch.
-- Ogden Nash
 
 
 

two questions: and's inside if's and checking the return code of a program

Post by Bill Marcu » Thu, 29 Dec 2005 06:48:28

On Tue, 27 Dec 2005 19:20:47 GMT, Kevin Collins

csh and tcsh use $status.

--
It seems a little silly now, but this country was founded as a protest
against taxation.
 
 
 

two questions: and's inside if's and checking the return code of a program

Post by tucker.jer » Thu, 29 Dec 2005 06:54:08

Thanks for the responses. Exactly what I was looking for.

Jeremy
 
 
 

two questions: and's inside if's and checking the return code of a program

Post by Sven Masch » Thu, 29 Dec 2005 07:15:16


If you see the need to emphasize this, let me correct it:
Only the first release on Version 7 had no test built-in, perhaps
for reasons of space. And nowadays, all relevant variants also come
with an echo built-in. This shell can be notably slower for other
reasons, like buffering issues, though. Built-ins (in some modern
shells) like expr might only get relevant upon excessive use.