Operating System - HP-UX
1833323 Members
3013 Online
110051 Solutions
New Discussion

Shell script condition statement

 
SOLVED
Go to solution
Damian Gardner
Advisor

Shell script condition statement

Hello. Simple question I hope. I want to test my backup_rc for 0 or 4 after a backup. Currently my script tests for 0 like so:
"if [ $BACKUP_RC -eq 0 ] then"
How do I incorporate an OR into it, syntax-wise?

Thanks much,
Damian
7 REPLIES 7
Ermin Borovac
Honored Contributor
Solution

Re: Shell script condition statement

if [ $BACKUP_RC -eq 0 -o $BACKUP_RC -eq 4 ]; then
...
Eknath
Trusted Contributor

Re: Shell script condition statement

HI Damian,

you can use logical operators say e.g
if [ $BACKUP_RC -eq 0 || $BACKUP -eq 1 ] then ...

try this

Cheers !!!
eknath
Muthukumar_5
Honored Contributor

Re: Shell script condition statement

You can do it as,


if [[ ${BACKUP_RC} -eq 0 || ${BACKUP_RC} -eq 4 ]]
then
statments;
fi

hth.
Easy to suggest when don't know about the problem!
Cem Tugrul
Esteemed Contributor

Re: Shell script condition statement

Operators (increasing precedence)
= += -= *= /= %= ^= assignment
?: conditional expression
|| logical or
& & logical and
~ !~ regular expression match, negated match
< < = > > = != == relationals
blank string concatenation
+ - add, subtract
* / % multiply, divide, modulus
+ - ! unary plus, unary minus, logical negation
^ exponentional
++ -- increment, decrement
$ field
Our greatest duty in this life is to help others. And please, if you can't
john korterman
Honored Contributor

Re: Shell script condition statement

Hi,
if you only expect 0 or 4 you already have good answers. However, it is good practice to double quote variables in order to avoid syntax errors in case a variable is not expanded.
If you expect a wider diversity of return values, you could use a case statement, e.g.:

case "$BACKUP_RC" in
0) echo the returncode was 0;;
1) echo the returncode was 1;;
4) echo the returncode was 4;;
*) echo the returncode was completely unexpected;;
esac

regards,
John K.
it would be nice if you always got a second chance
renarios
Trusted Contributor

Re: Shell script condition statement

Hi Damian,

Another possibility is the shell built-in "case". That is faster (because it's a shell built-in) and more readeble.

The syntax would look like:
case ${BACKUP_RC} in
0)
echo "BACKUP_RC is 0"
;;
1)
echo "BACKUP_RC is 1"
;;
*
echo "BACKUP_RC is ${BACKUP_RC}"
;;
esac
Hope that helps!

Cheers,

Renarios
Nothing is more successfull as failure
Basheer_2
Trusted Contributor

Re: Shell script condition statement

if [ $backup eq 1 ] || [ $backup eq 2 ] || [ $backup eq 3 ] || [ $backup eq 4] ; then
echo " do something "
fi