## 30.10. 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 (Section 29.11) on sh-like shells.) Most of these aliases run RCS (Section 39.5) 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 NOTE." "$f"'

The h alias stores the filename in a shell variable (Section 35.9). 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) (Section 35.14) 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) (Section 28.16) instead of `&&`.

-- JP

Copyright © 2003 O'Reilly & Associates. All rights reserved.