[英]chkconfig --list | grep --quiet “jetty” won't work in shell scripts
chkconfig-列表| grep --quiet“ jetty”在命令行或脚本中使用而没有“ set -o pipefail”,而不在脚本中使用“ set -o pipefail”。 为什么?
原始脚本:
#!/usr/bin/env bash
set -e
set -o pipefail
echo -e "checking jetty is installed ...\n"
echo -e "start...\n"
chkconfig --list | grep --quiet "jetty"
echo $?
echo -e "end...\n"
if [ $? -eq 0 ]; then
echo "ok"
else
echo "fail"
fi
跟踪执行后:它输出“开始...”,运行“ chkconfig --list | grep --quiet jetty”后脚本退出,因此“ echo $?” 后者跟踪什么也不输出。 只是不知道为什么管道失败了。 如果我评论“ set -o pipefail”,则脚本工作正常。
环境:
1)CentOS版本6.6(最终版)64位
2)GNU bash,版本4.1.2(1)-发行版(x86_64-redhat-linux-gnu)
3)GNU grep 2.6.3
使用set -e
,如果grep
找不到匹配项,脚本将退出-这就是set -e
所做的,它将所有错误转换为致命错误。 您需要包装任何可能在错误处理条件中失败的命令。 幸运的是,即使没有set -e
,这也是您应该编写此特定脚本的方式。
if chkconfig --list | grep --quiet 'jetty'; then
echo "ok"
else
echo "fail"
fi
您不应使用set -e
,因为如果遇到任何错误命令,它将使脚本退出。 还好像在echo $?
在chkconfig --list | grep --quiet "jetty"
chkconfig --list | grep --quiet "jetty"
更改了您的“ $?” 值。
所以你可以这样写:
if chkconfig --list | grep --quiet "ssh"
then
echo "ok"
else
echo "fail"
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.