来自wget输出的shell grep命令

时间:2013-05-31 15:14:19

标签: grep wget

我有一个脚本来检查网址的状态。我试图从下面提到的wget命令输出中找到connected这个词,如果找到则打印Running,打印Not Running。如何修改我的grep只打印单词而不输出wget命令的所有输出

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

输出wget命令:

--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.

我的脚本所需的输出:

Running

2 个答案:

答案 0 :(得分:1)

检查返回码

#!/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

答案 1 :(得分:0)

如果你可以使用Bash,那么变量$?读取上次执行的命令的退出状态。