所以我得让它说那个人不在课堂上。似乎它跳过了else语句。有什么问题吗?
if [ $# == 1 ]; then
if grep "$1" /acct/common/CSCE215 | cut -d ',' -f1-3 | tr ',' ' '; then
true
else
echo "Sorry that person is not in CSCE215 this semester"
fi
else
echo "Command line arguments are not equal to 1"
echo "$#"
fi发布于 2020-04-24 01:51:42
管道命令的退出代码是最后一个命令的退出代码(在您的示例中是tr,它始终是0)。
如果其中一个命令失败,请在脚本中使用set -o pipefail选项中断管道。
示例:
$ echo foo | grep bar | tr o a ; echo $?
0
$ set -o pipefail ; echo foo | grep bar | tr o a ; echo $?
1所以你的剧本可能是:
set -o pipefail
if [ $# -eq 1 ]; then
if grep "$1" /acct/common/CSCE215 | cut -d ',' -f1-3 | tr ',' ' '; then
true
else
echo "Sorry that person is not in CSCE215 this semester"
fi
else
echo "Command line arguments are not equal to 1"
echo "$#"
fiPS:使用set +o pipefail恢复通常的行为。
https://stackoverflow.com/questions/61399593
复制相似问题