Okay, so I'm in a situation where I'd really like to be using either a co-process via coproc
or via redirection such as <(some command)
, but unfortunately I'm limited to bash 3.2 in one of the my target environments, which means I'm limited in what I can do.
The reason I need a co-process is that I need to read line-by-line from one file, while looping over another.
Currently I'm using exec <6 /foo/bar
to create keep a file open for reading so that I can do read line <&6
whenever I need more input. This works fine, but it only works on plain-text files, however really I'd like to keep my file(s) compressed, rather than decompressing them before running my script.
I also need to be able to do the same for writing to a new, compressed file without having to waste space writing in plain-text then compressing afterwards.
So… are there any alternatives available in bash 3? As I've noted, I'm already in a loop over another file, so I don't have the option of just piping my output into gzip
(or piping zcat
into my loop) as I need to do this independently of my loop.
To try to give an example, here's a stripped down version of what I'm doing now:
# Decompress compressed match-file
gzip -dc /foo/compressed.gz > /tmp/match
# Setup file handles (to keep files open for reading/writing)
exec 5< /tmp/match
exec 6> /tmp/matches
# Loop over input file (/foo/bar) for matches
read next_match <&5
while read line; do
if [ "$line" = "$next_match" ]; then
read next_match <&5
echo "$line" >&6
fi
echo "$line"
done < /foo/bar
# Close file handles
exec <5&-
exec 6>&-
rm /tmp/match
# Compress matches and overwrite old match file
gzip -cf9 /tmp/matches /foo/compressed.gz
rm /tmp/matches
Forgive any typos, and the general uselessness of the actual script, I just wanted to keep it fairly simple. As you can see, while it works fine, it's not exactly optimal thanks to the wasteful plain-text files.