Bash: Chain of commands


I wondered more than once how to make a tool that chains together input/output filters using bash.
e.g. you would like to do a iteration of filters:

for i in 1 2 3 4; do grep -v $i; done

So I wrote this chaining tool that does the job in the obvious way:

chain.sh

#!/bin/bash
commands_file=$1
a=$(mktemp)
b=$(mktemp)
cat > $a
while read line; do
$line < $a > $b
mv $b $a
done < $commands_file
cat $a
rm $a

With this, you can do:

for i in 1 2 3 4; do
echo grep -v $i >> mycommands.txt
done
script/chain.sh mycommands.txt

lang=”bash”

  1. No comments yet.
(will not be published)

  1. No trackbacks yet.