- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: AND, OR with IF and GREP - statements in shell...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-13-2011 03:59 AM
тАО01-13-2011 03:59 AM
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
Solved! Go to Solution.
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-13-2011 04:08 AM
тАО01-13-2011 04:08 AM
Re: AND, OR with IF and GREP - statements in shell scripting
grep -i down
Just a thought,
Rita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-13-2011 04:09 AM
тАО01-13-2011 04:09 AM
Re: AND, OR with IF and GREP - statements in shell scripting
grep -i down
should read
grep -i down
Boy I hate giving up coffee....
/rcw
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-13-2011 04:24 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-13-2011 04:36 AM
тАО01-13-2011 04:36 AM
Re: AND, OR with IF and GREP - statements in shell scripting
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-13-2011 05:03 AM
тАО01-13-2011 05:03 AM
Re: AND, OR with IF and GREP - statements in shell scripting
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-13-2011 05:05 AM
тАО01-13-2011 05:05 AM
Re: AND, OR with IF and GREP - statements in shell scripting
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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-13-2011 05:09 AM
тАО01-13-2011 05:09 AM
Re: AND, OR with IF and GREP - statements in shell scripting
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-13-2011 05:10 AM
тАО01-13-2011 05:10 AM
Re: AND, OR with IF and GREP - statements in shell scripting
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-13-2011 07:20 PM
тАО01-13-2011 07:20 PM
Re: AND, OR with IF and GREP - statements in shell scripting
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-13-2011 08:42 PM
тАО01-13-2011 08:42 PM
Re: AND, OR with IF and GREP - statements in shell scripting
You can emulate 'and' operations with 'grep' by counting matches. Using your data, you might do:
if [ $(grep -E "rds :|crs :|mmd :|kms :" filename|grep -c Active) = 4 ]; then
echo "all ok"
else
echo "at least one down"
fi
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-15-2011 12:24 AM
тАО01-15-2011 12:24 AM
Re: AND, OR with IF and GREP - statements in shell scripting
>if [ $(grep -E "rds :|crs :|mmd :|kms :" filename | grep -c Active) = 4 ]
This will check for 4 types of lines. And if there are exactly 4 that have "Active", even if all are "rds :".
(And you should replace that "=" by -eq.)
If you want to do AND operations on lines, you can match patterns. (Only need one if you know the order. :-)
grep -e "pattern1.*pattern2" -e "pattern2.*pattern1" file