-   Programming (
-   -   Getting the name of the process that sent data to a pipe? (bash) (

KaosX 02-16-2011 04:38 PM

Getting the name of the process that sent data to a pipe? (bash)
I am trying to automate some directory naming when we're manually running some scripts and are using tee to direct the output to a file (log).

Right now this is what we do


./ 2>&1 | tee /home/user/some_dir/logs/manual/some_script_20110216_1628.log
As a matter of laziness and keeping the log files consistently named, I'd like to create a function to pipe it to so that it's doing all the naming

How I envision the command running

./ 2>&1 | myfunc
And what the logfile name should look like (and in the right directory)

I was thinking of adding a function to our profile to handle this.

Just in testing I was trying to stream line right on the command line, but I'm having some difficulty in getting the name of the script that is pushing data over the pipe.

Here is what I've tried


./ 2>&1 | tee $(cd ../logs/manual; pwd)/$0_$(date +%Y%m%d)-$(date +%H%M).log
but that created a file named


Can anyone offer any suggestion?

Thanks ahead of time!

z1p 02-16-2011 05:50 PM

Short answer is you can't. The command is just reading from stdin, so it doesn't know where the input is coming from. You may be able to figure out a way to pull it from the history, but its seems like create a script of a function that takes a script name and runs it naming the output log based on the script name would be simpler.

KaosX 02-17-2011 09:54 AM

Thanks for the reply - before I had even read it I was trying to mess with "!!" and "!$" to get what I needed but that seems to not be working the way I'd liked. I might be able to edit the scripts to export their name or something as well...a wrapper script was my first idea - it was met with opposition from the more senior members of my group...ugh.

druuna 02-17-2011 10:27 AM


It is possible to create a function, but you need to be a bit creative:


function myfunct() {
./$ScriptName 2>&1 | tee ${LogDir}/${ShortName}_$(date +%Y%m%d)-$(date +%H%M).log

You execute it as follows: myfunct

Hope this helps.

All times are GMT -5. The time now is 08:24 PM.