linux bash shell script reading information from a file

Associate
Joined
5 Dec 2002
Posts
141
Please can someone help.
I have a job to do for work which involves reading a list of numbers from a text file and using each entry as an input to an a program as part of a script. The text file is laid out like
0012345
0023456
0034567
......

I need to read each entry and apply it like this:

program $entry > outputfile

I then need it to iterate through the full list of numbers.

Also I need to append the output to the file instead of creating an output file for each $entry. I'm not too bothered about this part as I could get away with individual files but it would help.

I appreciate any help you can give.

p.s. If you need further info to help please let me know.
 
I tried the cat command but when I just echo the results it looks as though it is trying treated the cat command as just text. I get the following on screen:

cat
darren.txt

If I remove the quotes then it does read the file but just by echoing the results in screen it tries to treat the numbers as executables:

.darren.txt: line 1: 0012345: command not found
.darren.txt: line 2: 0023456: command not found
..........

It's partly right but at the moment I want it to just echo the numbers onto the screen.
 
You want to process each line in the input file so you need a loop, something like this:

Code:
while read myline
do
  echo $myline
done < inputfile

Have a read here for more information.

As for appending the results in the file use >> to redirect and append to your output file.
 
That works pretty much how I want it but is there a way to store each line into an array element. I've tried something like:

index = 0
while read myfile
do
echo $myfile
arr[$index] = $myfile
let "index = $index +1"
done < darren.txt

but it keeps saying command not found in the arr[$index]=$myfile line.
Sorry about this but I am a bit thick when it comes to bash shell/linux but unfortunately I need it for work.
 
Back
Top Bottom