LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-18-2012, 02:02 AM   #1
rmugunthan
Member
 
Registered: Jan 2009
Location: Coimbatore, India
Distribution: rhel
Posts: 49

Rep: Reputation: 3
shell script read input from file


Hi,

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


Regards,
Mugunthan R
 
Old 06-18-2012, 02:49 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Please use tags for code examples---makes things easier to read....

I think the common way of doing this is (using your script example):
Code:
file=/opt/linux_host
while read host; do
   echo "$host"
   process=`ssh root@$host "ps -ef | grep sop | grep -v grep | wc -l"`
   echo "number of process = $process"
done < $file

Code:
cat $file | while read host ; do
I maybe think this does not work because there are effectively two read commands?
 
Old 06-18-2012, 03:04 AM   #3
rmugunthan
Member
 
Registered: Jan 2009
Location: Coimbatore, India
Distribution: rhel
Posts: 49

Original Poster
Rep: Reputation: 3
Hi,

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.

Regards,
mugunthan R
 
Old 06-18-2012, 03:20 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
Old 06-18-2012, 03:28 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,983

Rep: Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182
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

Hope that helps.
 
Old 06-18-2012, 04:17 AM   #6
rmugunthan
Member
 
Registered: Jan 2009
Location: Coimbatore, India
Distribution: rhel
Posts: 49

Original Poster
Rep: Reputation: 3
Hi,

Thanks for the reply. I have only ipaddress in the "/opt/linux_host" file. I am working with SLES OS.

While executing the below script i get output from the first serer only and the loop disconnecting automatically.

file=/opt/linux_host
while read host; do
echo "$host"
process=`ssh root@$host "ps -ef | grep sop | grep -v grep | wc -l"`
echo "number of process = $process"
done < $file

But when i use the below one, i get the output from all the servers listed in the "/opt/linux_host" file.

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

Regards,
Mugunthan R
 
Old 06-18-2012, 04:22 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Again, please post an actual sample of the contents of that file

AND--what happens if you delete (or comment out) the line beginning with process? (I re-ran my test using a file with IP addresses and it worked fine)

Last edited by pixellany; 06-18-2012 at 04:47 AM.
 
Old 06-18-2012, 04:50 AM   #8
rmugunthan
Member
 
Registered: Jan 2009
Location: Coimbatore, India
Distribution: rhel
Posts: 49

Original Poster
Rep: Reputation: 3
Sorry, That file contains only ipaddress. I pasted some of them

10.47.37.44
10.47.37.45
10.47.37.46
10.47.37.47
10.47.37.48
10.47.37.49
10.47.37.65
10.47.37.66
10.47.37.67
.
.
.
.

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.
 
Old 06-18-2012, 05:27 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Lazy way out: Use the format that works.....

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?
 
Old 06-18-2012, 07:45 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,309

Rep: Reputation: 2744Reputation: 2744Reputation: 2744Reputation: 2744Reputation: 2744Reputation: 2744Reputation: 2744Reputation: 2744Reputation: 2744Reputation: 2744Reputation: 2744
Well, you could add
Code:
set -xv
at the top of your prog; that will show you exactly what the parser sees/does.
 
  


Reply

Tags
linux, script, shell


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
using an shell script how to change a value in a file according to the user input badrinath_tcs Linux - Software 6 09-28-2009 07:29 PM
shell script having multiple grep statements-I want input file to be read only once mukta9003 Linux - Newbie 4 08-27-2008 12:58 AM
[Shell] Read a File from script yussef Programming 4 08-19-2008 04:26 AM
shell script to read input from csv ip addresses? kr0m3 Programming 3 07-21-2007 08:51 AM
help on shell script as a wrapper to launch default programs according to input file daveiro Programming 2 11-24-2005 04:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:12 AM.

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
Open Source Consulting | Domain Registration