- The 25+ best Black Friday Nintendo Switch deals 2024
- Why there could be a new AI chatbot champ by the time you read this
- The 70+ best Black Friday TV deals 2024: Save up to $2,000
- This AI image generator that went viral for its realistic images gets a major upgrade
- One of the best cheap Android phones I've tested is not a Motorola or Samsung
Using the yes command to automate responses
One of the more unusual Linux commands is named “yes”. It’s a very simple tool intended to help you avoid having to answer a lot of questions that might be asked when you run a script or a program that needs a series of responses to do its work.
If you type “yes” by itself at the command prompt, your screen is going to start filling up with the just the letter “y” (one per line) until you hit control-C to stop it. It’s also incredibly fast. Unlike what you see displayed below, yes will likely spit out more than a million y’s in the time it likely takes you to reach down and press control-C. Fortunately, that’s not all that this command can do.
$ yes y y y ^C
If you like, you can pipe the output of the yes command to the head command to see a small sample of its output.
$ yes | head -3 y y y
Once the command receiving the output ends, the yes command ends as well.
If you want to fill up your screen with “no”, some other word, or even a complete sentence, you can do that. The command “yes no” will repeatedly display “no” until you terminate the command, again with a control-C.
$ yes no | head -3 no no no
You could even use a command like “yes I am filling up my screen” and watch your screen filling up with “I am filling up my screen”.
There is one thing that you shouldn’t even consider doing with yes. Here goes:
WARNING: One thing you really shouldn’t do is redirect the output of the yes command to a file. It will grow a lot faster than you will likely expect.
So, what is yes meant to do?
Since we’ve covered what yes can do, let’s look at how it’s meant to be used. The yes command was built to send answers to scripts or binaries when you prefer not to have to answer their questions yourself. In fact, the yes command makes it easy to run commands non-interactively and is often used to automate processes that will run overnight.
The limitation is that whenever you use the yes command, every line of output is going to be the same – whether “y” (the default), “yes” or something else. There is no way that you can get yes to answer “yes” to one question and “no” to another.
If you were using a script like the one shown below that asks a series of questions and you want the yes command to answer them for you, you could pipe the output of yes to the script using a command like this:
$ yes | myscript
The script would read the “y” answers provided by the yes command and respond accordingly.
$ yes | askme Ready to answer some questions? [y,n] Did you work more than 8 hours today? [y,n] Then you may need some rest! Are you ready for a nap? [y,n] Get some nice rest!
Alternately, you could ask yes to answer all questions with an “n”:
$ yes no | askme Ready to answer some questions? [y,n] OK, good bye!
The script:
#!/bin/bash echo "Ready to answer some questions? [y,n]" read ans if [ "$ans" != "y" ]; then echo "OK, good bye!" exit fi echo "Did you work more than 8 hours today? [y,n]" read ans case $ans in y) echo "Then you may need some rest!";; n) echo "OK, but you can take a short break";; *) echo "Avoiding the question?" esac echo "Are you ready for a nap? [y,n]" read ans case $ans in y) echo "Get some nice rest!"; exit;; n) echo "OK. Get some good work done!";; *) echo "Too difficult a question?";; esac
Wrap-up
The yes command is limited to repeating the same response to every question asked by a script or command, but it provides a way to run commands without having to be present to provide the needed responses.
Copyright © 2022 IDG Communications, Inc.