Linux 如何在 Bash 脚本中回答是

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16616120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:57:29  来源:igfitidea点击:

How to answer yes in Bash Script

linuxbash

提问by Vor

Have a quick question,

有一个快速的问题,

imagine that I have this code:

想象一下,我有这个代码:

mkdir -p $INSTALLDIR
sudo apt-get install -y git clojure leiningen
git clone git://github.com/maltoe/storm-install.git
./storm-install/storm_install.sh all `hostname` $INSTALLDIR

And this script will ask, do you want to install additional packages, and I want to say yes, How to do this automatically?

这个脚本会问,你想安装额外的包,我想说yes,如何自动执行此操作?

Or may be there is a way to kinda answer yes to any question by default?

或者可能有一种方法可以在默认情况下对任何问题回答“是”?

采纳答案by jim mcnamara

assume storm asks the question - use a here document - example:

假设风暴问这个问题 - 使用这里的文档 - 示例:

mkdir -p $INSTALLDIR
sudo apt-get install -y git clojure leiningen
git clone git://github.com/maltoe/storm-install.git
./storm-install/storm_install.sh all `hostname` $INSTALLDIR  <<-EOF
yes
EOF

The EOF can be any nonsense characters the shell will not interpret.

EOF 可以是 shell 不会解释的任何无意义字符。

回答by troelskn

With the command appropriately named yes

使用适当命名的命令 yes

回答by Slava Semushin

apthas also --force-yesoption which may be helpful:

apt还有--force-yes可能有帮助的选项:

   --force-yes
       Force yes; This is a dangerous option that will cause apt to
       continue without prompting if it is doing something potentially
       harmful. It should not be used except in very special situations.
       Using force-yes can potentially destroy your system! Configuration
       Item: APT::Get::force-yes.

回答by user5068816

I not sure but i advice try:

我不确定,但我建议尝试:

echo yes | ./storm-install/storm_install.sh all `hostname` $INSTALLDIR

回答by Jimmy

If you are running a script, try this:

如果您正在运行脚本,请尝试以下操作:

yes "yes" | bash script.sh 

回答by Happy

I have a script needs to pass "yes" for couple of times and at last we need to pass "version number" in order to deploy in to the nexus. Any suggestions?

我有一个脚本需要多次传递“是”,最后我们需要传递“版本号”才能部署到关系中。有什么建议?

回答by Sriram

( echo yes ; echo no; echo yes ) | script.sh