Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

AND, OR with IF and GREP - statements in shell scripting

 
SOLVED
Go to solution
Highlighted
Honored Contributor

AND, OR with IF and GREP - statements in shell scripting

Hi,
Please suggest about:
1. AND, OR operations inside an IF condition
2. AND, OR operations inside GREP

AND and OR operations give confusions and issues all the times(which braket"(" or "[[" or, ,,etc,...). can you please write how to use them with simple examples.

ISSUE: i have this output in a file(output-file). i would like to run an opcmsg if either rds, crs, mmd or kms is down.

OUTPUT-FILE:
**************
oper@corvette:/home/oper=>omnisv -status
ProcName Status [PID]
===============================
rds : Down
crs : Active [9569]
mmd : Active [9566]
kms : Active [9567]
omnitrig: Active
uiproxy : Active [9571]
===============================
Status: At least one of Data Protector relevant processes/services is not running.
**************

i wrote like this:
RDS=`grep -c rds output-file | grep -c Down`
CRS=`grep -c crs output-file | grep -c Down`
MMD=`grep -c mmd output-file | grep -c Down`
KMS=`grep -c kms output-file | grep -c Down`

if[[ $RDS -eq 1 || $CRS -eq 1 || $MMD -eq 1 || $KMS -eq 1 ]]
then
echo "At least one of Data Protector relevant processes/services is not running."
fi
11 REPLIES 11
Highlighted
Honored Contributor

Re: AND, OR with IF and GREP - statements in shell scripting

This might sound simplistic but you could just grep your file for "Down" and mail yourself an alert:

grep -i down
Just a thought,
Rita

Highlighted
Honored Contributor

Re: AND, OR with IF and GREP - statements in shell scripting

typo...

grep -i down
should read

grep -i down


Boy I hate giving up coffee....
/rcw
Highlighted
Honored Contributor
Solution

Re: AND, OR with IF and GREP - statements in shell scripting

Be a simplest comparation:

if [ "${RDS}${CRD}${MMD}${KMS}" != "0000" ]
then
echo "At least one of Data Protector relevant processes/services is not running."
fi

Rgds.
Highlighted
Honored Contributor

Re: AND, OR with IF and GREP - statements in shell scripting

Rita,
ya, presently i am doing the same thing - i am grep'ing for 4 "Active" and if its less, i am running few other commands.
issue is that the other two processes uiproxy and omnitrig need not be monitored.. i want to monitor only 4 processes, that is the issue.

Also please advice how to use AND and OR operators with IF and GREP.
Highlighted
Honored Contributor

Re: AND, OR with IF and GREP - statements in shell scripting

Sekar,

You question was:
AND and OR operations give confusions and issues all the times(which braket"(" or "[[" or, ,,etc,...). can you please write how to use them with simple examples.

..take a look at Jose wrote it and you have a good answer of your request for a simple example..

Rgrds,
Rita
Highlighted
Honored Contributor

Re: AND, OR with IF and GREP - statements in shell scripting

Jose, that worked !!!

the script now
RDS=`grep rds testfile | grep -c Down`
CRS=`grep crs testfile | grep -c Down`
MMD=`grep mmd testfile | grep -c Down`
KMS=`grep kms testfile | grep -c Down`

if [ "${RDS}${CRS}${MMD}${KMS}" != "0000" ] i
then
echo "At least one process is not running"
fi

between, still you can reply about -
How to use to AND and OR, with GREP and IF ?
Highlighted
Trusted Contributor

Re: AND, OR with IF and GREP - statements in shell scripting

Hi,

you have a mistake in your script.

"grep -c" return a count of matching lines.
You should replace:
RDS=`grep -c rds output-file | grep -c Down`
CRS=`grep -c crs output-file | grep -c Down`
MMD=`grep -c mmd output-file | grep -c Down`
KMS=`grep -c kms output-file | grep -c Down`
by (remove 1st "-c"):
RDS=`grep rds output-file | grep -c Down`
CRS=`grep crs output-file | grep -c Down`
MMD=`grep mmd output-file | grep -c Down`
KMS=`grep kms output-file | grep -c Down`

My suggestion:
if grep -qE '^(rds|crs|mmd|kms) : Down' output-file
then
echo ...
fi

Regards,
Mounaam
Highlighted
Honored Contributor

Re: AND, OR with IF and GREP - statements in shell scripting

Another simple way...

RDS=`cat output-file|grep -q ^"rds : Down"|echo $?`
CRS=`cat output-file|grep -q ^"crs : Down"|echo $?`
MMD=`cat output-file|grep -q ^"mmd : Down"|echo $?`
KMS=`cat output-file|grep -q ^"kms : Down"|echo $?`

if [ "${RDS}${CRD}${MMD}${KMS}" != "0000" ]
then
echo "At least one of Data Protector relevant processes/services is not running."
fi
Highlighted
Acclaimed Contributor

Re: AND, OR with IF and GREP - statements in shell scripting

Hi:

For 'and' and 'or' operators within an 'if' condition, you should read the section of the 'sh-posix(1) manpages entitled "conditional expressions" as well as the manpages for 'test(1)'.

The 'grep' command can perform regular expression alternation (an 'or') like this (using your example:

# grep -E "rds : Down|crs : Down|mmd : Down|mks : Down" filename && echo "at least one down" || echo "all ok"

...or with :

grep -e "rds : Down" -e "crs : Down" -e "mmd : Down" -e "mks : Down" filename && echo "at least one down" || echo "all ok"

Regards!

...JRF...