yes (Unix)

yes is a Unix command, which outputs an affirmative response, or a user-defined string of text continuously until killed.

Description

By itself, the yes command outputs 'y' or whatever is specified as an argument, followed by a newline repeatedly until stopped by the user or otherwise killed; when piped into a command, it will continue until the pipe breaks (i.e., the program completes its execution).

It can also be used to test how well a system handles high loads, as using yes results in 100% processor usage for systems with a single processor (for a multiprocessor system, a process must be run for each processor).

Source code

In C++:

#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
  if (argc == 1) {
    while (1) {
      cout << "y" << endl;
    }
  } else {
    while (1) {
      cout << argv[1] << endl;
    }
  }
}

In C:

#include <stdio.h>

int main(int argc, char *argv[]) {
  if (argc == 1) {
    while (1) {
      printf("y\n");
    }
  } else {
    while (1) {
      printf("%s\n", argv[1]);
    }
  }
}

Uses

yes can be used to send an affirmative (or negative; e.g. yes n) response to any command that would otherwise request one, thereby causing the command to run non-interactively.

This usage may be obsolete today, as most commands that would request response from the user have either a 'force' option (e.g., rm -f) or an 'assume-yes' option (e.g., apt-get -y).

As an example, the following:

rm -f *.txt

is functionally equivalent to

yes | rm *.txt

The yes command in conjunction with the head command can be used to generate large volume files for means of testing. For example, executing

yes 1234567 | head -1000 > file

results in a file consisting of 1000 lines each consisting of eight characters (1, 2, 3, 4, 5, 6, 7 and newline).

In 2006, the yes command received publicity for being a means to test whether or not a user's MacBook is affected by the Intermittent Shutdown Syndrome. By running the yes command twice via Terminal under Mac OS X, users were able to max out their computer's CPU, and thus see if the failure was heat related.[1][2]

References

  1. ↑ "Test for MacBook Random Shutdown Syndrome (RSS)". 2006-08-29. Retrieved 2012-05-09.
  2. ↑ "Testing your MacBook for Random Shut Downs". the apple files. 2006-08-02. Archived from the original on 2007-02-10. Retrieved 2008-01-16.

Further reading

External links

This article is issued from Wikipedia - version of the 9/6/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.