【问题标题】:Linux Bash function conditional execution user inputLinux Bash 函数条件执行用户输入
【发布时间】:2017-08-22 01:47:51
【问题描述】:

我正在尝试读取用户输入,例如是和否,并执行一个运行良好的函数,但它在执行一次后退出脚本。

当用户输入是或Y键时,我想编写脚本来执行所述功能,然后重复上一步。

这里是脚本。

redosmartcheck () {
read -n1 -p "Do you want to Hot Swap the Hard Disks and reperform the smart test? [y,n]" doit
case $doit in
  y|Y) smartcheck;;
  n|N) echo "continuing to the next stage to wipe all hard disk drives" ;;
  *) echo dont know ;;
esac
}
redosmartcheck
exit 0

如果用户按下 Y 键,脚本执行 smartcheck 功能并退出脚本。我怎样才能让它重复 redosmartcheck 功能而不是退出?

提前致谢

【问题讨论】:

    标签: linux bash function conditional


    【解决方案1】:

    我建议插入while:

    redosmartcheck () {
      while [[ $doit == "" || $doit =~ y|Y ]]; do
        read -n1 -p "Do you want to Hot Swap the Hard Disks and reperform the smart test? [y,n]" doit
        case $doit in
          y|Y) smartcheck;;
          n|N) echo "continuing to the next stage to wipe all hard disk drives" ;;
          *) echo "dont know";;
        esac
      done
    }
    
    redosmartcheck
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 2017-12-14
      • 2019-10-02
      • 2014-10-21
      • 2020-03-09
      • 2019-11-03
      相关资源
      最近更新 更多