Previous: Programming in M4sugar, Up: Programming in M4


8.4 Programming in M4sh

M4sh, pronounced “mash”, is aiming at producing portable Bourne shell scripts. This name was coined by Lars J. Aas, who notes that, according to the Webster's Revised Unabridged Dictionary (1913):

Mash \Mash\, n. [Akin to G. meisch, maisch, meische, maische, mash, wash, and prob. to AS. miscian to mix. See “Mix”.]
  1. A mass of mixed ingredients reduced to a soft pulpy state by beating or pressure....
  2. A mixture of meal or bran and water fed to animals.
  3. A mess; trouble. [Obs.] –Beau. & Fl.

For the time being, it is not mature enough to be widely used.

M4sh provides portable alternatives for some common shell constructs that unfortunately are not portable in practice.

— Macro: AS_DIRNAME (pathname)

Return the directory portion of pathname, using the algorithm required by POSIX. See Limitations of Usual Tools, for more details about what this returns and why it is more portable than the dirname command.

— Macro: AS_IF (test, [RUN-IF-TRUE], [RUN-IF-FALSE])

Run shell code TEST. If TEST exits with a zero status then run shell code RUN-IF-TRUE, else run shell code RUN-IF-FALSE, with simplifications if either RUN-IF-TRUE or RUN-IF-FALSE is empty.

— Macro: AS_MKDIR_P (filename)

Make the directory filename, including intervening directories as necessary. This is equivalent to `mkdir -p filename', except that it is portable to older versions of mkdir that lack support for the -p option. Also, AS_MKDIR_P succeeds if filename is a symbolic link to an existing directory, even though POSIX is unclear whether `mkdir -p' should succeed in that case.

— Macro: AS_SET_CATFILE (var, dir, file)

Set the shell variable var to dir/file, but optimizing the common cases (dir or file is `.', file is absolute etc.).