简体   繁体   English

Bash脚本在文件结束后循环

[英]Bash script looping after end of file

My bash script asks user for their first name, last name, address and phone number and writes this information that is input by the user to a file of the format "firstname.lastname"; 我的bash脚本要求用户提供其名字,姓氏,地址和电话号码,并将用户输入的此信息写入格式为“ firstname.lastname”的文件中; however I want to repeat this a number of times (I actually wanted to do something like a do..while loop where it runs atleast one time and asks user if they want to continue creating accounts or not but I see there is no do...while for bash it seems). 但是我想重复多次(我实际上想做类似do..while循环的事情,该循环至少运行一次,并询问用户是否要继续创建帐户,但我看不出有做。 ..而bash似乎)。 So when I execute this bash script in the terminal it will ask for how many accounts to be made and I provide all the input but the it only runs one time. 因此,当我在终端中执行此bash脚本时,它将询问要创建多少个帐户,我将提供所有输入,但是该输入只能运行一次。 What am I doing wrong? 我究竟做错了什么? Here is my script. 这是我的剧本。

#!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's answer is correct, but as an alternative answer, it is in fact quite easy to do what you initially wanted to do: 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