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!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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.
for host in `cat $file`;
process=`ssh root@$host "ps -ef | grep sop | grep -v grep | wc -l"`
echo "number of process = $process"
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.
All 3 examples do the same thing but have different issues depending on your requirement and the data:
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
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
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