Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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.
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.
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.
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
Distribution: openSuSE Tumbleweed-KDE, Mint 21, MX-21, Manjaro
Posts: 4,629
Rep:
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.