> My purpose is to pass the $? value (unchanged) from the wrapper > function "runcmd" to its caller so the caller can tell if the command > exited normally or it was killed by a signal.
In the general case passing values "unchanged" is not possible since in the exit code context you have a limited "bandwidth" of 8 bit (0..255). So you will have to map exit codes and signals onto that available range if you want to pass both through that single exit code channel. Mind that bash can also pass just 256 values through the exit code. So if all you want is to imitate bash behaviour (or ksh88 behaviour) the subtraction of 128, as previously proposed, may fit your needs. ________________________________ > From: [email protected] > Date: Mon, 2 Jul 2012 16:42:14 +0800 > Subject: Re: [ast-users] [ksh93] How to return $? from a function when > $? > 256 > To: [email protected] > CC: [email protected] > > On Mon, Jul 2, 2012 at 4:19 PM, Janis Papanagnou > <[email protected]<mailto:[email protected]>> > wrote: > > If you are speaking of a problem it would be helpful if you'd exactly specify > the conditions you expect; and why. A reference to bash behaviour seems not > to be an appropriate measure. > > You're right. It's always a challenge for me to make myself understood > right in English. I mentioned Bash here just to help others understand > my problem. > > The question is, what do you expect as a possible range of exit codes, and > in what way do you want to do the signalling of exit codes and/or signals. > > If, in your applications context, you don't have exit codes >128 then just > subtract 128, as in > (( rc >= 128 )) && (( rc -= 128 )) > return $rc > but then you wouldn't be able to distinguish signals from exit status. > > My purpose is to pass the $? value (unchanged) from the wrapper > function "runcmd" to its caller so the caller can tell if the command > exited normally or it was killed by a signal. > > ________________________________ > > From: [email protected]<mailto:[email protected]> > > Date: Mon, 2 Jul 2012 11:01:59 +0800 > > Subject: [ast-users] [ksh93] How to return $? from a function when $? > > 256 > > To: [email protected]<mailto:[email protected]>; > [email protected]<mailto:[email protected]> > > CC: > > > > For example, following code is very common in scripts: > > > > ### CODE BEGIN ### > > function debug > > { > > return > > } > > > > function runcmd > > { > > typeset cmd=$1 > > typeset rc > > > > debug "+++ $cmd" > > eval "$cmd" > > rc=$? > > if (( rc )); then > > debug "+++ $cmd failed with $rc" > > else > > debug "+++ $cmd succeeded" > > fi > > > > return $rc > > } > > > > runcmd '/usr/bin/sleep 15' > > echo $? > > ### CODE END ### > > > > The problem is when the command is killed by a signal, we cannot return > > the correct $? (> 256) from the runcmd() function. Bash does not has > > this problem since $? is equal to 128+signal when a command is killed. > > > > _______________________________________________ ast-users mailing list > > [email protected]<mailto:[email protected]> > > https://mailman.research.att.com/mailman/listinfo/ast-users > > _______________________________________________ ast-users mailing list [email protected] https://mailman.research.att.com/mailman/listinfo/ast-users
