Subject: Re: sh problem
To: Patrick Welche <prlw1@newn.cam.ac.uk>
From: Andrew Brown <atatat@atatdot.net>
List: current-users
Date: 01/25/2000 00:38:17
--7JfCtLOvnd9MIVvH
Content-Type: text/plain; charset=us-ascii
>> > It isn't clear to me what ! should do with the exit status of a
>> > subshell -
>...
>> (How the exit status of the subshell relates to the exit status of
>> anything it runs is another question. I imagine it's well-defined....)
>
>Exactly ;)
that's easy! it's just like a shell script.
in a shell script, unless you exit with a specific return code, the
shell exits with the return code of the last process executed (weirdly
like perl in a macabre way).
so...
( cmd1 ; cmd2 )
cmd1 ; cmd2
( cmd1 | cmd2 )
cmd1 | cmd2
all have the same exit code: that of cmd2. therefore, the shell
! cmd1 [ | cmd2 ... ]
(a) should have the exit code of the last process in the pipeline (one
process is a one stage pipeline), and (b) should be so, regardless of
the presence of parentheses.
try the attached shell script. for me it only prints 2s.
--
|-----< "CODE WARRIOR" >-----|
codewarrior@daemon.org * "ah! i see you have the internet
twofsonet@graffiti.com (Andrew Brown) that goes *ping*!"
andrew@crossbar.com * "information is power -- share the wealth."
--7JfCtLOvnd9MIVvH
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=l
#!/bin/sh
cat << 'EOF' > exitwhat
( sh -c 'exit 1' ; sh -c 'exit 2' ); echo $?
sh -c 'exit 1' ; sh -c 'exit 2'; echo $?
( sh -c 'exit 1' | sh -c 'exit 2' ); echo $?
sh -c 'exit 1' | sh -c 'exit 2'; echo $?
EOF
echo "trying sh..."; sh exitwhat
echo "trying bash..."; bash exitwhat
echo "trying ksh..."; ksh exitwhat
echo "trying zsh..."; zsh exitwhat
echo "trying csh..."; sed 's/?/status/' exitwhat | csh -f
echo "trying tcsh..."; tcsh -f exitwhat
rm exitwhat
--7JfCtLOvnd9MIVvH--