(no subject)
Jul. 29th, 2009 02:32 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
For Unix wizards out there: Here is a neat puzzle I just solved for a coworker. See if you can solve it. :-)
# Pipeline that prints every line in somefile:
cat somefile | while read FOO; do echo $FOO; done
Output:
# Pipeline that should do something on a remote host for every line in somefile:
cat somefile | while read FOO; do echo $FOO; ssh remotehost "<some command>"; echo "DONE."; done
Output:
Why does the loop only seem to run once? (You can try this on your own system; for an example, try using '/etc/passwd' for somefile, and 'id' for the command. My guess is you will get the same problem; if you don't I'd be interested to hear that.)
EDIT: Wow, time to first complete answer: 38 minutes. I will remember to ask elljay next time I am perplexed. I was wondering aloud to my coworker as I posted this whether anyone on my friendslist did enough shellscripting to care about this puzzle. :-)
(I think it took us about half an hour to figure it out also, because I think that's about the length of the meeting we were ignoring while we fiddled with it.)
# Pipeline that prints every line in somefile:
cat somefile | while read FOO; do echo $FOO; done
Output:
line1 line2 ...
# Pipeline that should do something on a remote host for every line in somefile:
cat somefile | while read FOO; do echo $FOO; ssh remotehost "<some command>"; echo "DONE."; done
Output:
line1 <output of ssh command> DONE. <END OF OUTPUT>
Why does the loop only seem to run once? (You can try this on your own system; for an example, try using '/etc/passwd' for somefile, and 'id' for the command. My guess is you will get the same problem; if you don't I'd be interested to hear that.)
EDIT: Wow, time to first complete answer: 38 minutes. I will remember to ask elljay next time I am perplexed. I was wondering aloud to my coworker as I posted this whether anyone on my friendslist did enough shellscripting to care about this puzzle. :-)
(I think it took us about half an hour to figure it out also, because I think that's about the length of the meeting we were ignoring while we fiddled with it.)