LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-16-2011, 03:38 PM   #1
KaosX
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

Code:
./some_script.sh 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
Code:
./some_script.sh 2>&1 | myfunc
And what the logfile name should look like (and in the right directory)
Code:
some_script_20110216-1628.log
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

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

"bash_20110216-1628.log"

Can anyone offer any suggestion?

Thanks ahead of time!
 
Old 02-16-2011, 04:50 PM   #2
z1p
Member
 
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, 08:54 AM   #3
KaosX
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, 09:27 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

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

Code:
function myfunct() {
LogDir="/home/user/some_dir/logs/manual"
ScriptName=$1
ShortName=${ScriptName%.*}
./$ScriptName 2>&1 | tee ${LogDir}/${ShortName}_$(date +%Y%m%d)-$(date +%H%M).log
}
You execute it as follows: myfunct some_script.sh

Hope this helps.
 
  


Reply



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 09:03 AM
[SOLVED] How do i process the output of a locate command, pipe it to what? vbekker Linux - Newbie 2 09-30-2010 07:46 PM
Can't connect to pipe process addr1 Arch 0 12-14-2008 06:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 PM.

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