LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   linux scripting help needed read from file line by line exc commands each line read (http://www.linuxquestions.org/questions/programming-9/linux-scripting-help-needed-read-from-file-line-by-line-exc-commands-each-line-read-128912/)

atokad 12-25-2003 07:24 PM

linux scripting help needed read from file line by line exc commands each line read
 
I need a script however probably pretty simple preferably bash

synopsis

file reads

a
b
c
d

I need a script to read the file line by line at each line useing the content of that line as a variable to be used in a command.example

script reads line 1 sees the a then uses the a as a variable in a command

then reads the next line and repeats itself

hope that explains it easy enough

help is much appreciated

thank you

atokad

fr0zen 12-25-2003 09:46 PM

First you want to read in the data from the file. This can be accomplished in several ways, the easiest is
Code:

FILE=`cat filename`
Then, you want to echo this file data out, and look at each line. For each line, simply execute the line as though it were a command:

Code:

echo "$FILE" | \    # notice we PIPE the output data to the next line (denoted by \)
while read CMD; do  # CMD is the line, this could be any variable name
  $CMD            # just execute it
done


atokad 12-26-2003 09:24 AM

well i figured out that my script i was useing was working however not correctly

this is the script

if [ -f /file ]; then
while read FILE; do
command $FILE
done < /file
fi

it works but if i place these lines in the file

1
2
3
4
5
6
7
8

it will read and work all the way to the last line the loop exits before
it read 8.

any ideas on why this happens

atokad

cfaj 12-26-2003 08:22 PM

Do you have a linefeed after the last line?

atokad 12-26-2003 10:24 PM

what are your referring to as a line feed
the

< /file

reads the file into the while command

elaborate i am firewall guru but not really a great scriptor

atokad


All times are GMT -5. The time now is 03:09 AM.