How to get the shell script to read the .txt file as an input/data?

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

When you see \r, it means "stop editing your files in Microsoft Notepad". It has filled your file with invisible carriage returns which will stop it from working.

You can clean it up with:

tr -d '\r' < inputfile.sh > outputfile.sh

inputfile and outputfile cannot be the same.

Where would I exactly write those commands? And any idea on how to get the shell to read the input file? Any advice on what to write the input file in instead of notepad?

In the shell.

while read file
do
...
done < inputfile

Write it inside the shell using any UNIX text editor you like. nano, pico, vi, emacs... Whichever you happen to have.

while read file
do
...
done < inputfile

Is file and input file the same thing?

And would it be input.txt

while read variablename
do
...
done < input.txt

I probably sound really annoying but where exactly does it fit in the current script listed above?

There is exactly one while read in your script. That's where it belongs.

The ... is the stuff inside the loop.

The 'done' at the end of the loop is something your script is missing right now and won't work without.

Basically, what you have already:

while read variablename
do
...
done

What you need:

while read variablename
do
...
done < inputfile

Ah I seem to understand, I will get back to you if I run into any issues

---------- Post updated at 09:18 PM ---------- Previous update was at 08:19 PM ----------

what exactly would i write in the shell for the input file. The text in the input file is :
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)

how would i type this in the shell to be the input file that is to be read?

Probably something along the lines of

nano filename

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)

control-W
enter
control-X

If you really have no editor whatsoever - get one ASAP - you can cheat with

cat > input.txt <<EOF
addi $t3 $s0 9
sw $s3 4($t0)
lw $t11 70000($s0)

addi $t3 $s0 9
sw $s3 4($t0)
lw $t11 70000($s0)
EOF

The "ASAP method" needs to avoid the shell's $var expansions.
Put the EOF in quotes!

cat > input.txt <<"_EOF_"
addi $t3 $s0 ?9
sw $s3 4($t0)
lw $t11 70000($s0)

addi $t3 $s0 ?9
sw $s3 4($t0)
lw $t11 70000($s0)
_EOF_

Still the shell might do some substititions. A safe method is to read from the Unix/Linux terminal input (at the shell prompt)

cat > input.txt

and paste the text/code into it, then hit Control-D to close the input.