Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-16-2011, 04:38 PM   #1
LQ Newbie
Registered: Feb 2011
Posts: 4

Rep: Reputation: 0
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!
Old 02-16-2011, 05:50 PM   #2
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
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.
Old 02-17-2011, 09:54 AM   #3
LQ Newbie
Registered: Feb 2011
Posts: 4

Original Poster
Rep: Reputation: 0
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.
Old 02-17-2011, 10:27 AM   #4
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401

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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
kill a process using pipe Hi_This_is_Dev Linux - General 12 06-08-2011 10:03 AM
[SOLVED] How do i process the output of a locate command, pipe it to what? vbekker Linux - Newbie 2 09-30-2010 08:46 PM
Can't connect to pipe process addr1 Arch 0 12-14-2008 07:15 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:35 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration