gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:33:16 27 4
gpt4 key购买 nike

我有一个脚本可以检查一些基本 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}
}

知道为什么会抛出错误消息吗?

最佳答案

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'.

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

关于linux - 意外标记附近的语法错误 '{',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29282335/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com