i have written my shell script in notepad however i am struggling to pass the data file to be read to the script the data file is of .txt format. My target is to run the shell script from the terminal and pass 3 arguments e.g. polg@DESKTOP-BVPDC5C:~/CS1420/coursework$ bash valsplit.sh input.txt correct.txt incorrect.txt . input.txt to be the file that is read and corrct and incorrect.txt is to be written to.
below is the script
#!/bin/bash
echo -n "" > $2
#correct.txt - gives it an empty string
echo -n "" > $3
#incorrect.txt - gives it an empty string
function checkImmediate {
if ! [[ $1 =~ \-?[0-9]+ ]]; then # Checking if it is less than 0 or greater than 9
problems=true # Then the problem will be true
echo "This is checking it if is not in the range of 0-9"
fi
if [[ $1 -gt 32768 || $1 -lt -32768 ]]; then #If the immediates are greater than this range
problems=true #Then the problem is equal to true
echo "Checking if it is greater than 32768 and less than -32768" #This is checking if it is greater than 32768, and less than -32768
fi
}
function checkRegister {
# $t1="$t0"
echo $t1
dollarSign=${1:0:1} # This is the sign bit $
char=${1:1:1} # This is the character s
num=${1:2:2} # This is the number 1
if ! [[ dollarSign =~ $ ]]; then #This is checking if the dollarsign is not equal to the dollarsign varible $
problems=true #If that is the case then the problems is true
echo "This does not have the pattern dollarsign $"
elif [[ $num =~ \-?[0-9]+\(.+\) ]]; then #This is checking it is greater than the pattern 0-9
problems=true
echo "Then this is greater than the pattern 0-9 and an error has occured"
elif [ $num -lt 0 ]; then #Don't know what this does .
problems=true
echo "This is less than 0/greater than 0-9 and a problem has occured"
elif [ $char == "s" ]; then #This is checking if char is equal to s
if [ $num -gt 7 ]; then #if the number is greater than 7 for num then a problem is true, as this does not go greater than 7
problems=true
echo "Then this does not has s and the number is greater than 7"
fi
elif [ $char == "t" ]; then #This is checking if the char is equal to t
if [ $num -gt 9 ]; then
problems=true
echo "Then this does not has t and the number is greater than 9"
fi
elif [[ $char =~ [a-z] ]]; then
problems=true
echo "This register has the letter which is not a-z"
else
problems=true
echo = "Then the problem is out of these bounds"
fi
}
while read line; do
echo $line # add $s0 $s1 $s2
array=($line) # ('add', '$s0', '$s1', '$s2')
instruction=${array[0]} # add
problems=false
if [[ $instruction =~ add|sub ]] && ! [[ ${instruction:3:1} =~ [a-z] ]]; then #check if instrction is equl to add or sb
echo "R Format"
iregisters=(${array[1]} ${array[2]} ${array[3]}) # ('$s0', '$s1', '$s2')
for i in ${iregisters[@]}; do
checkRegister $i
done
elif [[ $instruction =~ addi ]]; then # immediate as parameter | addi $t3 $s0 -9
echo "I Parameter Format"
ipararegisters=(${array[1]} ${array[2]}) # You don't need to check the last one as that is an immediate
immediate=${array[3]} #last immediate = 3
for i in ${ipararegisters[@]}; do
checkRegister $i
done
checkImmediate $immediate
elif [[ $instruction =~ lw|sw ]]; then
echo "I Offseted Format"
register1=${array[1]} # $s0
secondItem=${array[2]} # 8($t0)
immediate=${secondItem%(*} # 8
echo $immediate
checkImmediate $immediate
register2=${secondItem%)*} # 8($t2
register2=${register2##*(} # $t2
ioffsetregisters=($register1 $register2)
for i in ${ioffsetregisters[@]}; do
checkRegister $i
done
else
problems=true
echo "Instruction is not valid"
fi
if [[ "$problems" = false ]]; then
echo "Success"
echo $line >> $2 # true = write the line into correct.txt
else
echo "Failure"
echo $line >> $3
fi
here is the .txt file to be read called input.txt
#i n p u t . t x t
add $s0 $s1 $s2
sub $s2 $t0 $t3
add $s0 $s1 $z2
lw $t1 8($t2)
addi $t3 $s0 9
sw $s3 4($t0)
lw $t11 70000($s0)
please can someone see what is wrong with my script and why it wont read my file and can someone coach me on how to run the script from the terminal.
i get this message when i attempt to run the script in the terminal like this : ./valsplit.sh input.txt correct.txt incorrect.txt
error message = valsplit.sh: line 6: $'\r': command not found
valsplit.sh: line 7: syntax error near unexpected token `$'{\r''
'alsplit.sh: line 7: `function checkImmediate {
thanks for looking at my post guys, please help