Linux在运行bash脚本时跳过了我的else语句?

问题描述 投票:0回答:1

所以我需要让它说这个人不在这个类里。好像是跳过了我的else语句。cut -d ',' -...

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
The exit code of the piped commands is the exit code of the last one (
bash
1个回答
1
投票

Use tr option in your script to break piping if one of the commands failed.0Example:

So your script could be:set -o pipefailPS: Use

to restore the usual behavior.

$ 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 "$#"
fi

set +o pipefail所以我需要让它说这个人不在这个班级里,它好像跳过了我的 else 语句。它好像跳过了我的 else 语句,有什么问题吗?有什么问题吗? if [ $# == 1 ]; then if grep "$1" acctcommonCSCE215。

© www.soinside.com 2019 - 2024. All rights reserved.