gwillen: (Default)
[personal profile] gwillen
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:
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.)
This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

Profile

gwillen: (Default)
gwillen

April 2012

S M T W T F S
1234567
891011121314
15161718192021
22232425262728
29 30     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 19th, 2025 06:31 pm
Powered by Dreamwidth Studios