【问题标题】:shell grep command from the output of wgetwget 输出中的 shell grep 命令
【发布时间】:2013-06-01 00:09:36
【问题描述】:

我有一个脚本来检查 url 的状态。我正在尝试从下面提到的 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

【问题讨论】:

    标签: grep wget


    【解决方案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
    

    【讨论】:

    • 它打印整个 wget 命令的输出以及它的打印 retcode 为1 并在连接时显示为Not Running--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. Not Running
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-21
      • 2021-07-28
      • 1970-01-01
      • 2020-04-20
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多