执行此代码时出现语法错误


-3

每当我执行此代码时,我都会收到错误语法错误 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

超级用户不是脚本检查服务。你有什么尝试,你在哪里卡住?
Moses

如果[-z $ loc];那么如果uname -a | grep 64> / dev / null;那么sdir = $ KALDI_ROOT / tools / srilm / bin / i686-m64 else sdir = $ KALDI_ROOT / tools / srilm / bin / i686 fi if [-f $ sdir / ngram-count];然后echo“使用来自$ sdir的SRILM语言建模工具”export PATH = $ PATH:$ sdir else echo“SRILM toolkit可能没有安装。说明:tools / install_srilm.sh”exit 1 fi fithis是我需要知道的代码语法错误
nagma

我见过代码,你在问题中发布了它。语法错误发生在哪里?
Moses

Answers:


2

我接近错误语法错误 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脚本行的终端的屏幕截图   突出显示。

enter image description here

ShellCheck的目标是

  • 指出并澄清典型的初学者的语法问题,导致shell提供神秘的错误消息。

  • 指出并澄清导致shell表现奇怪的典型中级语义问题   与直觉相反。

  • 指出可能导致高级用户的其他工作脚本在未来失败的微妙警告,角落案例和陷阱   情况。

资源 ShellCheck

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.