Shell – Running several times the same command with several arguments after a pipe

pipeshelltee

I have a key (of random binary data) that gets generated by get_key.

And with this key, I can do several things with my encrypted files. For example, I can decrypt them.

get_key | tee >(decrypt file1) >(decrypt file2)

I would like to know how I could generalize that to n files where the files are given as FILES=file1 file2 file3 file4 file5.

At the moment, I can see two solutions:

1) Compute a string and eval it

2) replace decrypt by a recursive function f that calls decrypt does tee >(decrypt A[0]) | f ("${A[@]:1}") (it decrypts the first element and calls itself recursively) if the array is not empty and nothing if it is.

I wanted to know if you had a nicer way to doing that (note that I do not want the key to be written to a file or a variable, so loops aren't an option).


Edit: I'll use it in https://github.com/xavierm02/combine-keys

Best Answer

Make FIFOs in a loop and have your decrypts wait for them to be written to:

for i in "${A[@]}";do
    mkfifo /tmp/"$i"_fifo
    decrypt "$i" </tmp/"$i"_fifo &
done
getkey | tee >/dev/null /tmp/*_fifo
rm -f /tmp/*_fifo