LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 05-17-2004, 02:06 PM   #1
BruceC
Member
 
Registered: Jul 2003
Posts: 34

Rep: Reputation: 15
Looping thru a list in a file


Pardon my terrible newbieness.
I have a simple procedure I need my script to perform. Loop thru a list of computer names in a file and perform a task on each. My windows script would look something like "for /F %A in (file.txt) Do" I need my bash script to parse the[list] as a file of a list. I tried set -- $variablename.

#list is a file with a list of computer names.
FILE=/root/list
for computername in $FILE
do
set -- $computername
echo $computername
done
exit 0
 
Old 05-17-2004, 02:55 PM   #2
Muzzy
Member
 
Registered: Mar 2004
Location: Denmark
Distribution: Gentoo, Slackware
Posts: 333

Rep: Reputation: 30
Change $FILE to `cat $FILE`
Code:
for computername in `cat $FILE`
Notice the backticks.
 
Old 05-17-2004, 02:59 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Maybe something like this....

#list.txt is a file with a list of computer names.
file="/root/list.txt"
cat ${file} | \
while read NAME
do
set --$NAME
echo "$NAME"

done
 
Old 05-17-2004, 03:20 PM   #4
BruceC
Member
 
Registered: Jul 2003
Posts: 34

Original Poster
Rep: Reputation: 15
Muzzy's didn't work but Homey's did except for the | \ part on line 3. It would just exit at that point. What does the | \ do?

It gives the output I need but with an error, unexpected end of file at the exit 0 line.
 
Old 05-17-2004, 03:51 PM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
the | is a pipe. More or less for connecting commands. In this case, "cat..." opens the file and "while read" reads the file one line at a time.
When I had | \ I just used the \ to continue the command on the next line so it looks neater.
Try it without the exit 0
 
Old 05-17-2004, 04:58 PM   #6
BruceC
Member
 
Registered: Jul 2003
Posts: 34

Original Poster
Rep: Reputation: 15
The result I am getting is the cat ${file} part, this is what is echoing 'file' to the screen. It displays the contents of 'file' then stops/hangs at the while read NAME line. I put some echos in between lines to check.
 
Old 05-17-2004, 05:52 PM   #7
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Here is the result when I just cat the file...

[root@mudd home]# cat /home/list.txt
fred
sam
joe
ted
sue
sally
[root@mudd home]#

Here is the script...
Code:
#!/bin/bash

#list.txt is a file with a list of computer names.
file="/home/list.txt"
cat ${file} | \
while read NAME
do
set -- $NAME
echo "$NAME"

done
And here is the output when I run that script.....

[root@mudd home]# sh test
fred
sam
joe
ted
sue
sally
[root@mudd home]#
 
Old 05-18-2004, 09:23 AM   #8
BruceC
Member
 
Registered: Jul 2003
Posts: 34

Original Poster
Rep: Reputation: 15
This is the result I get also, but it's not the result I want. I don't want to display the contents of the file, I want to perform an operation on each line in the file. I don't think the while statement is even being executed.
 
Old 05-18-2004, 09:29 AM   #9
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Ok, I guess I'm not sure just what you are trying to do with this..

set -- $NAME
echo "$NAME"


The echo part will list each name as it is read.
 
Old 05-18-2004, 10:27 AM   #10
BruceC
Member
 
Registered: Jul 2003
Posts: 34

Original Poster
Rep: Reputation: 15
The echo "$NAME" is just a test while I get the looping to work. I will replace it with ssh name cp this or ssh name runscript.sh ...
Add a line after this echo like echo **** to verify that it is being executed. For my purposes I don't need the cat to echo the contents, I just need a function to parse a file.
 
Old 05-18-2004, 01:23 PM   #11
BruceC
Member
 
Registered: Jul 2003
Posts: 34

Original Poster
Rep: Reputation: 15
I found a solution:

FILE="list" #Assign FILE to file list in current dir

while read NAME #can't see how while read gets FILE but it does
do
echo $FILE
ssh $NAME [command...]
ssh $NAME [command...]
done < list #exit reading file
 
Old 05-19-2004, 04:22 AM   #12
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE 13.1 / 12.3_64-KDE, Ubuntu 14.04, Fedora 20, Mint 17, Chakra
Posts: 3,689

Rep: Reputation: Disabled
Quote:
Originally posted by BruceC
... while read NAME #can't see how while read gets FILE but it does
do ...
done < list #exit reading file
My guess: "do" and "done" is effectively a pair of brackets for the while statlement, and you attach "list" to that via the "<" as input stream in the last line and thus "FILE" gets assigned ... any guru with a clearer explanation?

Last edited by JZL240I-U; 05-19-2004 at 04:24 AM.
 
Old 05-19-2004, 09:51 AM   #13
BruceC
Member
 
Registered: Jul 2003
Posts: 34

Original Poster
Rep: Reputation: 15
I've cleaned up a few things. The FILE= statement is not needed, I took it out.

while read NAME #NAME here can be anything, used in the loop as the var.
do
ssh $NAME [command...]
ssh $NAME [command...]
done < list #exit reading file. This is where the file is found. It defaults
#to pwd, the path can be used.
 
  


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
konqueror problems (looping dirs and file opening) Cyber Maid Linux - Software 4 12-17-2005 08:57 PM
list file name ust Linux - Software 4 12-07-2005 03:58 AM
Help!How can I get the file name list in C program? wuzhong Programming 6 09-22-2004 12:56 PM
C++ get file list Wynd Programming 2 06-09-2003 06:33 AM
can not list tar file juno Linux - Software 2 09-23-2002 03:47 AM


All times are GMT -5. The time now is 07:42 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration