The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's manas6 UNIX for Dummies Questions & Answers 0 06-05-2008 03:44 AM
capture ftp return code..PLZ HELP anju Shell Programming and Scripting 3 04-03-2008 07:08 AM
asking about return code naamas03 Shell Programming and Scripting 3 08-28-2007 01:53 AM
return code of a process filedeliver High Level Programming 1 04-18-2007 10:42 PM
Return Code of tar in AIX dupeng AIX 3 02-22-2004 09:05 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-17-2005
Registered User
 

Join Date: Feb 2005
Location: Columbus OH
Posts: 133
Return Code of multiple inner executions

Need some clarification on two topics and I apologize for the long post.

Topic 1). I have a PERL script which sends output to the console. This is executed like a daemon script. I would like to capture the "print" commands of the script to a log file. How can this be done.

Topic 2) Within the same PERL program I have a call to a Shell script whose return status code is captured. However within the Shell script the last line of execution is a call to java program as "java java_pgm config_file arg_1"
Within the java program, an explicit exit status of "1" is returned if there are any issues encountered in the java program execution; else "0" is returned.
The issue is when the java program has encounters exception. In this situation the shell script returned a value of 256 and the java program returned the following messages to the console.
javax.mail.internet.AddressException: Illegal whitespace in address in string ``. .''
at javax.mail.internet.InternetAddress.checkAddress(InternetAddress.java(Compiled Code))
at javax.mail.internet.InternetAddress.parse(InternetAddress.java(Compiled Code))
at javax.mail.internet.InternetAddress.parse(InternetAddress.java:529)
at javax.mail.internet.InternetAddress.parse(InternetAddress.java:506)
at com.xxxxx.utilities.emailnotifier.EmailNotifier.getMailMessage(EmailNotifier.java:274)
at com.xxxxx.utilities.emailnotifier.EmailNotifier.main(EmailNotifier.java:75)
Any ideas as to how to why a value of 256 was returned as opposed to either a "1" or a "0". I did not write the java program. Thanks

Last edited by jerardfjay; 05-17-2005 at 10:14 AM. Reason: remove specific
Reply With Quote
Forum Sponsor
  #2  
Old 05-17-2005
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,644
Quote:
In this situation the shell script returned a value of 256 and the java program Any ideas as to how to why a value of 256 was returned as opposed to either a "1" or a "0". I did not write the java program. Thanks

This is because the java application is exiting due to an uncaught exception, and never encounters the exit(1); statement in it's code. In short it's baddly written code.
Reply With Quote
  #3  
Old 05-17-2005
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
For Q #1 -
redirect the output to a logfile like this
Code:
perl myscript.pl > ~/logfile
"~/" is Korn shell for home directory
Reply With Quote
  #4  
Old 05-18-2005
Registered User
 

Join Date: Feb 2005
Location: Columbus OH
Posts: 133
Quote:
Originally Posted by jim mcnamara
For Q #1 -
redirect the output to a logfile like this
Code:
perl myscript.pl > ~/logfile
"~/" is Korn shell for home directory
I was hoping there would be something similar in PERL to perform the redirection within the program instead of actually using the command line redirection (for example we can use exec >> $LOGFILE 2>&1 in shell programming). Is there any such equivalent to redirect STDOUT and STDERR to a logfile within PERL program? Please advise. Thanks.

Jerardfjay

Last edited by jerardfjay; 05-18-2005 at 04:28 AM. Reason: better reason for request
Reply With Quote
  #5  
Old 05-20-2005
Registered User
 

Join Date: Feb 2005
Location: Columbus OH
Posts: 133
Quote:
Originally Posted by jerardfjay
I was hoping there would be something similar in PERL to perform the redirection within the program instead of actually using the command line redirection (for example we can use exec >> $LOGFILE 2>&1 in shell programming). Is there any such equivalent to redirect STDOUT and STDERR to a logfile within PERL program? Please advise. Thanks.

Jerardfjay
There is a way to do this in PERL. You could open a log file for writing. and since the default file handle for print commands is always the currently selected file handle, you can use the select the file handle for the log file before you use your print commands. This way you are directing all of your print command outputs to the log file instead of the console.

open (LOGFILE, $logfile);
select (LOGFILE);

print 1;
.
.
print 2;
.
.

The only issue concern that I have for this method is that, I don't believe that STDERR will be redirected to the logfile. Note to keep in mind is that "select (Filehandle)" is a stick operation. It works well in this situation, however if you have a need to make interaction with the user from within the program you have to explicitly specify select (STDOUT) to redirect output to console again.

Jerardfjay

Last edited by jerardfjay; 05-20-2005 at 05:00 AM. Reason: explained the select operator
Reply With Quote
  #6  
Old 05-20-2005
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,477
There is I/O redirection in Perl. Try this:

Code:
#!/usr/bin/perl
{
local *STDOUT;
local *STDERR;

open FILE, ">>log.txt" || die "Cannot write to log file!";
open STDOUT, ">&FILE" || die "Cannot redirect STDOUT";
open STDERR, ">&FILE" || die "Cannot redirect STDOUT";

print STDOUT "A\n";
print STDERR "B\n";
}
print STDOUT "C\n";
print STDERR "D\n";
This will redirect STDOUT and STDERR to FILE. Note that outside the block, the STDOUT and STDERR will be restored, because the filehandles in the block are local.
Reply With Quote
  #7  
Old 05-23-2005
Registered User
 

Join Date: Feb 2005
Location: Columbus OH
Posts: 133
Quote:
Originally Posted by cbkihong
This will redirect STDOUT and STDERR to FILE. Note that outside the block, the STDOUT and STDERR will be restored, because the filehandles in the block are local.
Thanks cbkihong. I executed your piece of Code and the got the following output.

C
D

and the contents of log.txt provided

B
A

Why, is the order of the log file in reverse eventhough the order of the print commands is A and then B. Also I did a chmod -w on the current directory and tried your snippet. I was either expecting to see "Cannot write to log file" on the console, however the only output that I got was

C
D

As always your feedback is greatly appreciated. Thanks.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0