绕过 Shell 脚本中的 Yes/No 吗?

4

有一个shell脚本,每次我在服务器上运行两次时,它会多次询问是否选择“是/否”,几乎要100次。我已经厌倦了每次都输入“是”。有没有办法运行该脚本时将“是”作为默认选项?以下是我的脚本!请注意,我不能编辑我的脚本。只能使用./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."

....


4
是的!http://linux.die.net/man/1/yes - Ken
进一步解释@Ken的评论:yes | ./ittp-update.sh将回答所有问题(通过连续输入yes和换行符)。 - Biffen
1个回答

11
如果您只需要对所有问题回答“是”,您可以使用:
yes Yes | ./ittp-update.sh

这是如何工作的:

  1. 程序yes将你提供的字符串(如果你没有给出任何内容,则默认为"y",因为这是在*nix程序中表示肯定答复的方式)重复打印到标准输出,直到它接收到SIGPIPE信号。
  2. 管道符(|)连接前一个命令(yes)的标准输出和后一个命令(./ittp-update.sh)的标准输入。
  3. ./ittp-update.sh完成时,shell会自动向通过管道连接的任何命令发送SIGPIPE信号(在这种情况下只有yes命令)。
  4. 一旦收到SIGPIPE信号,yes将退出。

有关更多信息,请参阅man yes


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接