我如何总是用 bash 脚本对任何提示回答“否”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1962207/
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-09-17 21:30:10  来源:igfitidea点击:

How do I always answer No to any prompt with a bash script?

bash

提问by gpow

I have a program that runs and asks users certain questions. I want to automate it so that every question is responded to with No.

我有一个程序可以运行并询问用户某些问题。我想自动化它,以便每个问题都用 No 回答。

回答by Brian Campbell

yes no | <command>

Where <command>is the command you want to answer noto.

<command>您要回答的命令在哪里no

(or yes nif you actually need to just output an n)

(或者,yes n如果您实际上只需要输出一个n

The yescommand, by default, outputs a continuous stream of y, in order to answer yes to every prompt. But you can pass in any other string as the argument, in order for it to repeat that to every prompt.

yes命令,通过默认,输出的连续流y中,为了回答是每一个提示。但是您可以传入任何其他字符串作为参数,以便它在每个提示中重复该字符串。

As pointed out by "just somebody", yesisn't actually standardized. While it's available on every system I've ever used (various BSDs, Mac OS X, Linux, Solaris, Cygwin), if you somehow manage to find one in which it doesn't, the following should work:

作为由“只是有人”指出yes实际上不规范。虽然它在我使用过的每个系统(各种 BSD、Mac OS X、Linux、Solaris、Cygwin)上都可用,但如果你设法找到一个它没有的系统,以下应该有效:

while true; do echo no; done | <command>

Or as a full-fledged shell script implementation of yes, you can use the following:

或者作为一个完整的 shell 脚本实现yes,您可以使用以下内容:

#!/bin/sh

if [ $# -ge 1 ]
then
    while true; do echo ""; done
else
    while true; do echo y; done
fi

回答by miku

actually, it looks funny ...

实际上,看起来很有趣......

$ yes no

manpages excerpt:

联机帮助页摘录:

$ man yes 

YES(1)                    BSD General Commands Manual                   YES(1)

NAME
     yes -- be repetitively affirmative

SYNOPSIS
     yes [expletive]

DESCRIPTION
     yes outputs expletive, or, by default, ``y'', forever.

...

回答by ghostdog74

for systems with no such command, just a simple echo should work

对于没有此类命令的系统,只需简单的 echo 即可

echo "no" | command

for repetitions , not that hard to make a while/for loop that goes on forever.

对于重复,制作一个永远持续的 while/for 循环并不难。

回答by just somebody

just in case you might be interested in some portability: yes(1) is nonstandardin that it's not described in the Single Unix Specification (POSIX by another name). but it's quite portable anyway (see the HISTORY paragraph; pity The MYYN didn't quote the whole thing):

以防万一您可能对某些可移植性感兴趣: yes(1) 是非标准的,因为它没有在 Single Unix Specification(POSIX 的另一个名称)中描述。但无论如何它都非常便携(请参阅 HISTORY 段落;遗憾的是 MYYN 没有引用整个内容):

YES(1)                  FreeBSD General Commands Manual                 YES(1)

NAME
     yes — be repetitively affirmative

SYNOPSIS
     yes [expletive]

DESCRIPTION
     The yes utility outputs expletive, or, by default, “y”, forever.

HISTORY
     The yes command appeared in Version 32V AT&T UNIX.

FreeBSD 9.0                      June 6, 1993                      FreeBSD 9.0

edit

编辑

in case you hit an odd system that does not implement this command, it's trivial to provide it yourself. this from FreeBSD-9:

如果您遇到一个没有实现此命令的奇怪系统,那么您自己提供它是微不足道的。这来自 FreeBSD-9:

int
main(int argc, char **argv)
{
 if (argc > 1)
  while (puts(argv[1]) != EOF)
   ;
 else
  while (puts("y") != EOF)
   ;
 err(1, "stdout");
 /*NOTREACHED*/
}

回答by StackExchange What The Heck

If you find that yesanswers too fast for some reason, you can slow things down a little by using a modified version of @Brian Campbell 's answer - this adds in 0.1 seconds of delay in between each repitition of the word "no"

如果您发现yes由于某种原因答案太快,您可以使用@Brian Campbell 答案的修改版本来减慢速度 - 这会在每次重复“不”这个词之间增加 0.1 秒的延迟

while true; do echo no; sleep 0.1s; done | <command here>