意外标记“{”附近的语法错误

syntax error near unexpected token '{'

我有一个脚本可以检查一些基本 MIPS 指令的语法。然而,当我 运行 它时,我在控制台中得到以下输出:

bash valsplit.sh input.txt correct.txt incorrect.txt
functions.sh: line 31: syntax error near unexpected token `}'
functions.sh: line 31: `}'
valsplit.sh: line 49: syntax error near unexpected token `done'
valsplit.sh: line 49: `  done <$input'

这是主要脚本:

#!/bin/bash 
#this script validates the syntax of MIPS instructions provided in the input.txt file
#the script requires 3 param: input.txt, correct.txt and incorrect.txt -- in that order!

source functions.sh

input=
correct=
incorrect=

#Returns error message if incorrect number of arguments given.  
if [ $# -eq 3 ]; then

  error_count=0
  error_file="" 

  #Iterate over every line in the file
  while read line; do

    error_file=$line

    check_correct_instruction_syntax $line
    check_correct_num_of_params $line

    if [ $line == add* ] || [ $line == sub* ]; then
      a=( $line )
      check_register_syntax ${a[1]} 
      check_register_syntax ${a[2]}
      check_register_syntax ${a[3]}
    else if [ $line == addi* ]; then 
      a=( $line )
      check_register_syntax ${a[1]}
      check_register_syntax ${a[2]}
      check_immediate_range ${a[3]}
    else if [ $line == lw* ] || [ $line == sw* ]; then
      a=( $line )
      check_register_syntax ${a[1]}
      check_sw_or_lw_immediate_register_param ${a[2]}
    fi

    if [[ $error_count > 0 ]]; then 
      echo "$error_file"
      $line >> $incorrect
      $error_count=0
    else 
      $line >> $correct
    fi 

  done <$input

else 
  echo "You need to provide 3 arguments in the following order: input.txt, correct.txt, incorrect.txt"
fi

这是包含函数的脚本:

#!/bin/bash

#Checks instruction syntax is correct
#Pass the entire line as an argument
check_correct_instruction_syntax() {
  for word in "${[0]}"; do
    if [[ $word != add ]] && [[ $word != sub ]] && [[ $word != addi ]] && [[ $word != lw ]] && [[ $word != sw ]]; then
      $error_file="$error_file \n Incorrect instruction. Use add, sub, addi, lw or sw." 
      error_count=`expr $error_count + 1`
    fi 
  done        
}

#Checks number of paramters is correct
#Pass the entire line as an argument
check_correct_num_of_params() {

  a=(  )
  word=${1[0]}
  if [[ $word == add ]] || [[ $word == sub ]] || [[ $word == addi ]]; then 
    if [[ ${#[@]} != 4 ]]; then  
      $error_file="$error_file \n Incorrect number of paramters for instruction ${a[0]}. It should have 3."
      error_count=`expr $error_count + 1`
    fi
  else if [[ $word == lw ]] || [[ $word == sw ]]; then 
    if [[ ${#[@]} != 3 ]]; then 
      $error_file="$error_file \n Incorrect number of paramters for instruction ${a[0]}. It should have 2."
      error_count=`expr $error_count + 1`
    fi
  fi  
}

#Checks register syntax is correct
#Pass the register to be checked as an argument
check_register_syntax() {

  register=""

  if [ "${register:0:1}" != "$" ]; then 
    $error_file="$error_file \n Incorrect register syntax. Registers should start with a $."
    error_count=`expr $error_count + 1`
  fi

  if [[ ! ${register:1:2} == "t" ]] && [[ ! ${register:1:2} == "s" ]]; then
    $error_file="$error_file \n Unrecognized register name $register. Use s or t."
    error_count=`expr $error_count + 1`
  fi

  if [ "${register:1:2}" = "t" ]; then
    if [ ! "${register:2:3}" -ge 0 -a "${register:2:3}" -le 9 ]; then
      $error_file="$error_file \n Incorrect register number. Temporary registers are numbered 0 to 9."
      error_count=`expr $error_count + 1`
    fi
  fi  


  if [ "${register:1:2}" = "s" ]; then 
    if [ ! "${register:2:3}" -ge 0 -a "${register:2:3}" -le 7 ]; then
      $error_file="$error_file \n Incorrect register number. Permanent registers are numbered 0 to 7."
      error_count=`expr $error_count + 1`
    fi 
  fi

}

#Check the immediates are within the correct range
#Pass the immediate as an argument 
check_immediate_range() {
  if [ !  -ge -32768 -a  -le 32767 ]; then
    $error_file="$error_file \n Out of bounds immediate . Immediates range between -32768 and 32767."
    error_count=`expr $error_count + 1`
  fi
}

#Check the second param for sw or lw contains an immediate and a register
#Pass the entire line as a paramter
check_sw_or_lw_immediate_register_param() {
  if [[  == sw* ]] || [[  == lw* ]]; then
    a=(  )
    second_param="${a[2]}"
    check_immediate_range ${second_param:0:1}
    check_register_syntax ${second_param:2:5}
}

知道它抛出错误消息的原因吗?

ShellCheck 报告:

else if [ $line == addi* ]; then 
^–– SC1075 Use 'elif' instead of 'else if'.
                   ^–– SC2081 [ .. ] can't match globs. Use [[ .. ]] or grep.

if [[  == sw* ]] || [[  == lw* ]]; then
^–– SC1046 Couldn't find 'fi' for this 'if'.

还有许多其他错误和警告。 运行 通过他们的站点查看您的脚本的完整列表。