【问题标题】:bypass Yes/No in Shell Script在 Shell 脚本中绕过 Yes/No
【发布时间】:2014-08-04 07:17:04
【问题描述】:

当我在服务器上运行两次时,它会多次询问 yes/no 几乎 100 次。我厌倦了每次都输入yes。有什么方法可以运行该脚本,只需将 yes 作为默认选项。假设以下是我的脚本!仅供参考,我无法编辑我的脚本。 只是我可以使用./ittp-update.sh 运行它

  #!/bin/bash
  echo "Do you need to install the necessary compiling tools?"
  select yn in "Yes" "No"; do
case $yn in
    Yes ) sudo apt-get install tools; break;;
    No ) <Here I want to skip to the next step. I originally left it 
          blank and removed the "done" command (after esac command) 
          to do this, but if I choose yes, it skips to the end 
          (where the next "done" command is and ends the script>
esac

echo "Do you need to eidt your configuration?"
select ny in "No" "Yes"; do
   case $ny in
    No ) <what do I put here to skip to the next tag 
         (labeled compile for example purposes); break;;
    Yes ) 
esac
 echo "You have 3 options with how you can edit you config file."

....

【问题讨论】:

  • 要扩展@Ken 的评论:yes | ./ittp-update.sh 将回答yes 的所有 个问题(通过输入yes 后跟一个新行一遍又一遍)。跨度>

标签: bash shell ubuntu


【解决方案1】:

如果您只需要对所有内容回答“是”,则可以使用

yes Yes | ./ittp-update.sh

这是如何工作的:

  1. yes 程序会打印出你给它的字符串(如果你什么都不给它,则打印“y”,因为这是在 *nix 程序中给出肯定答案的默认方式)重复 em> 在标准输出上,直到收到 SIGPIPE。
  2. 管道 (|) 将前面命令 (yes) 的标准输出连接到以下命令 (./ittp-update.sh) 的标准输入。
  3. 当./ittp-update.sh 完成时,shell 会自动将SIGPIPE 发送到通过管道连接到它的任何命令(在这种情况下仅yes)。
  4. 收到SIGPIPE 后,yes 退出。

更多信息请参见man yes。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-07
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 2015-08-27
    相关资源
    最近更新 更多