I'm trying to use tee in a for loop as such:
for ea in $(ls *bam)
do samtools mpileup -f $ref $ea | \
tee \
>(java -jar $varscan2 mpileup2indel --output-vcf 1 > vcf/"$(echo $ea | sed s/.bam//)"_mpileup2indel.vcf) \
>(java -jar $varscan2 mpileup2snp --output-vcf 1 > vcf/"$(echo $ea | sed s/.bam//)"_mpileup2snp.vcf) | \
tail -n 5
done;
I.e. using the output of samtools mpileup, and piping that into two separate commands. I added the tail -n 5 to prevent the output of samtools mpileup from being printed in its entirety to stdout (I want full output to be used as input to java -jar varscan, however).
This seems to work initially, but the command doesn't seem to complete (file size for each output is smaller than if command was done without tee).
Eventually I get an error that the two java -jar $varscan commands are waiting for an input that never arrives (prior to getting a chance to start the second iteration of the loop).
Is this the best way to accomplish what I'm after, i.e. using the output of the first command in two separate commands (ideally, not recording/printing the output of the first command at all)? Is tee incompatible with for loops?
Thanks in advance.
tailshould be piped right after the command you want to tail, not at the end of the pipe line. Also look at Why not parsels(and what to do instead)?. – schrodingerscatcuriosity Oct 22 '21 at 17:59java -jar $varscanprocesses, or do you get an error from each of those two processes (i.e. two error messages)? It would probably be useful if you could edit the actual error messages into your question. – fra-san Oct 22 '21 at 18:05for ea in $(ls *bam)you are parsing the output of thelscommand, which for various reasons detailed in the link is considered bad practice. – schrodingerscatcuriosity Oct 22 '21 at 18:06samtools mpileupto be piped in its entirety to myteecommands, but not to the stdout. I'm not sure how else to accomplish this. – Rebecca Eliscu Oct 22 '21 at 18:25-poption of tee:| tee -p .... – Oct 22 '21 at 18:30head, but not when totail. In general anything that exits before EOF and causesteeto get SIGPIPE will make it not send the whole stream to files specified as operands (process substitutions in your case). Istail -n 5in your actual code? If not then maybe the actual command exits early.tee -pseems a good idea. If-pis not supported then trytee … | { actual_command; cat >/dev/null; }, so even afteractual_commandexits,catreads the rest of the stream fromteeandteenever gets SIGPIPE. – Kamil Maciorowski Oct 23 '21 at 11:14