(1 Antwort)
Vor 4 Jahren geschlossen.
Ich habe zwei Skripte:
foo.sh:
#!/bin/bash
echo -e "onentwo" |
while read line; do
cat not-existing
echo hello $line
done
bar.sh:
#!/bin/bash
echo -e "onentwo" |
while read line; do
ssh [email protected] 'cat not-existing' # Here is the only difference
echo hello $line
done
Und jetzt führe ich sie aus
$ ./foo.sh
cat: not-existing: No such file or directory
hello one
cat: not-existing: No such file or directory
hello two
$ ./bar.sh
cat: not-existing: No such file or directory
hello one
Die Ausgabe von bar.sh
ist für mich überraschend. Ich würde erwarten, dass es für beide Skripte gleich ist.
Warum funktioniert die Ausgabe von foo.sh
und bar.sh
sich unterscheiden? Ist es ein Fehler oder ein Feature?
Hinweis
Das Folgende funktioniert wie erwartet, d.h. die Ausgabe davon ist die gleiche wie die Ausgabe von foo.sh
:
#!/bin/bash
for line in `echo -e "onentwo"`; do
ssh [email protected] 'cat not-existing'
echo hello $line
done
Warum?
Akzeptierte Antwort:
In bar.sh
, die two
wird von ssh
verbraucht . Im letzten Beispiel die vollständige Ausgabe von echo
wird von for
verwendet bevor es mit der Schleife beginnt.
Um ssh
zu vermeiden Ihre Daten von der Standardeingabe verschlingen, verwenden Sie ssh -n
. Dadurch wird die Standardeingabe von ssh
verknüpft mit /dev/null
statt mit der Standardeingabe des while
Schleife.
Dies wird das tun, was Sie erwarten:
#!/bin/bash
echo -e "onentwo" |
while read line; do
ssh -n [email protected] 'cat not-existing' # Here is the only difference
echo hello $line
done
Hätten Sie geschrieben
#!/bin/bash
echo -e "onentwo" |
while read line; do
ssh [email protected] 'cat'
echo hello $line
done
dann die cat
auf dem entfernten Rechner hätte two
ausgegeben da seine Standardeingabe von ssh
an ihn weitergegeben wird die es wiederum aus der Schleife und echo
bekam . Es wird two
gedruckt statt one
da die erste Eingabezeile bereits von read
verbraucht wurde .