
linux yes 命令

The yes command seems too simple to be of any practical use, but in this tutorial, we’ll show you its application and how to benefit from its pent-up positivity in Linux and macOS.
yes命令似乎太简单了,无法实际使用,但是在本教程中,我们将向您展示其应用程序以及如何从Linux和macOS中被抑制的积极性中受益。
yes命令 (The yes Command)
The yes
command is one of the simplest commands in Linux and other Unix-like operating systems like macOS. And by simple, we mean simple in its use and its initial implementation. The source code for the original version—released in System 7 Unix and authored by Ken Thompson—amounts to a mere six lines of code.
yes
命令是Linux和其他类似macOS的类Unix操作系统中最简单的命令之一。 简单来说,我们的意思是其使用和初始实现都很简单。 由System 7 Unix发布并由Ken Thompson编写的原始版本的源代码只有六行代码。
But don’t write it off for being a simple little command. It can be used in some interesting and useful ways.
但是不要因为只是一个简单的小命令而将其写下来。 可以以一些有趣且有用的方式使用它。
是的,怎么办? (What Does yes Do?)
Used without any command line parameters, the yes
command behaves as though you were typing “y” and hitting Enter, over and over (and over and over) again. Very quickly. And it will carry on doing so until you press Ctrl+C to interrupt it.
如果不使用任何命令行参数,则yes
命令的行为就好像您在键入“ y”并再次按Enter(一次又一次)一样。 很快它将继续进行直到您按Ctrl + C中断它为止。
yes

In fact, yes
can be used to repeatedly generate any message you choose. Simply type yes
, a space, the string you wish to use, and then press Enter. This is often used to cause yes
to generate an output stream of “yes” or “no” strings.
实际上,可以使用yes
重复生成您选择的任何消息。 只需键入yes
,一个空格,您要使用的字符串,然后按Enter。 这通常用于使yes
生成“ yes”或“ no”字符串的输出流。
yes yes

yes anything you like

但是,这有什么用? (But What Use Is That?)
The output from yes
can be piped into other programs or scripts.
yes
的输出可以通过管道传递到其他程序或脚本中。
Does this sound familiar? You start a long process running and step away, leaving it to run. When you return to your computer, the process hasn’t completed at all. In your absence, it has asked you a question and is sat waiting for a “yes” or “no” response.
这听起来很熟悉吗? 您开始运行一个漫长的过程,然后走开,然后继续运行。 当您返回计算机时,该过程根本没有完成。 在您不在的情况下,它会问您一个问题,请您静静等待“是”或“否”的答复。
If you know in advance that all your answers are going to be positive (“yes” or “y”) or negative (“no” or “n”) you can use yes
to provide those responses for you. Your long process will then run through to completion unattended with yes
providing the answers to any questions the process asks.
如果您事先知道所有答案都是肯定的(“是”或“ y”)或否定的(“否”或“ n”),则可以使用“ yes
为您提供这些答案。 然后,您的漫长过程将无人照看地完成直到完成为止, yes
为过程中提出的任何问题提供答案。
在脚本中使用yes (Using yes With Scripts)
Look at the following Bash shell script. (We need to imagine that this is a part of a much larger script that will take a considerable time to run.)
查看以下Bash shell脚本。 (我们需要想象这是一个更大的脚本的一部分,它将花费大量时间来运行。)
#!/bin/bash
# ...
# in the middle of some long script
# obtain a response from the user
# ...
echo "Are you happy to proceed? [y,n]"
read input
# did we get an input value?
if [ "$input" == "" ]; then
echo "Nothing was entered by the user"
# was it a y or a yes?
elif [[ "$input" == "y" ]] || [[ "$input" == "yes" ]]; then
echo "Positive response: $input"
# treat anything else as a negative response
else
echo "negative response: $input"
fi
This script asks a question and awaits a response. The logic flow within the script is decided upon by the input from the user.
该脚本提出一个问题并等待响应。 脚本中的逻辑流程取决于用户的输入。
- A “yes” or “y” indicates a positive response. “是”或“ y”表示肯定的响应。
- Any other input is considered a negative response. 任何其他输入均被视为否定响应。
- Pressing Enter with no input text does nothing. 按Enter键,不输入任何文字。
To test this, copy the script to a file and save it as long_script.sh
. Use chmod
to make it executable.
要对此进行测试,请将脚本复制到文件中,并将其另存为long_script.sh
。 使用chmod
使其可执行。
chmod +x long_script.sh
Run the script with the following command. Try providing “yes,” “y,” and anything else as input, including pressing Enter with no input text.
使用以下命令运行脚本。 尝试提供“是”,“ y”和其他任何内容作为输入,包括按Enter键而不输入任何文本。
./long_script.sh

To get yes
to provide our response to the script’s question, pipe the output from yes
to the script.
为了得到yes
向我们的脚本的问题的回应,从输出管yes
给脚本。
yes | ./long_script.sh

Some scripts are more rigid in their requirements and only accept the full word “yes” as a positive response. You can provide “yes” as a parameter to yes
, as follows:
一些脚本的要求更为严格,只接受完整的“是”作为肯定的回答。 您可以提供“是”作为一个参数yes
,如下所示:
yes yes | ./long_script.sh

