I've got a script that checks the syntax of some basic MIPS instructions. When I run it, however, I get the following output in the console:
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'
Here's the main script:
#!/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
And here's the script containing the functions:
#!/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}
}
Any ideas why it's throwing the error message?