UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 11.8 Repeating a Cycle of Commands Chapter 11
The Lessons of History
Next: 11.10 Check Your History First with :p
 

11.9 Running a Series of Commands on a File

[There are times when history is not the best way to repeat commands. Here, Jerry gives an example where a few well-chosen aliases can make a sequence of commands, all run on the same file, even easier to execute. -TOR]

While I was writing the articles for this book, I needed to look through a set of files, one by one, and run certain commands on some of those files. I couldn't know which files would need which commands, or in what order. So I typed a few temporary aliases on the C shell command line. (I could have used shell functions (10.9) on sh-like shells.) Most of these aliases run RCS (20.14) commands, but they could run any UNIX command (compilers, debuggers, printers, and so on).

% alias h 'set f="\!*";co -p -q "$f" | grep NOTE'
% alias o 'co -l "$f"'
% alias v 'vi "$f"'
% alias i 'ci -m"Fixed title." "$f"'

The h alias stores the filename in a shell variable (6.8). Then it runs a command on that file. What's nice is that, after I use h once, I don't need to type the filename again. Other aliases get the filename from $f:

% h ch01_summary
NOTE: Shorten this paragraph:
% o
RCS/ch01_summary,v  ->  ch01_summary
revision 1.3 (locked)
done
% v
"ch01_summary" 23 lines, 1243 characters
   ...

Typing a new h command stores a new filename.

If you always want to do the same commands on a file, you can store all the commands in one alias:

% alias d 'set f="\!*"; co -l "$f" && vi "$f" && ci "$f"'
% d ch01_summary

The && (two ampersands) (44.9) means that the following command won't run unless the previous command returns a zero ("success") status. If you don't want that, use ; (semicolon) (8.5) instead of &&.

- JP


Previous: 11.8 Repeating a Cycle of Commands UNIX Power ToolsNext: 11.10 Check Your History First with :p
11.8 Repeating a Cycle of Commands Book Index11.10 Check Your History First with :p

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