grep function

Hi Guys,

I have a very limited knowledge on shell scripting.
When I execute dspmq, I get either

" Running" or "Running in Standby" as output

$dspmq
QM1    Running
QM2    Running as StandBy

I want my script to run only if the output of dspmq is "Running".

I executed the below command

dspmq -m qmgrname |grep "Running

"

But my script is getting kicked off for both "Running" and "Running as Standby"

When I grep for "Running", I am getting both "Running" and "Running as Standby".

As "Running" is available in both the strings, I am getting my script kicked for both cases.
How do I get my script kicked for "Running" alone.

Let me know your thoughts.

Grep uses regex, $ is meta for end of line, and beware trailing white space.

 
grep 'Running *$'

Line tools like grep do not work for linefeeds in the pattern, as the buffer is one line.

error

why don't you grep for "as StandBy" i.e.

dspmq |grep "as StandBy" && {
echo "kick off your script here within these braces"
}

Assuming bash/ksh/zsh:

if [ $(dspmq |grep -vc "as StandBy") -ge 1 ]
then
# code here
fi

This counts the number of lines that don't match "as StandBy"

Andrew

For grep, the nicest test wrapper is the -q, as it is terse, grep can stop at the first hit and just exit 0 else exit 1 at eof, with no additional interpreting commands like -ge:

if ( dspmq | grep -q "as StandBy" )
then
 . . .
else
 . . .
fi

I guess even the () can be dropped, as they cost you a fork for a subshell, but I like some wrapper.

OP is interested in "Running", not "Running as Standby". Your solution would yield a false positive. Yes, using

if ( dspmq | grep -q -v "as StandBy" )

will work without giving a false positive, but certain Unices don't follow the POSIX switch for "quiet", using -s instead.

Andrew