Reputation: 13
So I need to have it say the person is not in the class. It seems like it is skipping my else statement. Is something wrong?
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
Upvotes: 1
Views: 117
Reputation: 15614
The exit code of the piped commands is the exit code of the last one (tr
in your case, which is always 0
)
Use set -o pipefail
option in your script to break piping if one of the commands failed.
Example:
$ echo foo | grep bar | tr o a ; echo $?
0
$ set -o pipefail ; echo foo | grep bar | tr o a ; echo $?
1
So your script could be:
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
PS: Use set +o pipefail
to restore the usual behavior.
Upvotes: 1