LinuxQuestions.org
Visit Jeremy's Blog.
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 07-12-2005, 11:13 AM   #1
mhcueball2
LQ Newbie
 
Registered: Jul 2005
Posts: 5

Rep: Reputation: 0
ksh - get stdout name as variable


Writing a ksh script. If someone starts a process with:
test.ksh > date.log

How can I grab 'date.log' name as a variable in test.ksh?

Reason: In test.ksh, I check the logfile for errors. If errors are found, I kill the process. The date.log always changes (and it isn't always a date). So I need to get that date.log as a variable...without entering something like test.ksh date.log > date.log

I tried 'echo $stdout' ...but nothing.
 
Old 07-12-2005, 11:33 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
try this:
Code:
stdout=$(lsof -ad1 -p $$ -Fn | sed -n '/n/s///p')
 
Old 07-12-2005, 11:51 AM   #3
mhcueball2
LQ Newbie
 
Registered: Jul 2005
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks for the help...but:

lsof : not found
 
Old 07-12-2005, 12:26 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Tell more about the system you're using.
 
Old 07-12-2005, 02:00 PM   #5
mhcueball2
LQ Newbie
 
Registered: Jul 2005
Posts: 5

Original Poster
Rep: Reputation: 0
Actually, I don't know much about it either...I'm not an admin (and its at a remote location).

But its a AIX risc box, and I just have korn shell access (88 variant).

Last edited by mhcueball2; 07-12-2005 at 02:05 PM.
 
Old 07-12-2005, 03:44 PM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
If you're allowed to install freeware on this system, try downloading lsof for aix from, for example:
ftp://aixpdslib.seas.ucla.edu/pub/lsof/RISC/
 
Old 07-12-2005, 03:57 PM   #7
mhcueball2
LQ Newbie
 
Registered: Jul 2005
Posts: 5

Original Poster
Rep: Reputation: 0
I tried...but since I'm not an admin, I can't put that in the bin.

There's gotta be a better solution to this.
 
Old 07-12-2005, 04:06 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
I guess you can put the lsof binary under your home directory, but it may ask for root privileges ...

Quote:
There's gotta be a better solution to this.
I don't think so.

The shell has no way to know to what is directed its stdout. The only thing it can check is wether stdout is a file or a terminal (test -t).

Perhaps does a standard utility exists with AIX, Solaris 10 has "pfiles" which does a similar work than lsof.
 
Old 07-15-2005, 10:23 AM   #9
mhcueball2
LQ Newbie
 
Registered: Jul 2005
Posts: 5

Original Poster
Rep: Reputation: 0
OK...nothing yet.

How about getting the stdout content as a variable...so I can hit it like that, or a way of copying during the process?
 
Old 07-15-2005, 11:15 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Can you clarify your request, I'm confused by your last question ...
 
Old 07-15-2005, 11:36 AM   #11
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
I don't know about AIX, but in my Unix (RedHat 8.0), /proc/X/fd shows all file descriptors for process X as symbolic links. You want process $$, and stdout is fd 1. Demonstration:
Code:
perl -we 'warn `ls -l /proc/$$/fd/1`' > tmpdidu
produces
Code:
l-wx------    1 brech    users          64 Jul 15 11:34 /proc/5386/fd/1 -> /home/brech/tmp/tmpdidu
Now you need to determine the target of a symlink in ksh. You could ls -l /proc/$$/fd/1 | cut -d\> -f2- | tail +2c , but I'm sure there are more elegant ways than forking off three external processes (ls, cut, tail).

I'm not sure I understand the reason why you want to do this. It might be most portable to pass the destination file as an argument, and in the script change every command generating output from, e.g.,
Code:
echo Hello
to
Code:
echo Hello >> $1
Good luck!
 
Old 07-16-2005, 11:50 AM   #12
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
The problem is that your script has no knowledge of your output file's name - it's just standard output. The shell has done the redirection before it runs the script. As suggested above, add an output parameter requirement to the script, and test that stdout is a tty (test -t 1) before it runs.
 
  


Reply


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
what is stdout stefaandk Linux - Newbie 1 09-06-2005 08:20 PM
stdout stdin Furlinastis Linux - Newbie 3 08-12-2005 12:00 AM
[KSH-Regular Expressions] How can I specify Repition patterns in a variable? Aziz Programming 0 11-02-2004 02:01 PM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 04:19 AM
stdout with tk? sk8guitar Programming 1 07-30-2003 03:48 PM


All times are GMT -5. The time now is 02:40 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration