Running multiple commands with xargs
cat a.txt | xargs -d $'\n' sh -c 'for arg do command1 "$arg"; command2 "$arg"; ...; done' _
...or, without a Useless Use Of cat:
<a.txt xargs -d $'\n' sh -c 'for arg do command1 "$arg"; command2 "$arg"; ...; done' _
To explain some of the finer points:
The use of
"$arg"
instead of%
(and the absence of-I
in thexargs
command line) is for security reasons: Passing data onsh
's command-line argument list instead of substituting it into code prevents content that data might contain (such as$(rm -rf ~)
, to take a particularly malicious example) from being executed as code.Similarly, the use of
-d $'\n'
is a GNU extension which causesxargs
to treat each line of the input file as a separate data item. Either this or-0
(which expects NULs instead of newlines) is necessary to prevent xargs from trying to apply shell-like (but not quite shell-compatible) parsing to the stream it reads. (If you don't have GNU xargs, you can usetr '\n' '\0' <a.txt | xargs -0 ...
to get line-oriented reading without-d
).The
_
is a placeholder for$0
, such that other data values added byxargs
become$1
and onward, which happens to be the default set of values afor
loop iterates over.
With GNU Parallel you can do:
cat a.txt | parallel 'command1 {}; command2 {}; ...; '
Watch the intro videos to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
For security reasons it is recommended you use your package manager toinstall. But if you cannot do that then you can use this 10 secondsinstallation.
The 10 seconds installation will try to do a full installation; ifthat fails, a personal installation; if that fails, a minimalinstallation.
$ (wget -O - pi.dk/3 || lynx -source pi.dk/3 || curl pi.dk/3/ || \ fetch -o - http://pi.dk/3 ) > install.sh$ sha1sum install.sh | grep 883c667e01eed62f975ad28b6d50e22a12345678 883c667e 01eed62f 975ad28b 6d50e22a$ md5sum install.sh | grep cc21b4c943fd03e93ae1ae49e28573c0cc21b4c9 43fd03e9 3ae1ae49 e28573c0$ sha512sum install.sh | grep da012ec113b49a54e705f86d51e784ebced224fdf79945d9d 250b42a4 2067bb00 99da012e c113b49a 54e705f8 6d51e784 ebced224fdff3f52 ca588d64 e75f6033 61bd543f d631f592 2f87ceb2 ab034149 6df84a35$ bash install.sh