I find filter programs that have an "in place" option very useful — for
example, sed -i
or perl -i
.
But wouldn't it be nice if you could do that with any filter?
Meet inplace. Invoke this script as:
inplace COMMAND ... -- FILE ...
and it'll filter each FILE
through COMMAND
, using the usual trick of writing the output to a temporary file in the same directory and then renaming it over the original (unless the command failed).
Some examples:
inplace sort -u -- channels.conf
inplace jpegtran -rotate 90 -- *.jpg
This program is an implementation of the xargs
Unix design pattern: it takes
a command and a separate list of arguments to apply it to.
However, there isn't a standard way to separate the command arguments from the
remaining arguments, so the choice of --
is rather arbitrary.
Some options I've seen:
find
usesCOMMAND ... \;
.xargs
avoids the problem by taking the list of filenames on stdin (whitespace-separated — which is awkward given that modern filenames can quite reasonably contain whitespace).- execline used to
use
COMMAND ... \;
, but now usesCOMMAND ... ''
(i.e. the empty string). - GNU parallel uses
COMMAND ... :::
, or reads lines from stdin if no:::
is found.
I quite like the ''
solution; having the option to read lines from stdin
is also occasionally useful.