如何修复此 bash 脚本?
How to fix this bash script?
我这里有这个脚本:
killall -q
#turns out system already has a method for this, yey
#-q because we already have a error handling
returned=$?
#does this so that it doesn't read the success state of the ifs
if [ $returned = 0 ]
then
printf "Process kill attempt returned "
echo $returned
echo "Process killed sucessfully."
#yey we did it
else
#oh noes it failed
printf "Process kill attempt returned "
echo $returned
echo "Process kill attempt failed."
printf "Most likely cause of failure: "
if [ $returned = 1 ]
then
echo "process does not exist or is not running"
elif [ $returned = 2 ]
echo "process is system task; insufficient permissions"
else
echo "unknown failure " $returned "; no known fail in database has this value"
fi
fi
看的时候没问题,但是运行的时候就报错了。
nathan@HAL-LINUX:~$ xscreensaver
nathan@HAL-LINUX:~$ killproc xscreensaver
/usr/local/bin/killproc: line 23: syntax error near unexpected token `else'
/usr/local/bin/killproc: line 23: ` else'
nathan@HAL-LINUX:~$
我的原始脚本省略了嵌套 if,直接告诉您它因错误 3 或 1 或 2 而失败。
我应该回去吗?或者有什么办法可以解决这个问题?
您忘记了 elif
之后的 then
:
if [ $returned = 1 ]
then
echo "process does not exist or is not running"
elif [ $returned = 2 ]
then
echo "process is system task; insufficient permissions"
else
echo "unknown failure " $returned "; no known fail in database has this value"
fi
我这里有这个脚本:
killall -q
#turns out system already has a method for this, yey
#-q because we already have a error handling
returned=$?
#does this so that it doesn't read the success state of the ifs
if [ $returned = 0 ]
then
printf "Process kill attempt returned "
echo $returned
echo "Process killed sucessfully."
#yey we did it
else
#oh noes it failed
printf "Process kill attempt returned "
echo $returned
echo "Process kill attempt failed."
printf "Most likely cause of failure: "
if [ $returned = 1 ]
then
echo "process does not exist or is not running"
elif [ $returned = 2 ]
echo "process is system task; insufficient permissions"
else
echo "unknown failure " $returned "; no known fail in database has this value"
fi
fi
看的时候没问题,但是运行的时候就报错了。
nathan@HAL-LINUX:~$ xscreensaver
nathan@HAL-LINUX:~$ killproc xscreensaver
/usr/local/bin/killproc: line 23: syntax error near unexpected token `else'
/usr/local/bin/killproc: line 23: ` else'
nathan@HAL-LINUX:~$
我的原始脚本省略了嵌套 if,直接告诉您它因错误 3 或 1 或 2 而失败。
我应该回去吗?或者有什么办法可以解决这个问题?
您忘记了 elif
之后的 then
:
if [ $returned = 1 ]
then
echo "process does not exist or is not running"
elif [ $returned = 2 ]
then
echo "process is system task; insufficient permissions"
else
echo "unknown failure " $returned "; no known fail in database has this value"
fi