Return Code of multiple inner executions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Return Code of multiple inner executions
# 1  
Old 05-17-2005
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 02:14 PM.. Reason: remove specific
# 2  
Old 05-17-2005
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.
# 3  
Old 05-17-2005
For Q #1 -
redirect the output to a logfile like this
Code:
perl myscript.pl > ~/logfile

"~/" is Korn shell for home directory
# 4  
Old 05-18-2005
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 08:28 AM.. Reason: better reason for request
# 5  
Old 05-20-2005
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 09:00 AM.. Reason: explained the select operator
# 6  
Old 05-20-2005
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.
# 7  
Old 05-23-2005
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. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run script every minute and terminate after specified number of executions

when it runs and look at my acron.log file it generates an error as below /tmp/prog.sh: line 4: (12 Replies)
Discussion started by: azherkn3
12 Replies

2. UNIX for Advanced & Expert Users

How to increase executions speed of loops.?

(2 Replies)
Discussion started by: Venkatesh1
2 Replies

3. Shell Programming and Scripting

variable value return of multiple command

I wrote this script #!/bin/bash var=`du -sch /var/log/messages;du -sch /var/log/maillog` echo $var I am getting result as follows. # sh my.sh 2.1M /var/log/messages 2.1M total 296K /var/log/maillog 296K total I need it like below 2.1M /var/log/messages 296K... (3 Replies)
Discussion started by: anilcliff
3 Replies

4. Shell Programming and Scripting

Pausing between two executions in a shell script

HI, Please help me on this. I have to execute 100 scripts which i have redirected in to a file . I want to pause the script after first execution and once i say enter key word it has to go to next execution. My looks like for RUNFILES in `cat ${SOURCEFILES}/scripts` do echo $RUNFILES; ... (1 Reply)
Discussion started by: srichunduru
1 Replies

5. Shell Programming and Scripting

Need Multiple Return Values

Hi, I need to retrun multiple values function errorFileCreation { echo "Before" return -1 "Siva"; echo "Aftyer" } echo ${?} - This can be used to getting first value. how can i get second one. Advance Thanks... Shiv (3 Replies)
Discussion started by: rsivasan
3 Replies

6. Shell Programming and Scripting

Can $? return multiple values?

Hi, I have a script which does something like the below: execute_some_script.sh $arg1 $arg2 `exec-some-cmd` if then; do something else do something else fi However, during some cases, there is an error saying: line xxx: [: too many arguments at the line number which has... (5 Replies)
Discussion started by: laloo
5 Replies

7. Shell Programming and Scripting

return code of multiple java process

Hi, I have a unix shell script which is launching multiple java processes by calling a java class in a loop, but each time with a different set of parameters. Now I have to use the return code from each process in the script later. but how do i obtain the return code from each process... (1 Reply)
Discussion started by: rama354
1 Replies

8. Shell Programming and Scripting

checking for return status between multiple commands

i have to run set of commands command1 command2 command3 command4 Now Whenever any of these command fails i should quit while capturing error message. Is there a better way then checking for $? after each command. (1 Reply)
Discussion started by: vickylife
1 Replies

9. Shell Programming and Scripting

Gen. Question - Script calls multiple programs - Return Code Handling?

General Question: If a script calls multiple external programs (external to the script, but still on unix), where do the return codes go? Let's say one of external programs fails, does the entire script fail and send a non-zero return code to the job scheduling software, or is the return code sent... (1 Reply)
Discussion started by: jnanasakti
1 Replies

10. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies
Login or Register to Ask a Question