Previous: Readfile Function, Up: General Functions [Contents][Index]
Michael Brennan offers the following programming pattern, which he uses frequently:
#! /bin/sh awkp=' … ' input_program | awk "$awkp" | /bin/sh
For example, a program of his named flac-edit has this form:
$ flac-edit -song="Whoope! That's Great" file.flac
It generates the following output, which is to be piped to the shell (/bin/sh):
chmod +w file.flac metaflac --remove-tag=TITLE file.flac LANG=en_US.88591 metaflac --set-tag=TITLE='Whoope! That'"'"'s Great' file.flac chmod -w file.flac
Note the need for shell quoting. The function shell_quote()
does it. SINGLE is the one-character string "'" and
QSINGLE is the three-character string "\"'\"":
# shell_quote --- quote an argument for passing to the shell
function shell_quote(s, # parameter
SINGLE, QSINGLE, i, X, n, ret) # locals
{
if (s == "")
return "\"\""
SINGLE = "\x27" # single quote
QSINGLE = "\"\x27\""
n = split(s, X, SINGLE)
ret = SINGLE X[1] SINGLE
for (i = 2; i <= n; i++)
ret = ret QSINGLE SINGLE X[i] SINGLE
return ret
}