LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Looping thru a list in a file (https://www.linuxquestions.org/questions/linux-newbie-8/looping-thru-a-list-in-a-file-182518/)

BruceC 05-17-2004 01:06 PM

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

Muzzy 05-17-2004 01:55 PM

Change $FILE to `cat $FILE`
Code:

for computername in `cat $FILE`
Notice the backticks.

homey 05-17-2004 01:59 PM

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

BruceC 05-17-2004 02:20 PM

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.

homey 05-17-2004 02:51 PM

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

BruceC 05-17-2004 03:58 PM

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.

homey 05-17-2004 04:52 PM

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]#

BruceC 05-18-2004 08:23 AM

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.

homey 05-18-2004 08:29 AM

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.

BruceC 05-18-2004 09:27 AM

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.

BruceC 05-18-2004 12:23 PM

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

JZL240I-U 05-19-2004 03:22 AM

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? ;)

BruceC 05-19-2004 08:51 AM

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.


All times are GMT -5. The time now is 01:40 AM.