每当我执行此代码时都会出现语法错误

每当我执行此代码时都会出现语法错误

每当我执行此代码时,我都会收到错误语法错误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 行为奇怪且违反直觉的典型中级语义问题。

  • 指出细微的警告、极端情况和陷阱,这些可能会导致高级用户的原本可以正常工作的脚本在未来情况下失败。

来源壳牌检测

相关内容