Скрипт Bash не завершается при нажатии ctrl d

Скрипт Bash не завершается при нажатии ctrl d

Я пишу простой скрипт bash, и согласно моим спецификациям задания, он должен завершаться по ctrl-d. Однако он этого не делает, он просто прекращает чтение текущего ввода и начинает читать следующий ввод. Как это исправить? Вот мой скрипт:

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

решение1

Вы можете проверить код выхода из readкоманды.

if [[ $? != 0 ]]; then
    echo "Exiting"
    exit 1
fi

решение2

Вот как мне удалось добиться желаемого поведения:

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

Теперь, когда я нажимаю control-d, приложение закрывается, как и ожидалось.

Связанный контент