1847221 Members
3294 Online
110263 Solutions
New Discussion

Re: while or for loop

 
Shabu Khan-2
Frequent Advisor

while or for loop


I am so embarrased to ask this question after writing thousands of shell scripts ... but I'll ask anyways :)

My input file looks like this:
server1:user1 user2 user3
server2:user1 user2
server3:user1 user3
.......

I am reading this way:
SAVE_IFS="${IFS}"
IFS=":"

while read HOST USERS; do
do something ....
done
IFS="${SAVE_IFS}"

It just exits after the first read and wouldn't move on, I tried cat'ng the file out and piping it to while read but still the same issue ...

I want the variable HOST to get the server name and the USERS to get the rest of the line ...

Any thoughts?

What is going on?
11 REPLIES 11
Sandman!
Honored Contributor

Re: while or for loop

Not sure what's wrong as it runs w/o problems on my machine. Are you doing a remsh to a remote server by any chance? Run it in debug mode by inserting the line below at the very top of your script and post its output i.e.

#!/usr/bin/sh -x
<...rest of script...>
Shabu Khan-2
Frequent Advisor

Re: while or for loop


Yes, I am ssh'ng in (actually ttlg'ng in, this is opsware's version of ssh works the same way as ssh) ...

I ran it in debug mode earlier, just exits after the first read, nothing useful ...
Michael Mike Reaser
Valued Contributor

Re: while or for loop

You need to export the value of IFS to the shell. That is, your second line should be

export IFS=":"

Otherwise, nothing later "knows" about the value, since it was never exported into the shell's environment. I *think*...
There's no place like 127.0.0.1

HP-Server-Literate since 1979
Shabu Khan-2
Frequent Advisor

Re: while or for loop

Thanks for the responses.

Nah, export is not going to help ... I tried to echo '${HOST} - ${USERS}' earlier and it worked fine - went through the list and echo'ed it just like I expected, its just when I put in the ssh/ttlg line that it exits after the first line ... may be with ssh there are options like BatchMode etc that might work, but I am really not sure if 'ttlg' has any ...

Anyways, any other thoughts/suggestions are welcome.

Thanks,
Shabu
Dennis Handly
Acclaimed Contributor

Re: while or for loop

>Mike: You need to export the value of IFS to the shell.

No, exporting IFS is near useless. As soon as you create a new shell, it is reset.

>Mike: Otherwise, nothing later "knows" about the value,

Nothing later needs to know. Only the current shell and that doesn't need to have it exported.

>it's just when I put in the ssh/ttlg line that it exits after the first line

Can you show what you mean?
When I use: ssh machine script-with-read
this works fine.

Are you using "ssh $HOST $USER" within your while loop? If so, you need to use:
ssh -n $HOST $USER

Otherwise ssh eats your input from /tmp/myinput.
Bob E Campbell
Honored Contributor

Re: while or for loop

Over the years I have fallen out of love of tweaking IFS for such a simple case. I would handle this case through parameter expansions:

while read LINE
do
HOST=${LINE%%:*}
USERS=${LINE#*:}

... do something

done

Yes, you can make IFS work. It is just that so often what you think you told the shell is not what you said.
john korterman
Honored Contributor

Re: while or for loop

Hi,

have you specified which shell interpreter to use in the first line?
If not, try
#!/usr/bin/sh

regards,
John K.
it would be nice if you always got a second chance
Peter Nikitka
Honored Contributor

Re: while or for loop

Hi,

calling a ssh in your loop will eat al remaining input, because ssh read stdin as well.
A usage of 'ssh -n' will prevent that command doing so.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Arturo Galbiati
Esteemed Contributor

Re: while or for loop

Hi Shabu,
my suggetion is to load the HOSt USER into an array and after perform another loop reading data for array. probbaly the sheel used withi the first loop read the rest of teh file as STDIN.
Just my .02$
Art
john korterman
Honored Contributor

Re: while or for loop

Hi again,

if you change
do something
in your script to
echo "hello $HOST $USERS"

does the script then produce only a single line of output? If yes, it must be the "do something" that causes the exit.

regards,
John K.
it would be nice if you always got a second chance
Dennis Handly
Acclaimed Contributor

Re: while or for loop

Using tusc you can see the read in ssh that eats up the data. That's after the shell goes out of its way to put the file pointer back to the next unread line.