September 18th, 2007 by depesz | Tags: | 3 comments »
Did it help? If yes - maybe you can help me?

[ wersja polska poniżej ]

english version

just found this nice brain teaser.

i have this code in bash:

echo -e "1 1\n2 2" | while read A B; do echo "[$A] [$B]"; echo "ZZZ"; done

it will print:

[1] [1]
ZZZ
[2] [2]
ZZZ

which is perfectly valid and sensible.

but if i'll change the command to:

echo -e "1 1\n2 2" | while read A B; do echo "[$A] [$B]"; ssh localhost date; echo "ZZZ"; done

i.e. i added ‘ssh localhost date' which connect to localhost over ssh, logins to my own account, and issues “date" command (can be any command), it shows only:

[1] [1]
Tue Sep 18 15:45:24 CEST 2007
ZZZ

and finishes work. (i have a password-less login in localhost, so it doesn't ask for password).

and the question is: why there is no second step of while loop?

( side note: i know the answer, it's just a riddle for you 🙂

wersja polska

trafiłem właśnie na niezłą łamigłówkę:

poniższa linijka w bashu:

echo -e "1 1\n2 2" | while read A B; do echo "[$A] [$B]"; echo "ZZZ"; done

wypisze:

[1] [1]
ZZZ
[2] [2]
ZZZ

co jest w pełni sensowe i oczekiwane.

ale jeśli zmienię ją na:

echo -e "1 1\n2 2" | while read A B; do echo "[$A] [$B]"; ssh localhost date; echo "ZZZ"; done

tzn. dodam ‘ssh localhost date', co łączy się na moje konto na localhoście i wykonuje polecenie “date" (może to być dowolne polecenie), wynikiem całości jest tylko:

[1] [1]
Tue Sep 18 15:45:24 CEST 2007
ZZZ

i to koniec (mam logowanie bezhasłowe więc nie ma prośby o hasło).

pytanie: czemu nie ma drugiego wykonania kodu w pętli while?

( oczywiście (jak przy poprzednich łamigłówkach) znam odpowiedź ).

  1. 3 comments

  2. # kura
    Sep 18, 2007

    ROT13(
    Orpnhfr ffu rngf gur vachg sebz fgqvb. Lbh pna ercynpr ’ffu ybpnyubfg qngr’ jvgu ‘png’ gb purpx jung’f tbvat ba. 🙂
    )

  3. Sep 18, 2007

    @kura:
    you’re right:) i rot13ed the solution not to spoil it for others.

  4. # Krzysztof Wilczynski
    Sep 21, 2007

    This is probably not an answer (more like a clue) 😛 Anyway, just add -n option to ssh 🙂

Sorry, comments for this post are disabled.