exit and echo $?
exit is very useful to debug the program, its syntax is : # exit n, Here n is a number between 0-255, if it is bigger than 255, for example, 256, the value retuned by issuing”echo $? ” will be evaluted in the following equation: n-255-1
That says, when n is 266, the returned result is 256-255-1 = 0 which is also the value of “$?”
eg:
# !/bin/bash
# ifexit
echo “Please input a number: ”
# input the testing number
read NUMB
# if statement
if [ “$NUMB” -eq “1” ]
then
echo “$NUMB = 1”
elif [ “$NUMB” -eq “2” ]
then
exit 222
else
exit 278
fi
Input | Output
—————————–
2 222
3 278-256=22
分类: 科技