LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-25-2003, 07:24 PM   #1
atokad
LQ Newbie
 
Registered: Dec 2003
Posts: 13

Rep: Reputation: 0
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
 
Old 12-25-2003, 09:46 PM   #2
fr0zen
Member
 
Registered: Nov 2003
Location: 127.0.0.1
Distribution: xubuntu
Posts: 217

Rep: Reputation: 30
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
 
Old 12-26-2003, 09:24 AM   #3
atokad
LQ Newbie
 
Registered: Dec 2003
Posts: 13

Original Poster
Rep: Reputation: 0
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
 
Old 12-26-2003, 08:22 PM   #4
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Do you have a linefeed after the last line?
 
Old 12-26-2003, 10:24 PM   #5
atokad
LQ Newbie
 
Registered: Dec 2003
Posts: 13

Original Poster
Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 01:42 PM
C: fread to read a file line by line until the end Blue_muppet Programming 2 09-19-2008 09:42 AM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
[C & Linux] Read line from text-file Y_Haarman Programming 5 11-08-2002 04:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration