如何在 while 循环内接受用户输入

如何在 while 循环内接受用户输入

我对 shell 脚本编写非常陌生,并尝试创建一个循环,该循环将继续提示用户输入,直到他们确认输入正确。我试图为此使用 while 循环,但出现错误:

joyentmac2252:scripts jessebutryn$ ./bashprofilemod.sh
Please enter your Joyent Public Cloud (JPC) username and press [ENTER]: 
jesse.butryn
The username you entered is: jesse.butryn Is this correct? y/n :n
./bashprofilemod.sh: line 13: syntax error near unexpected token `fi'
./bashprofilemod.sh: line 13: `  fi'

到目前为止的完整脚本如下,很抱歉,如果它真的很脏,但我仍在掌握窍门。到目前为止它还不完整,因为我正在尝试让 while 循环工作,然后再进行下一步。

#!/bin/sh
#Prompts user for their Joyent Cloud username (usually first.last)
read -p "Please enter your Joyent Public Cloud (JPC) username and press [ENTER]: " username
#Confirms user input
read -p "The username you entered is: $username Is this correct? y/n :" uconf
#
while [ $uconf = "N" -o $uconf = "n" ]
do
  read -p "Please enter your Joyent Public Cloud (JPC) username and press [ENTER]: " username
  read -p "The username you entered is: $username Is this correct? y/n :" uconf
  if [ $uconf = "Y" -o $uconf = "y" ]
    echo "Thanks your username will be entered as: $username$."
  fi
done

感谢任何意见

答案1

您还没有使用then过这两个if语句。

if [ $uconf = "Y" -o $uconf = "y" ];then
    echo "Thanks your username will be entered as: ${txtcyn}$username${txtrst}."
 fi



 if [ $kconf = "Y" -o $kconf = "y" ];then 
     echo "Thanks your ssh public key will be entered as: ${txtcyn}$HOME/.ssh/$sshkey${txtrst}."
 fi

相关内容