【问题标题】:KornShell check using exit codesKornShell 使用退出代码进行检查
【发布时间】:2012-01-03 10:20:46
【问题描述】:

我正在尝试创建一个 KornShell (ksh) 脚本来检查进程的状态。 如果状态是“通过”,它只会回显“通过”。 如果状态为“失败”,它将发送警报。

我可以想到两种方法:

  1. 将进程状态捕获到临时文件中,并在该文件中搜索术语“通过”或“失败”并采取相应措施

    例如

    servicename > tmp.file
    grep pass tmp.file
    
    if exists, echo "pass"
    
  2. Grep 表示“通过”或“失败”,并使用退出代码进行其他操作

    例如

    servicename | grep pass
    
    if exit $? = 0, echo "pass"
    
    else do something
    

您如何看待上述两种方法,您会如何处理? 任何代码 sn-ps 将不胜感激。

【问题讨论】:

  • servicename 是否在失败时将其退出状态设置为非零?

标签: shell unix grep ksh


【解决方案1】:

假设 servicename 在失败时返回非零退出代码,您可以这样做:

if servicename > /dev/null 2>&1
then
    echo pass
else
   # do something
fi

如果servicename 没有正确设置其退出代码:

if servicename | grep -q pass
then
    echo pass
else
   # do something
fi

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 2012-01-02
    • 2014-01-25
    • 2013-09-14
    • 1970-01-01
    • 2010-12-30
    相关资源
    最近更新 更多