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를 누르면 응용 프로그램이 원하는 대로 종료됩니다.

관련 정보