shell 意外标记“then”

drnojrws  于 8个月前  发布在  Shell
关注(0)|答案(1)|浏览(107)

日安
我在我自己的debian笔记本上测试shell脚本

android-verify.sh: line 14: syntax error near unexpected token `then'
android-verify.sh: line 14: `if [[ ! -z $iopt ]] then { wd=$(pwd) basename "$(test -L "$0" && readlink "$0" || echo "$0")" > /tmp/scriptname'

我还是新来的。请告知。
当尝试包含;
在以下说明中

if [[ ! -z $iopt ]]**;** then { wd=$(pwd) basename "$(test -L "$0" && readlink "$0" || echo "$0")" > /tmp/scriptname

scriptname=$(echo -e -n $wd/ && cat /tmp/scriptname); su -c "cp $scriptname /usr/bin/monitor" root && echo "Congratulations! Script Installed, now run monitor Command" || echo$
fi

这个bash终端返回以下消息

android-verify.sh: line 17: syntax error near unexpected token `fi'
android-verify.sh: line 17: `fi '
kuhbmx9i

kuhbmx9i1#

下面是修改后的代码,以解决问题并使其更具可读性:

if [[ ! -z $iopt ]] ; then
    wd=$(pwd)
    basename "$(test -L "$0" && readlink "$0" || echo "$0")" > /tmp/scriptname
    scriptname=$(echo -e -n $wd/ && cat /tmp/scriptname)
    su -c "cp $scriptname /usr/bin/monitor" root && echo "Congratulations! Script Installed, now run monitor Command" || echo
fi
  • 我重新格式化了代码,使它更清楚正在发生的事情,也能够与您讨论行号。它还有助于允许shell显示行号,以防出现其他错误。
  • 在第一行中,您猜测在then之前需要一个分号,这是正确的。
  • 在第二行的wd变量中,base之前没有分号,因此wd变量实际上只为base命令设置,而不是任何后续命令。
  • 在第5行有三个echo,最后一行有一个'$',这是不正确的,这将导致一个错误的命令不存在。

所以总结一下:

  • 考虑将代码重新格式化为换行符,以提高可读性。
  • if测试和then语句之间添加“;”
  • wdbase之间添加“;”或换行符
  • echo$更改为echo

希望能帮上忙。

相关问题