Monday, January 9, 2012

UNIX Utilities III: Yes

This isn't a hugely popular program, but it does have its uses: the basic "yes" command, simply repeats whatever you put in to it, over and over and over again. This can be handy for things like testing buffering on a new shell you're developing.

Note that the usage is incredibly similar to echo, but simply provides it in a for loop:

/**
* A utility that emulates the UNIX command "yes", repeating the input
* until killed.
*
* Used in places like Jurassic Park (the movie) when Nedry leaves a
* trap that outputs the string "You didn't say the magic word!" over
* and over.
*
* Copyright 2011-12-23 Joseph Lewis <joehms22@gmail.com>
*/

#include <iostream>

using namespace std;

int main(int nargs, char* vargs[])
{
string s;

for(int i = 0; i < nargs - 1; i++)
{
if(i != 0)
s += " ";
s += vargs[i + 1];
}
s += "\n";

while(true)
{
cout << s;
}
}

No comments:

Post a Comment