Return Code of multiple inner executions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Return Code of multiple inner executions
# 8  
Old 06-20-2005
Quote:
Originally Posted by jerardfjay
Why, is the order of the log file in reverse eventhough the order of the print commands is A and then B.:
Sorry .. i didn't see the post until today. Hopefully you will still remember this.

As STDOUT and STDERR are duped filehandles of FILE, you can treat them as two separate pipes to the output file despite they end up being connected to the same output file, and the two channels are not necessarily synchronized.

Recall that Perl IO is buffered if you use the print() function. Data written to filehandles using these functions are not written immediately but buffered independently. So although the write is sequential, the resulting sequence may be in any interleaving order.

The only way I can think of to force the ordering is to wrap the logging routine inside an exclusive lock with flock(). When the lock is released, Perl will be forced to flush the filehandle, so the correct order will always be preserved by blocking out-of-order writes:

Code:
#!/usr/bin/perl

sub toLog {
 local *HANDLE = shift;
 my $msg = shift;
 flock HANDLE, Fcntl::LOCK_EX;
 print HANDLE $msg;
 flock HANDLE, Fcntl::LOCK_UN;
}

{
 local *STDOUT;
 local *STDERR;
 local *FILE;

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

 toLog(STDOUT, "A\n");
 toLog(STDERR, "B\n");
}
print STDOUT "C\n";
print STDERR "D\n";

which on my system will now give the correct order instead of 'B', 'A'. I may be wrong in this regard given my limited knowledge in I/O in general and the way it is implemented in Perl. So if you have less clumsy ways to achieve this or would like to make corrections, do it.

Last edited by cbkihong; 06-20-2005 at 10:39 AM..
# 9  
Old 06-20-2005
Quote:
Originally Posted by cbkihong
which on my system will now give the correct order instead of 'B', 'A'. I may be wrong in this regard given my limited knowledge in I/O in general and the way it is implemented in Perl. So if you have less clumsy ways to achieve this or would like to make corrections, do it.
Thanks cbkihong. I shall try the options that you have provided. I probably will not understand your explanation until I have actually tried the various forms that you have suggessted. Thanks Jerardfay. Smilie
# 10  
Old 06-20-2005
If the previous method looks bizarre to you, here's another method discussed in the Perl FAQ:

Code:
#!/usr/bin/perl

use IO::Handle;

{
 local *STDOUT;
 local *STDERR;
 local *FILE;

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

 STDOUT->autoflush(1);
 STDERR->autoflush(1);

 print STDOUT "A\n";
 print STDERR "B\n";
}
print STDOUT "C\n";
print STDERR "D\n";

I have problems with this autoflush() on socket I/O so I tend to avoid it most of the time, but in this case it just seems to work quite well.

As always, there are a lot of ways to do the same thing in Perl. 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