Bash脚本-当用户输入为“时退出While循环”;“完成”;

Bash脚本-当用户输入为“时退出While循环”;“完成”;,bash,Bash,当用户输入检测到“完成”时,如何退出bash中的while循环 其目的是让用户输入餐厅名称,当他们键入done时,while循环转义,它在数组中随机吐出一个餐厅?从现在起,它只在CTRL+D时转义,但这也会关闭终端 附言:我和bash只玩了大约2个小时 echo 'Where are you having trouble deciding? Type done to finish' while read restaurants do rest_array=("${rest_array[@]

当用户输入检测到“完成”时,如何退出bash中的while循环

其目的是让用户输入餐厅名称,当他们键入done时,while循环转义,它在数组中随机吐出一个餐厅?从现在起,它只在CTRL+D时转义,但这也会关闭终端

附言:我和bash只玩了大约2个小时

echo 'Where are you having trouble deciding? Type done to finish'

while read restaurants
do
  rest_array=("${rest_array[@]}" $restaurants)
done


echo Eat at ${rest_array[$rand]}

使用break退出bash中的循环

echo 'Where are you having trouble deciding? Type done to finish'

while read restaurants
do
  if [[ "$restaurants" == "done" ]]; then
     break
  fi
  rest_array+=("$restaurants")
done


echo Eat at ${rest_array[$rand]}

使用
break
命令退出任何循环。
rest\u array+=(“$restary”)
是一种更简单的附加到数组的方法。我尝试了这种方法,但它完全关闭了脚本(使用WIN10)您使用的是什么类型的shell?使用bash,文件名randomsrest。真奇怪。你能解释一下“它完全脱离了脚本”吗?当然。当我打开脚本(双击脚本文件)时,我输入相关文本,一旦我输入done,然后输入enter,bash窗口就会关闭,就像我在窗口外键入CTRL+D或X一样。我希望我描述得恰当