gsinha
gsinha

Reputation: 1185

Capture tee's argument inside piped Perl execution

How to capture piped command's argument ?

I use :

perl my_script.pl -some_args  |  tee arg_filename

How to get arg_filename 's value inside my_script.pl ?

CONTEXT
I need to send this filename in a mail which my_script.pl sends at the end.
I need to use tee because we dump huge output in the standard logging which we use inside my_script.pl but we print more relevant things on STDOUT and STDERR.
This script imports and uses additional proprietary libraries which could not be edited. But they too generate logs.

Upvotes: 2

Views: 57

Answers (2)

Erik Bennett
Erik Bennett

Reputation: 1099

If I had to "cheat", I might try something akin to:

#!/bin/sh
MYLOG=/tmp/logfile
export MYLOG
perl my_script.pl -some_args  |  tee $MYLOG

That is, get the name of the tee file before you invoke either perl or tee. Everyone else's answers are more correct; I only mention this as a counter-measure.

Upvotes: 0

Sobrique
Sobrique

Reputation: 53498

The short answer is - you can't.

tee is a separate process with it's own arguments. There is no way to access these arguments from that process. (well, I suppose you could run ps or something).

The point of tee is to take STDOUT write some of it to a log file, and pass through the rest of it down the 'pipe'. (Which results in printing it if nothing else).

What you could probably do instead is implement some manner of logging within your perl script, where the core functionality of tee is replicated by printing a line to both STDOUT and a designated log file.

Upvotes: 3

Related Questions