The yes utility in Linux and UNIX systems has been a simple but powerhouse tool for developers and sysadmins for decades. This unassuming command hides impressive capabilities for test automation, stress testing, performance benchmarking, and more through leveraging pipelines and redirects.

In this comprehensive guide, we‘ll uncover everything from the basics of using yes for automation to more advanced applications for system benchmarking and software testing.

An Introduction to the yes Command

The yes command simply outputs the default string "y" repeatedly, endlessly to standard output. This allows you to approve any confirmations and prompts asked by other commands and scripts in the pipeline non-interactively.

Some examples of using yes:

$ yes | rm -i files* # Automatically confirm file removals

$ yes | head -n 1000000 | wc -l # Stress test wc by feeding it tons of input 

$ yes | bash script.sh # Automate approvals for any prompts

Under the hood, yes leverages fork and pipes to efficiently generate the output without delay. The command itself is implemented in C, contained in the GNU coreutils package. Conceptually, it acts similarly to other simple data generators like cat outputting /dev/null.

In the UNIX philosophy, yes is an elegant building block that does one thing well – endlessly spit out "y". Combined creatively with other programs in the pipeline, incredible things are possible. Next we‘ll explore the syntax and options available.

yes Command Line Options and Syntax

The basic syntax of the yes command is simply:

yes [options] [string]

Options:

  • -h, --help – Print the help content for yes
  • -V, --version – Print which version of yes is installed

If no additional string is provided besides options, yes will default to endlessly outputting the string "y".

Examples:

$ yes
y
y
y
y
...

$ yes test
test
test
test
...

This makes yes perfect for automating confirmations that are looking specifically for a "y" or "yes" input to proceed.

Next, let‘s explore some common use cases taking advantage of this automated output.

Common yes Use Cases and Examples

While a simple command, yes unlocks several excellent applications through Linux pipelines. Let‘s go through some common examples.

1. Automating Interactive Confirmations

Commands like cp, mv, rm, and ln support flags like -i to interactively confirm each file operation before proceeding.

This quickly becomes tedious and repetitive for large batches of files. The yes command can automatically approve them:

