UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 9.24 Get File List by Editing Output of ls -l, grep, etc. Chapter 9
Saving Time on the Command Line
Next: 9.26 Expect
 

9.25 The C Shell repeat Command

The C shell has a built-in command that lets you execute a command repeatedly:

% repeat n command

All you do is specify the number of repetitions, followed by the command you want to re-execute. A trivial example would be:

% repeat 4 echo Enter name:
Enter name:
Enter name:
Enter name:
Enter name:

Simple, right? Just imagine what Jack Nicholson could have done in the movie The Shining if he had traded in his typewriter for a UNIX system:

% repeat 500 echo "All work and no play makes Jack a dull boy."

Ok, this is fun at first, but you may soon wonder whether this command has any down-to-earth uses. It does, and I'll conclude with some more useful examples:

  1. Print three copies of memo:

    % repeat 3 pr memo | lp

  2. Run popd (14.6) four times to clear a directory stack:

    % repeat 4 popd

  3. Append 50 boilerplate files to report:

    % repeat 50 cat template >> report

Some versions of the C shell repeat command have a quoting bug. See the end of article 47.2.

- DG


Previous: 9.24 Get File List by Editing Output of ls -l, grep, etc. UNIX Power ToolsNext: 9.26 Expect
9.24 Get File List by Editing Output of ls -l, grep, etc. Book Index9.26 Expect

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System