簡體   English   中英

Bash腳本在文件結束后循環

[英]Bash script looping after end of file

我的bash腳本要求用戶提供其名字,姓氏,地址和電話號碼,並將用戶輸入的此信息寫入格式為“ firstname.lastname”的文件中; 但是我想重復多次(我實際上想做類似do..while循環的事情,該循環至少運行一次,並詢問用戶是否要繼續創建帳戶,但我看不出有做。 ..而bash似乎)。 因此,當我在終端中執行此bash腳本時,它將詢問要創建多少個帳戶,我將提供所有輸入,但是該輸入只能運行一次。 我究竟做錯了什么? 這是我的劇本。

#!bin/bash

echo "How many accounts are you creating?"
read num
echo "Enter first name" 
read fName
echo "Enter last name"
read lName
echo "Enter address"
read add
echo "Enter phone number"
read phn
echo "Enter gender m for male and f for female"
read gender

if [ "$gender" == "m" ]
    then
        sex="male"
elif [ "$gender" == "f" ]
    then 
        sex="female"
else 
    echo"Invalid gender. Restart the script and enter a valid gender"
    exit 0
fi
for (( i = 0; i<=num; i++))
do
    cat > $fName.$lName <<-EOF
        Full Name: $fName $lName
        Address: $add
        Phone number: $phn
        Gender: $gender
    EOF
done

將您的輸入代碼放入循環內,或將該代碼包裝在函數中,然后在循環內調用該函數。

zerobandwidth的答案是正確的,但作為替代答案,實際上很容易完成您最初想要做的事情:

while true; do
    # Your account creation code goes here

    echo "Create another account (y/n)?"
    read another
    if [ "$another" != y ]; then
        break
    fi
done

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM