BASH,如果用户响应为否,如何重复命令?

时间:2016-05-25 02:16:39

标签: bash

以下是我所说的如果选择No,如何让脚本跳回第一个echo语句,谢谢?

echo "Please enter some input: " 
read input_variable 
echo "You entered: $input_variable" 

read -r -p "Is this Correct? [y/N] " response 
case $response in 
[yY][eE][sS]|[yY] 
do_something 
;; 
*) 

{返回第一行 - 回声"请输入一些输入:"}

;; 
esac

2 个答案:

答案 0 :(得分:1)

在输入确认后,仅在 循环的}循环中包裹您的提示:

while

请注意,不仅break,而且除while :; do # same as: `while true; do` - keep looping until exited with `break` echo "Please enter some input: " read -r input_variable echo "You entered: $input_variable" read -r -p "Is this Correct? [y/N] " response case $response in [yY][eE][sS]|[yY]) break ;; esac done do_something 处理程序匹配的输入之外的任何输入都将导致循环保持活动状态。

答案 1 :(得分:0)

在整个代码周围使用循环:

quit=0

while [ $quit == 0 ]; do
  echo "Please enter some input: " 
  read input_variable
  echo "You entered: $input_variable" 

  read -r -p "Is this Correct? [y/N] " response
  case $response in
  [yY])
    echo "do_something"
    quit=1
    ;;
  *)
    ;;
  esac
done