【发布时间】:2021-07-17 18:31:51
【问题描述】:
我这里有这个脚本:
killall -q $1
#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 而失败。
我应该回去吗?或者有没有办法解决这个问题?
【问题讨论】:
-
shellcheck.net 会发现问题的。
-
@Socowi:老实说,我什至没有阅读代码。我刚刚对其运行了 shellcheck,它告诉我错误是什么:D
标签: bash if-statement nested sh nested-if