Operating System - HP-UX
1834135 Members
2559 Online
110064 Solutions
New Discussion

Re: sh-posix and ksh feature or bug?

 
SOLVED
Go to solution
Klaus Crusius
Trusted Contributor

sh-posix and ksh feature or bug?

The following shell script behaves nondeterministic under HPUX11.
Sometimes the function returns 0 (as expected) and sometimes it returns 129. Problem seems to be, that the execution of the "echo $? >$tmpfile" command is perfomed (sometimes) after the $tempfile is read after the pipe command.
My understanding of the execution of the pipe command was, that all processes of the pipe have to be finished, before the next statement is stating execution. Am I wrong with this assumption, or is there a bug in the implementation of ksh and sh-posix. The problem did not show with the old Bourne shell or with bash.


#! /usr/bin/sh
# write to logfile and also to stderr, return exit code of command
logandshow()
{
set -x
tmpfile="$(mktemp -c -p ers)"
logfile="$1"
shift
{ "$@"; echo "$?">$tmpfile; } | tee -a "$logfile"
RC=`cat "$tmpfile"`
RC=${RC:-129}
set +x
return $RC
}

logandshow "shelltest.logfile" sleep 1
There is a live before death!
5 REPLIES 5
harry d brown jr
Honored Contributor

Re: sh-posix and ksh feature or bug?

Klaus,

change this line

{ "$@"; echo "$?">$tmpfile; } | tee -a "$logfile"

to this

{ "$@"; echo "$?">$tmpfile; } | tee -a "$logfile";sleep 1

That way you force the "tee" to COMPLETE before proceeding to next line

live free or die
harry
Live Free or Die
Klaus Crusius
Trusted Contributor

Re: sh-posix and ksh feature or bug?

the problem is the completion of the echo, though. Of course the sleep will give the echo a godd chance to complete as well. But can we rely on that?
There is a live before death!
harry d brown jr
Honored Contributor

Re: sh-posix and ksh feature or bug?


I think its related to the buffering of output.

This works:

{ "$@"; echo "$?">$tmpfile | tee -a "$logfile"; }


by moving the "tee" and attaching it to the "echo" statement.


live free or die
harry
Live Free or Die
Dietmar Konermann
Honored Contributor
Solution

Re: sh-posix and ksh feature or bug?

Well, that's interesting. Just had a discussion here with a colleague about this...

It's of course a race, but we don't believe that's a shell bug...

The left side of the pipe (the echo command) redirects stdout (from where the pipe is reading) to a file. In this case the pipe may complete before the echo is finished.

This works:

{ "$@"; echo "$?">$tmpfile; true; } | tee -a "$logfile"

Regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Klaus Crusius
Trusted Contributor

Re: sh-posix and ksh feature or bug?

Thanks for you interest. I tried also your solution, which worked ok, but I had no idea, if it was reliable.
So I introduced a wait after the pipeline, which worked as well.

In the meanwhile I found a statement in the man pages of sh-posix.

"If the pipeline is not in the background, the shell will wait for the last command specified in the pipeline to complete, and may also wait for all commands to complete."

I think, that clarifies the the HPUX implementations are within specs. Although it would be a nice feature, if the shells waited for all commands to complete.
There is a live before death!