This isn't going to be particularly simple. Your question is really too broad, as it stands. One approach would be:
- Determine whether the archive contains one or two files
- Set up named pipes (fifos) for each of the files ("mkfifo" command)
- Run commands to output the content of the files in the archive to the appropriate fifo, as a background process
- Run the primary command, specifying the fifos as the filename arguments
Giving a full rundown of all of this is, I think, beyond what should be the scope of a Stackoverflow question. For (1), you could probably do something like:
FILECOUNT=`tar -vzf (filename.tar.gz) | wc -l`
This lists the files within the archive (tar -vzf
) and counts the number of lines of output from that command (wc -l
). It's not foolproof but should work if the filenames are simple names like the ones you suggested (file1.txt, file2.txt).
For (2), make either one or two fifos as appropriate:
mkfifo file1-fifo.txt
if [ $FILECOUNT = 2 ]; then
mkfifo file2-fifo.txt
fi
For (3), use tar
with -O
to extract file contents from the archive, and redirect it to the fifo(s), as a background process:
tar -O -xf (filename.tar.gz) file1.txt > file1-fifo.txt &
if [ $FILECOUNT = 2 ]; then
tar -O -xf (filename.tar.gz) file2.txt > file2-fifo.txt &
fi
And then (4) is just:
SECONDFILE=""
if [ $FILECOUNT = 2 ]; then
SECONDFILE=file2-fifo.txt
fi
./main 1000 file1-fifo.txt $SECONDFILE
Finally, you should delete the fifo nodes:
rm file1-fifo.txt
rm file2-fifo.txt
Note that this will involve extracting the archive contents twice (in parallel), once for each file. There's no way (that I can think of) of getting around this.