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

时间:2015-03-26 15:21:05

标签: linux bash shell command-line

我有一个脚本可以检查一些基本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=$1
correct=$2
incorrect=$3

#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 "${$1[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=( $1 )
  word=${1[0]}
  if [[ $word == add ]] || [[ $word == sub ]] || [[ $word == addi ]]; then 
    if [[ ${#$1[@]} != 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 [[ ${#$1[@]} != 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="$1"

  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 [ ! $1 -ge -32768 -a $1 -le 32767 ]; then
    $error_file="$error_file \n Out of bounds immediate $1. 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 [[ $1 == sw* ]] || [[ $1 == lw* ]]; then
    a=( $1 )
    second_param="${a[2]}"
    check_immediate_range ${second_param:0:1}
    check_register_syntax ${second_param:2:5}
}

为什么它会抛出错误信息?

1 个答案:

答案 0 :(得分:2)

ShellCheck报告:

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

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

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