
Ich schreibe ein einfaches Bash-Skript und gemäß meiner Aufgabenspezifikation soll es mit Strg-D beendet werden. Das tut es jedoch nicht, sondern es stoppt einfach das Lesen der aktuellen Eingabe und beginnt mit dem Lesen der nächsten Eingabe. Wie kann ich das beheben? Hier ist mein Skript:
while true ; do
echo Please enter your full name:
read fullName
echo Please enter your street addres:
read streetAddress
echo Please enter your zip code, city, and state in that order:
read zip city state
echo $fullName > Name.txt
echo "$streetAddress $city $state $zip" >> Locations.txt
echo $fullName >> "$zip".txt
echo $streetAddress >> "$zip".txt
echo "$city $state $zip" >> "$zip".txt
echo '' >> "$zip".txt
done
Antwort1
Sie können den Exitcode des read
Befehls überprüfen.
if [[ $? != 0 ]]; then
echo "Exiting"
exit 1
fi
Antwort2
So habe ich das gewünschte Verhalten erreicht:
notFinished=true
while $notFinished ; do
echo Please enter your full name:
while read fullName ; do
echo Please enter your street addres:
read streetAddress
echo Please enter your zip code, city, and state in that order:
read zip city state
echo $fullName > Name.txt
echo "$streetAddress $city $state $zip" >> Locations.txt
echo $fullName >> "$zip".txt
echo $streetAddress >> "$zip".txt
echo "$city $state $zip" >> "$zip".txt
echo '' >> "$zip".txt
continue 2
done
notFinished=false
done
Wenn ich jetzt Strg-D drücke, wird die Anwendung wie gewünscht beendet.