in reply to Re^2: How to know the status of a command invoked by open function?
in thread How to know the status of a command invoked by open function?

[ Please don't use <pre>...</pre>. Use <p> at the start of paragraphs and use <c>...</c> around computer text (code, data, output, etc). ]

why the following two opens have different result?

open(PS,"ls . 2>/dev/null |")
is equivalent to
open(PS, '-|', /bin/sh', '-c', 'ls . 2>/dev/null ')
but
open(PS,"ls . |")
get optimised to
open(PS, '-|', 'ls', '.')

Also see the update to my original reply.

But how can i get the exit status for the command in this case?

You already know how. It's in $? after a successful close. In this case, it's 141. The shell returns 141 (128|SIGPIPE) when its child dies from SIGPIPE.

Replies are listed 'Best First'.
Re^4: How to know the status of a command invoked by open function?
by zhujian0805 (Sexton) on Aug 16, 2011 at 01:21 UTC

    it's weird that i get return code '0' when i run my script on AIX.

      Yes, very weird. You'd get zero if ls managed to finish before close closed the pipe, but that's not very likely. Or maybe AIX processes in general or AIX's ls specifically behave differently when they encounter a closed pipe. Or maybe the shell doesn't relay the death of the child to its parent in AIX.

      On AIX, what output do you get from

      /bin/sh -c 'perl -e'\''kill TERM => $$'\''' ; echo $?
        # /bin/sh -c 'perl -e'\''kill TERM => $$'\''' ; echo $? Terminated 143