ข้อผิดพลาดทางไวยากรณ์เมื่อใดก็ตามที่ฉันเรียกใช้รหัสนี้


-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

Superuser ไม่ใช่บริการตรวจสอบสคริปต์ คุณลองทำอะไรและคุณติดอยู่ที่ไหน
Moses

ถ้า [-z $ loc]; ถ้า uname -a | grep 64 & gt; / dev / null; sdir = $ KALDI_ROOT / tools / srilm / bin / i686-m64 else sdir = $ KALDI_ROOT / tools / srilm / bin / i686 fi หาก [-f $ sdir / ngram-count]; echo "การใช้เครื่องมือสร้างแบบจำลองภาษา SRILM จาก $ sdir" ส่งออก PATH = $ PATH: $ sdir อื่น echo "ชุดเครื่องมือ SRILM อาจไม่ได้ติดตั้งคำแนะนำ: เครื่องมือ / install_srilm.sh" ทางออก 1 fi fithis คือรหัสที่ฉันต้องการ ข้อผิดพลาดทางไวยากรณ์
nagma

ฉันเห็นรหัสแล้วคุณโพสต์ไว้ในคำถามของคุณ ข้อผิดพลาดทางไวยากรณ์เกิดขึ้นที่ไหน
Moses

คำตอบ:


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 - เครื่องมือวิเคราะห์สคริปต์เชลล์แบบสแตติก

ShellCheck เป็นเครื่องมือ GPLv3 ที่ให้คำเตือนและคำแนะนำสำหรับ   สคริปต์เชลล์ bash / sh:

สกรีนช็อตของเทอร์มินัลแสดงบรรทัดสคริปต์เชลล์ที่มีปัญหา   ไฮไลต์

enter image description here

เป้าหมายของ ShellCheck คือ

  • เพื่อชี้ให้เห็นและชี้แจงปัญหาทางไวยากรณ์ของผู้เริ่มต้นทั่วไปที่ทำให้เชลล์ให้ข้อความแสดงข้อผิดพลาดแบบเข้ารหัส

  • เพื่อชี้ให้เห็นและชี้แจงปัญหาความหมายระดับกลางทั่วไปที่ทำให้เชลล์ทำงานผิดปกติและ   เคาน์เตอร์สังหรณ์ใจ

  • เพื่อชี้ให้เห็น caveats ที่ละเอียดอ่อนมุมตัวพิมพ์เล็กและข้อผิดพลาดที่อาจทำให้สคริปต์การทำงานของผู้ใช้ขั้นสูงล้มเหลวในอนาคต   พฤติการณ์

แหล่ง ShellCheck

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.