$ yes | cp -i *.mp3 /mnt/external-drive
$ yes | rm -i /temp/*~*  
$ yes | ln -i ~/downloads/*.pdf books/references

You can also use yes n or yes N to automatically deny all interactive confirmations.

Bulk file operations with thousands of confirmations are completed almost instantly – no need to babysit the terminal and type Y constantly!

2. Stress Testing Software with High Load

By generating endless repeating output, you can pipe yes into other programs to observe their performance and stability under heavy sustained load.

For example, pipe millions of inputs to wc -l to benchmark it:

$ time yes | head -n 1000000 | wc -l
1000000

real    0m3.419s
user    0m2.513s
sys     0m0.776s   

Other ideas:

  • Pipe input to compression tools like gzip
  • Feed random data to checksum programs like md5sum, sha256sum
  • Test database load speeds by inserting newline JSON objects
  • Overflow text editor buffers to test their limits
  • Crash underperforming processes under high data loads

This simple technique can uncover stability issues and performance limits quickly.

3. Automating Script Approvals and Confirmations

Bash scripts often require interactive user input for confirms, selections, etc before proceeding.

Rather than manually inputting these or editing the scripts, you can automatically feed approvals with yes:

$ yes | sudo bash critical_script.sh
$ yes | python setup.py install
$ yes | ./configure && make && make install

As long as the scripts are looking for a "y"/"yes" to proceed at inputs, this will work great.

4. Generating Random Data

While contents of /dev/urandom should be preferred for cryptography, yes can produce an endless stream of randomized y/Y/yes/YES responses:

$ yes 2>&1 | tr -d ‘\n‘ | head -c 1000000 > random-bytes
$ openssl rand -base64 1000000 > better-random-bytes  

Piping the output without newlines ensures a continuous stream on one line for quick, moderate quality randomness.

5. Automating Key Generation

For testing encrypted communications and file transfer tools, automatically generating valid keys and certs is handy.

Many openssl subcommands can intake yes to progress through key generation prompts:

$ yes | openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out certificate.pem
$ yes | ssh-keygen -q -N "" -f keypair

This outputs a functional self-signed certificate and passphrase-less SSH keypair for integration testing.

6. Fuzz Testing with Random Inputs

Fuzzing is a software testing technique that throws extreme random data at an API or application to catch edge cases and uncover crashes.

A quick and dirty fuzzer can be built with yes:

$ yes | head -c 50000 | ./vulnerable-app
$ yes | tr -d ‘\n‘ | head -c 50000 | curl -d @- http://localhost/test_api

This streams 50KB of randomized y/n characters at the app, watching logs and output for anomalies.

While not sufficient for robust security testing, it can augmented with tools like AFL for open source audits.

7. Benchmarking Hardware Performance

Piping yes workloads can also benchmark aspects like CPU, memory, storage speeds, and network throughput.

CPU Tests

$ yes > /dev/null # Max 1 core 
$ yes | yes > /dev/null # Max 2 cores
$ yes | yes | yes > /dev/null # Max 3 cores

Memory Tests

Use tools like stress and memtester while piping yes to other processes:

$ memtester 1G test & yes > /dev/null

Storage Write Speeds

$ yes | tr -d ‘\n‘ | dd of=tempfile bs=32M iflag=fullblock
$ rm tempfile # clean up 

Network Throughput

Pipe yes into networking tools like ncat, nc, openssl s_client:

$ yes | nc -l 4444 > /dev/null # server
$ yes | nc 192.168.1.5 4444 # client 

Monitor transferred bytes and timing to detect bottlenecks under heavy sequential I/O.

There are more advanced purpose-built system benchmarking utilities, but yes provides a quick and dirty way to stress components.

8. Generating Dummy Data

Need to quickly populate files/databases with nonsense Lorem Ipsum text for testing environments?

Use yes with tools like head and sed:

$ yes Lorem | head -n 100 > lorem-file
$ yes Ipsum | sed -n ‘1,100p‘ > ipsum-file

You can even insert it straight into MySQL databases:

$ yes "INSERT INTO test_table (text_column) VALUES (‘Lorem‘)"; | head -n 500 | mysql -u root -D test_db

This flexibility makes yes great for seeding test data.

As you can see, the applications are almost endless despite the command being plain and simple.

Now that we‘ve covered common examples, let‘s discuss some best practices and tips for using yes effectively.

Key yes Tips and Best Practices

Here are some key best practices to follow when leveraging yes:

  • Redirect stdout to /dev/null when benchmarking to avoid unnecessary terminal output.
  • Understand that each yes invocation uses non-trivial CPU, so it cannot scale up infinitely.
  • Append | head or | sed to restrict total outputs if necessary.
  • Prefer /dev/urandom over yes for cryptography/security uses.
  • Use multiple yes pipes in parallel to saturate multi-core systems.
  • Ensure the receiving processes properly handle endless input without memory leaks.
  • Comparing yes | xargs -P vs plain yes | pipes shows the throughput differences.
  • Adding stdbuf -o0 can reduce output buffering in some receiving programs.
  • Consider written data with yes as disposable for testing since it lacks context.

Following these tips will ensure you apply yes correctly while avoiding common pitfalls.

Now let‘s compare yes to some alternative methods before concluding.

yes vs Other Tools Like echo, cat & while Loops

Besides the yes command, there are a few other common ways in Bash to generate repetitive output. Let‘s contrast the differences:

echo

The echo command can print arguments and strings. But it lacks native looping.

cat

The cat tool can print file contents to stdout, but not variable output.

while Loops

Bash while loops can replicate yes behavior but less efficiently:

while true; do echo y; done

Compared to these approaches, yes:

  • Has native C implementation for speed
  • Forks processes instead of bash subshells
  • Reduces memory vs infinite while loop
  • Handles buffers smarter for piping
  • Is more portable across UNIX platforms
  • Easier invocation than echo/cat hacks

So while other methods can generate automated output, yes remains highly optimized for that singular purpose.

While a simple command, yes provides immense power when creatively combined with other UNIX programs via pipes and redirects. It excel at test automation, output generation, performance testing, and other niche applications.

I hope this guide has shed some light on the underlying utility of this tool and inspired you to harness yes beyond just automated confirmations. Let me know if have any other favorite tricks or use cases for yes!

Similar Posts