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.
Someone, could you please tell me the difference between the below commands.
for host in `cat $file`; do
cat $file | while read host ; do
I thought both the commands are doing the same thing. When i used "cat $file | while read host ; do" in my script the loop disconnect automatically. But "for host in `cat $file`; do" working fine for me.
My script for your reference.
file=/opt/linux_host
for host in `cat $file`;
do
echo "$host"
process=`ssh root@$host "ps -ef | grep sop | grep -v grep | wc -l"`
echo "number of process = $process"
done
I tried, like you mentioned but that also not helps.
in "/opt/linux_host" file i have list of linux server ipaddress. i need check the particular process status in all the servers. So i write the script like above. If i execute the script like you mentioned also, it ended by giving result from only one server.
But if i modify that to "for host in `cat $file`; do", it is working fine for me. I want to know how it is get differs from the while loop.
What is the format of the file you are reading from?
Here's a test I just did (I created a file called "list"):
Code:
[mherring@(none) play]$ more list
a
b
c
d
[mherring@(none) play]$ while read item; do echo $item; done < list
a
b
c
d
[mherring@(none) play]$ for item in `cat list`; do echo $item; done
a
b
c
d
All 3 examples do the same thing but have different issues depending on your requirement and the data:
Code:
for host in `cat $file`; do
This will work ok as long as the data does not contain any whitespace or it will be broken up based on the values stored in IFS
Code:
cat $file | while read host; do
This will work ok unless you require any information to be available outside the loop, eg. if you place a counter in the loop to be incremented for each host and then display the count after the loop
it will still be zero as the pipe creates a subshell and any changes to the counter are lost once the shell closes
Code:
while read host; do <your_stuff>; done<$file
This is potentially the safest of the 3 as it is not affected by either of the above issues
I would also be quoting your variables if there are any unusual characters in them
For normal operations it is working fine. like only echo the server ipaddress. But i am using password less authentication to get information from the remote hosts.
Regards,
Mugunthan R
Last edited by rmugunthan; 06-18-2012 at 04:56 AM.
Otherwise, in the format that does NOT work, I wonder if something is getting into the data stream besides the contents of file? (I'm trying to think of how to test this)
In both cases, do you get the correct number of processes the first time thru the loop?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.