【发布时间】:2015-01-23 04:23:19
【问题描述】:
chkconfig --list | 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
跟踪执行后: 它输出“Start...”,运行“chkconfig --list | grep --quiet jetty”后脚本退出,所以“echo $?”后面的跟踪什么也不输出。只是不知道为什么管道失败。如果我评论“set -o pipefail”,那么脚本就可以正常工作。
环境:
1)CentOS release 6.6 (Final) 64bit
2)GNU bash,版本 4.1.2(1)-release (x86_64-redhat-linux-gnu)
3)GNU grep 2.6.3
【问题讨论】:
-
你是什么意思它在脚本中不起作用?您是使用
bash还是/bin/sh运行脚本? -
另外,chkconfig --list 输出确实包含“jetty”
-
嗨,Etan,我只是以 /bin/sh 和 "./test.sh" 形式运行它,但仍然失败。正如您所问的“您的意思是它在脚本中不起作用是什么意思?” :我把“回声$?”在 "chkconfig --list | grep --quiet jetty" 之后跟踪它,但它会停在那里而不去 "echo $?"
-
脚本的
#!行是什么?#!/bin/sh? -
你还没有告诉我们这里的“失败”是什么。它会爆炸吗?它会杀死一只小猫吗?它找不到输出吗?什么?