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
Code:
#!/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 {
valsplit.sh: line 7: syntax error near unexpected token `$'{\r''
'alsplit.sh: line 7: `function checkImmediate {
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.
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:
Code:
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?
Hi dears
i have text file like this:
INPUT.txt
001_1_173 j nuh ]az
001_1_174 j ]esma. nuh ]/.xori
.
.
. and have another text
like this
TABLE.txt
j j
nuh word1... (6 Replies)
Hi, I am complete new to C programming and shell scripting. I just wrote a simple C code to calculate integral using trapezoid rule. I am prompting user to pass me No. of equally spaced points , N , upper and lower limit. My code looks as follows so far:
#include<stdio.h>
#include<string.h>... (2 Replies)
Dear experts,
I am new to linux programming. I have a shell script which i should run it on all my samples.
I only define input and out put for this script. The inputs are 3 numbers(coordination numbers) which are available in a series of text file.
Since i have a lots of samples, it takes a... (5 Replies)
Hi ...programmers...
I need a shell script to perform some specific task..
my txt file looks like this
netcdf new {
dimensions:
XAX1_11 = 11 ;
variables:
double XAX1_11(XAX1_11) ;
XAX1_11:point_spacing = "even" ;
XAX1_11:axis = "X" ;
float DEPTH(XAX1_11) ;... (19 Replies)
Hi Frnds...
I have an input file name.txt and another file named as source.. name.txt is having only one column and source is having around 25 columns...i need to read from name.txt line by line and search it in source file and then save the result in results file..
I have a rough idea about the... (15 Replies)
Hi all,
Help needed urgently.
I am currently writing a shellscript to read data/record from a flat file (.txt) file, and import/upload the data to oracle database. The script is working fine, but it takes too long time (for 18000 records, it takes around 90 mins).
I guess it takes so long... (1 Reply)
The scope of the shell/perl script is to read the input text file. Validate the expiry date of each certificate and send the mail to the user. The user takes action to add the new certificate to the storage file and user owns the responsibility to update the input text file with the new certificate... (5 Replies)
I need to read data from a file called "test.csv" through shell script where the file contains values like name,price,descriptor etc. There are rows where descriptor (& in some rows name) are written as string & other characters like "car_+" OR "bike*" etc where it should contains strings like... (3 Replies)
I am having one text file and i need to read that data from my shell script.
I will expain you the scenario:
Script look like:
For name type 1:
For age type 2:
For Salary type3:
echo "Enter the input:"
read the data
if input is 1 then go to the Text file and print the... (2 Replies)