Jill448
Jill448

Reputation: 1793

shell grep command from the output of wget

I have a script to check the status of a url. I am trying to grep for the word connected from the below mentioned wget command output and print Running if it is found else print Not Running. How can I modify my grep to print only the word without the all the output from wget command

#!/bin/ksh
STAT=`wget 'http://server:port/ABC_Service/app' | grep connected`
if [ -z "$STAT" ] ; then
   echo "Running"
else
  echo "Not Running"
fi

Output of wget command:

--2013-05-31 11:09:32--  http://server:port/ABC_Service/app
Resolving server... 10.109.136.31
Connecting to server|10.109.136.31|:port... connected.
HTTP request sent, awaiting response... 401 Unauthorized
Authorization failed.

Desired Output from my script:

Running

Upvotes: 1

Views: 2532

Answers (2)

iruvar
iruvar

Reputation: 23374

Check the return code

#!/bin/ksh
#use your wget command in place of echo below
echo "connected" 2>&1 | grep connected >/dev/null
retcode=$?
if [ $retcode = 0 ]
then
   echo "Running"
else
  echo "Not Running"
fi

Upvotes: 1

Claudio
Claudio

Reputation: 10947

If you can use Bash, then the variable $? reads the exit status of the last command executed.

Upvotes: 0

Related Questions