bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "exit" doesn't work from inside of PIPED read block


From: Bob Proulx
Subject: Re: "exit" doesn't work from inside of PIPED read block
Date: Mon, 19 Nov 2007 10:19:36 -0700
User-agent: Mutt/1.5.13 (2006-08-11)

Please keep the mailing list in the CC so that others may participate
in the discussion and enable it to be available in the archives.

> #!/bin/bash -p

Why are you using the -p option?

> echo $$
> cat /tmp/atf | while read; do echo $$; exit 3; done

In both cases the $$ is expanded by the shell before invoking the
command.  So they get the same pid value, the pid of the parent.

I don't know how to output the pid of the subshell.  Perhaps someone
on the mailing list will have an answer.

You can verify that there is a subshell however.

  echo $$ ; echo foo | sleep 123
  8924
  ...sleep 123 now running...

In another terminal look at the processes running.  I used 'ps -efH'.
Look for pid 8924.

  rwp       8924  8923  0 10:10 pts/36   00:00:00     bash
  rwp       1016  8924  0 10:13 pts/36   00:00:00       sleep 123

Bob


Blaine Simpson wrote:
> Thanks for taking the time to answer (both), Bob.  Inline reply below.
> 
> Bob Proulx wrote:
> > Blaine Simpson wrote:
> >   
> >>    "exit" doesn't exit the current shell when inside of PIPED read blocks,
> >>     yet everything works find if the input comes from "redirection".
> >>     
> >
> > This is because pipes operate in subshells and the exit applies to the
> > subshell.  Please see the bash FAQ question E4 for more information.
> >
> >   ftp://ftp.cwru.edu/pub/bash/FAQ
> >
> >   
> >>     cat /tmp/atf | while read; do exit 3; done  # for any text file 
> >> /tmp/atf
> >>     Yet the following works
> >>     cat /tmp/atf | while read; do exit 3; done
> >>     
> >
> > Those two are the same.  You probably meant to say:
> >
> >   while read; do exit 3; done < /tmp/atf
> >   
> Exactly.  Copy and paste error from the file where my notes are.
> > No pipeline there and so that is done in the current shell.
> >   
> I thought I had eliminated shelling as the cause because I echo'd $$ in
> the body (where "exit 3" is above), and it is the same exact pid as the
> root shell.
> 
> I'm attaching the test script.
> > Bob
> >   

> #!/bin/bash -p
> 
> echo $$
> cat /tmp/atf | while read; do echo $$; exit 3; done
> 
> # WORKS:
> #while read; do exit 3; done < /tmp/atf
> 
> echo "Post in $$"




reply via email to

[Prev in Thread] Current Thread [Next in Thread]