It is currently Thu, 20 Jan 2022 04:16:11 GMT



 
Author Message
 Bash shell is always returning last exit status of the pipe
I have a bash shell script like

#! /bin/bash
foo=0
{
     false
     foo=$?
echo foo is $foo

and it always prints out foo as 0, the exit status of the "tee" command.
If I remove the pipe, it prints out foo as 1. How can I get it to give
me the exit status of the "false" command (in real life this command
isn't "false"!)



 Mon, 14 Aug 2006 08:11:26 GMT   
 Bash shell is always returning last exit status of the pipe
In article <PQa%b.9477$qL1.6561@fed1read02>,
 Scott Simpson <ssimp...@visionsolutions.com> wrote:

When you use a pipeline, the commands (except for the last one in the
pipeline) are executed in subshells, so variable assignments don't
persist.

The solution is to write the value to a file and read it after the
pipeline is completed.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***



 Mon, 14 Aug 2006 08:46:40 GMT   
 Bash shell is always returning last exit status of the pipe
On Wed, 25 Feb 2004 19:46:40 -0500, Barry Margolin <bar...@alum.mit.edu>
wrote:

AFAIK there's no such exception about last command in pipeline.

--
A. Alper Atycy
PGP key @ http://pks.gpg.cz:11371/pks/lookup?op=index&search=0xB824F550



 Mon, 14 Aug 2006 09:46:33 GMT   
 Bash shell is always returning last exit status of the pipe
On Wed, 25 Feb 2004 16:11:26 -0800, Scott Simpson

No, it's printing the value you've assigned before the pipeline.

The following is an excerpt from Bash reference:
"Each command in a pipeline is executed in its own subshell."

So you basically have to ponder over passing values from subshell.

But, I'd recommend:
Whatever you want to do with the return value, do it while in the pipeline,
i.e. within braces.

--
A. Alper Atycy
PGP key @ http://pks.gpg.cz:11371/pks/lookup?op=index&search=0xB824F550



 Mon, 14 Aug 2006 09:53:55 GMT   
 Bash shell is always returning last exit status of the pipe
In article <ksjq3093arjfhpt6hhdbbt178du72o4...@4ax.com>,
 A. Alper ATICI <alper.aticiST...@softhome.net> wrote:

It's not *required* by POSIX, but most implementations execute the last
command in the main shell process.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***



 Mon, 14 Aug 2006 09:54:29 GMT   
 Bash shell is always returning last exit status of the pipe
On Wed, 25 Feb 2004 20:54:29 -0500, Barry Margolin <bar...@alum.mit.edu>
wrote:

... and as for Bash, which the OP has asked about?

--
A. Alper Atycy
PGP key @ http://pks.gpg.cz:11371/pks/lookup?op=index&search=0xB824F550



 Mon, 14 Aug 2006 10:09:16 GMT   
 Bash shell is always returning last exit status of the pipe

    AFAIK, only ksh88 and ksh93. All the rest execute all commands in
    a pipeline in a subshell.

--
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License



 Mon, 14 Aug 2006 10:24:17 GMT   
 Bash shell is always returning last exit status of the pipe

Excellent suggestion. I was confused because I thought that pipes I
executed in curly braces didn't spawn a subshell but those within
parentheses were spawned in a subshell.

On another note, these semantics suck. By virtue of adding a pipe
symbol, my namespace changes. Geeesh.



 Mon, 14 Aug 2006 10:26:30 GMT   
 Bash shell is always returning last exit status of the pipe
In article <403D5956.5080...@visionsolutions.com>,
 Scott Simpson <ssimp...@visionsolutions.com> wrote:

Curly braces by themselves don't cause a subshell to be used, but
pipelines do.

It pretty much *has* to be that way.  In order to execute two commands
concurrently, they have to be in separate processes.  So both commands
in a pipeline can't possibly be executed in the original shell process.

Perhaps you could make use of bash's $PIPESTATUS array, which contains
the termination status of each command in the last pipeline.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***



 Mon, 14 Aug 2006 13:40:42 GMT   
 Bash shell is always returning last exit status of the pipe
On Wed, 25 Feb 2004 16:11:26 -0800, Scott Simpson
In Bash there is an array called PIPESTATUS.

--
Incrsease your earoning poswer and gaerner profwessional resspect.
Get the Un1iversity Dewgree you have already earned.
 [from the prestigious, non-accredited University of Spam!]



 Mon, 14 Aug 2006 15:23:06 GMT   
 Bash shell is always returning last exit status of the pipe

: In article <ksjq3093arjfhpt6hhdbbt178du72o4...@4ax.com>,
:  A. Alper ATICI <alper.aticiST...@softhome.net> wrote:
:
: > On Wed, 25 Feb 2004 19:46:40 -0500, Barry Margolin <bar...@alum.mit.edu>
: > >
: > >When you use a pipeline, the commands (except for the last one in the
: > >pipeline) are executed in subshells,
: >
: > AFAIK there's no such exception about last command in pipeline.
:
: It's not *required* by POSIX, but most implementations execute the last
: command in the main shell process.

Bash, ash, pdksh and Bourne sh's execute all statements in a pipeline in separate
subshells.  Real ksh (ksh88 and ksh93), zsh and ksh derived posix shells,  like
HP-UX's,  execute the last statement in the current process.  Obviously,  this has
meaning only if the last command is a builtin - i.e. read.  Had POSIX required this
it would have saved a lot of people a lot of work.

Dan Mercer

:
: --
: Barry Margolin, bar...@alum.mit.edu
: Arlington, MA
: *** PLEASE post questions in newsgroups, not directly to me ***



 Tue, 15 Aug 2006 00:11:00 GMT   
 Bash shell is always returning last exit status of the pipe

"Chris F.A. Johnson" <c.fa.john...@rogers.com> wrote in message news:c1jlcg$1j5fm4$1@ID-210011.news.uni-berlin.de...
: On Thu, 26 Feb 2004 at 01:54 GMT, Barry Margolin wrote:
: > In article <ksjq3093arjfhpt6hhdbbt178du72o4...@4ax.com>,
: >  A. Alper ATICI <alper.aticiST...@softhome.net> wrote:
: >
: >> On Wed, 25 Feb 2004 19:46:40 -0500, Barry Margolin <bar...@alum.mit.edu>
: >> >
: >> >When you use a pipeline, the commands (except for the last one in the
: >> >pipeline) are executed in subshells,
: >>
: >> AFAIK there's no such exception about last command in pipeline.
: >
: > It's not *required* by POSIX, but most implementations execute the last
: > command in the main shell process.
:
:     AFAIK, only ksh88 and ksh93. All the rest execute all commands in
:     a pipeline in a subshell.

Zsh and ksh derived posix shells,  like HP's,  do as well.  So of major sh
implementations,  only bash,  original (obsolescent) Bourne and Berkeley
derived sh's have this crummy behavior.

Dan Mercer
:
: --
:     Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
:     ===================================================================
:     My code (if any) in this post is copyright 2004, Chris F.A. Johnson
:     and may be copied under the terms of the GNU General Public License



 Tue, 15 Aug 2006 00:13:18 GMT   
 Bash shell is always returning last exit status of the pipe
On Wed, 25 Feb 2004 18:26:30 -0800, Scott Simpson

That symbol actually relates to an IPC mechanism called pipe,
and that is inter-process (not intra-process).

--
A. Alper Atycy
PGP key @ http://pks.gpg.cz:11371/pks/lookup?op=index&search=0xB824F550



 Tue, 15 Aug 2006 02:22:59 GMT   
 Bash shell is always returning last exit status of the pipe

I know processes must be spawned to make a pipe (and you use the pipe(2)
system call), but what wasn't clear to me was why you couldn't access
all the exit statuses (since the shell just does a wait(2) and the shell
gets all the exit statuses). Fortunately, as stated before, you can use
the PIPESTATUS variable.

What isn't clear to me is why you need to use a subshell (that is, the
shell spawns a shell which then itself spawns the pipe processes). Job
control reasons maybe?



 Tue, 15 Aug 2006 03:37:32 GMT   
 Bash shell is always returning last exit status of the pipe
%% Scott Simpson <ssimp...@visionsolutions.com> writes:

  ss> A. Alper ATICI wrote:

  >> That symbol actually relates to an IPC mechanism called pipe, and that
  >> is inter-process (not intra-process).

  ss> I know processes must be spawned to make a pipe (and you use the pipe(2)
  ss> system call), but what wasn't clear to me was why you couldn't access
  ss> all the exit statuses (since the shell just does a wait(2) and the shell
  ss> gets all the exit statuses).

Yes the shell knows them, but how can it allow you to access them from
your script?  Traditional Bourne shell doesn't have arrays.  It would
have to be something bogus like $?1 is the exit status for the first
command, $?2 is the exit status of the second one, etc.

  ss> What isn't clear to me is why you need to use a subshell (that is,
  ss> the shell spawns a shell which then itself spawns the pipe
  ss> processes). Job control reasons maybe?

