![每当我执行此代码时都会出现语法错误](https://linux22.com/image/1496270/%E6%AF%8F%E5%BD%93%E6%88%91%E6%89%A7%E8%A1%8C%E6%AD%A4%E4%BB%A3%E7%A0%81%E6%97%B6%E9%83%BD%E4%BC%9A%E5%87%BA%E7%8E%B0%E8%AF%AD%E6%B3%95%E9%94%99%E8%AF%AF.png)
每当我执行此代码时,我都会收到错误语法错误else
。
以下是代码:
if [ -z $loc ] then
if [uname -a| grep 64 >/dev/null] then
sdir=$KALDI_ROOT/tools/srilm/bin/i686-m64
else
sdir=$KALDI_ROOT/tools/srilm/bin/i686
fi
if [ -f $sdir/ngram-count ] then
echo "Using SRILM language modelling tool from $sdir"
export PATH=$PATH:$sdir
else
echo "SRILM toolkit is probably not installed.
Instructions: tools/install_srilm.sh"
exit 1
fi
fi
答案1
我收到错误语法错误else
您可以使用http://www.shellcheck.net/检查你的语法:
$ shellcheck myscript
Line 1:
if [ -z $loc ] then
^-- SC1010: Use semicolon or linefeed before 'then' (or quote to make it literal).
Line 2:
if [uname -a| grep 64 >/dev/null] then
^-- SC1009: The mentioned parser error was in this if expression.
^-- SC1073: Couldn't parse this test expression.
^-- SC1035: You need a space after the [ and before the ].
^-- SC1009: Use 'if cmd; then ..' to check exit code, or 'if [[ $(cmd) == .. ]]' to check output.
^-- SC1035: You are missing a required space here.
^-- SC1072: Fix any mentioned problems and try again.
$
修复明显的错误(缺少;
s 和空格导致:
if [ -z $loc ]; then
if [ uname -a | grep 64 >/dev/null ] then
sdir=$KALDI_ROOT/tools/srilm/bin/i686-m64
else
sdir=$KALDI_ROOT/tools/srilm/bin/i686
fi
if [ -f $sdir/ngram-count ]; then
echo "Using SRILM language modelling tool from $sdir"
export PATH=$PATH:$sdir
else
echo "SRILM toolkit is probably not installed.
Instructions: tools/install_srilm.sh"
exit 1
fi
fi
和:
$ shellcheck myscript
Line 2:
if [ uname -a | grep 64 >/dev/null ]; then
^-- SC1009: The mentioned parser error was in this if expression.
^-- SC1073: Couldn't parse this test expression.
^-- SC1009: Use 'if cmd; then ..' to check exit code, or 'if [[ $(cmd) == .. ]]' to check output.
^-- SC1072: Fix any mentioned problems and try again.
$
您可以自行修复剩余的错误。
ShellCheck - 一个shell脚本静态分析工具
ShellCheck 是一个 GPLv3 工具,可以为 bash/sh shell 脚本提供警告和建议:
终端的屏幕截图,突出显示了有问题的 shell 脚本行。
ShellCheck 的目标是
指出并澄清导致 shell 给出神秘错误消息的典型初学者语法问题。
指出并澄清导致 shell 行为奇怪且违反直觉的典型中级语义问题。
指出细微的警告、极端情况和陷阱,这些可能会导致高级用户的原本可以正常工作的脚本在未来情况下失败。
来源壳牌检测