不经过深思就不要说是 (Don’t Say yes Without Thinking It Through)
You need to be certain that the input you are going to feed into the script or program is definitely going to give you the outcome you expect. To be able to make that decision, you must know the questions and what your responses should be.
您需要确定要输入到脚本或程序中的输入一定会为您带来预期的结果。 为了做出决定,您必须知道问题以及应该做出的回应。
The logic in the script, command, or program might not match your expectations. In our example script, the question might have been “Do you wish to stop? [y,n].” If that had been the case, a negative response would have allowed the script to proceed.
脚本,命令或程序中的逻辑可能与您的期望不符。 在我们的示例脚本中,问题可能是“您想停止吗? [y,n]。” 如果真是这样,那么否定的答复将允许脚本继续进行。
You must be familiar with the script, command, or program before you blithely pipe yes
into it.
您必须先熟悉脚本,命令或程序,然后才能将yes
管道传输到其中。
在命令中使用yes (Using yes With Commands)
In its infancy, yes
would be used with other Linux commands. Since then, most of those other Linux commands have their own way of running without human interaction. yes
is no longer required to achieve that.
起初, yes
将与其他Linux命令一起使用。 从那时起,大多数其他Linux命令都有自己的运行方式,而无需人工干预。 yes
,不再需要达到此目的。
Let’s take the Ubuntu package manager apt-get
as an example. To install an application without having to press “y” half-way through the installation, yes
would have been used as follows:
让我们以Ubuntu软件包管理器apt-get
为例。 要安装应用程序而不必在安装过程中半按“ y”,将按以下方式使用yes
:
yes | sudo apt-get install fortune-mod

The same result can be achieved using the -y
(assume yes) option in apt-get
:
使用apt-get
的-y
(假设是)选项可以实现相同的结果:
sudo apt-get -y install fortune-mod

You’ll see that apt-get
didn’t even ask its usual “Do you want to continue? [Y/n]” question. It just assumed the answer would be “yes.”
您会看到apt-get
甚至没有问它通常的“您要继续吗? [是]。 它只是假设答案是“是”。
On other Linux distributions, the situation is the same. On Fedora you would have used this type of package manager command at one time:
在其他Linux发行版上,情况相同。 在Fedora上,您可能一次使用过这种类型的软件包管理器命令:
yes | yum install fortune-mod
The dnf
package manager has replaced yum
and dnf
has its own -y
(assume yes) option.
dnf
软件包管理器已替换yum
并且dnf
具有其自己的-y
(假设是)选项。
dnf -y install fortune-mod
The same applies to cp
, fsck
, and rm
. These commands each have their own -f
(force) or -y
(assume yes) options.
cp
, fsck
和rm
。 这些命令每个都有自己的-f
(强制)或-y
(假定是)选项。
So does it seem that yes
has been relegated to only working with scripts? Not quite. There are a few more tricks in the old dog yet.
如此看来, yes
已被降级为仅使用脚本吗? 不完全的。 老狗还有一些技巧。
一些进一步的技巧 (Some Further yes Tricks)
You can use yes
with a sequence of digits generated by seq
to control a loop of repeated actions.
您可以yes
seq
生成的一系列数字使用yes
,以控制重复动作的循环。
This one-liner echoes the generated digits to the terminal window and then calls sleep
for one second.
这种单线将生成的数字回显到终端窗口,然后调用sleep
一秒钟。
Instead of simply echoing the digits to the terminal window, you could call another command or script. That command or script doesn’t even need to use the digits, and they’re only there to kick-start each cycle of the loop.
您可以调用另一个命令或脚本,而不是简单地将数字回显到终端窗口。 该命令或脚本甚至不需要使用数字,它们只是在那里启动循环的每个周期。
yes "$(seq 1 20)" | while read digit; do echo digit; sleep 1; done

Sometimes it is useful to have a large file to test with. Perhaps you want to practice using the zip command, or you want to have a sizeable file to test FTP uploads with.
有时,使用大型文件进行测试很有用。 也许您想练习使用zip命令,或者想拥有一个较大的文件来测试FTP上传。
You can rapidly generate large files with yes
. All you need to do is give it a long string of text to work with and redirect the output into a file. Make no mistake; those files will grow rapidly. Be ready to press Ctrl+C within a few seconds.
您可以使用yes
Swift生成大文件。 您需要做的就是为它提供一长串文本,然后将输出重定向到文件中。 不犯错误; 这些文件将快速增长。 准备在几秒钟内按Ctrl + C。
yes long line of meaningless text for file padding > test.txt
ls -lh test.txt
wc test.txt

The file generated here took about five seconds on the test machine used to research this article. ls
reports that it is 557 Mb in size, and wc
tell us there are 12.4 million lines in it.
此处生成的文件在用于研究本文的测试机上花费了大约五秒钟。 ls
报告说它的大小为557 Mb,据wc
告诉我们其中有1240万条线。
We can limit the size of the file by including head
in our command string. We tell it how many lines to include in the file. The -50
means head
will let just 50 lines through to the test.txt
file.
我们可以通过在命令字符串中包含head
来限制文件的大小。 我们告诉它要在文件中包含多少行。 -50
表示head
将只允许50行进入test.txt
文件。
yes long line of meaningless text for file padding | head -50 > test.txt

As soon as there are 50 lines in the test.txt
file, the process will stop. You don’t need to use Ctrl+C. It comes to a graceful halt on its own volition.
一旦test.txt
文件中有50行,该过程将停止。 您无需使用Ctrl + C。 它自己的意志优雅地停止了。
wc
reports that there are exactly 50 lines in the file, 400 words and it is 2350 bytes in size.
wc
报告文件中恰好有50行,共400个字,大小为2350字节。
Even though it is still useful for feeding responses into long-running scripts (and a few other tricks), the yes
command isn’t going to be a part of your daily toolkit of commands. But when you do need it, you’ll find it is simplicity itself—and all in six lines of golden code.
尽管它对于将响应馈入长时间运行的脚本(以及其他一些技巧)仍然很有用,但是yes
命令不会成为您日常命令工具包的一部分。 但是,当您确实需要它时,您会发现它本身就是简单性,并且全部用六行黄金代码完成。
翻译自: https://www.howtogeek.com/415535/how-to-use-the-yes-command-on-linux/
linux yes 命令