Nope, just physics! :)

What you wrote above is not quite correct.  What happens is that each
command in the pipeline is a process.  That process might be a program,
or it might be a subshell.  Whatever it is, it's a child process, and it
has to be.  Consider this pipeline:

  for a in 1 2 3 4 5 6; do echo $a; done | while read x; do echo $x; done

There is no way that the same parent shell can both run the first for
loop to print data to the pipe, and also run the second while loop to
read data from the pipe.  Unless you want to start inventing
multithreaded shells or something--yikes!

--
-------------------------------------------------------------------------------
 Paul D. Smith <psm...@nortelnetworks.com>   HASMAT--HA Software Mthds & Tools
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist
-------------------------------------------------------------------------------
   These are my opinions---Nortel Networks takes no responsibility for them.



 Tue, 15 Aug 2006 07:34:55 GMT   
 
   [ 17 post ]  Go to page: [1] [2]

Similar Threads

1. bash shell programming - script return without exit

2. exit status from remote shell to local shell

3. Q: bash shell always login shell?

4. problems in kernel 2.4.20 stat() seems to return always size zero for any fifo (named pipe)

5. last exit status

6. Test exit status of command prior to last command

7. exit status of not-last processes in a pipeline

8. ersh 3.0 - rsh frontend returning exit status of remote cmd (new)

9. ersh - rsh frontend hack returning exit status of remote cmd

10. Returning exit status


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software