exit status of the command in pipe from shell & named pipe

exit status of the command in pipe from shell & named pipe

Post by Shigeru IKED » Fri, 24 Jan 1997 04:00:00



-----BEGIN PGP SIGNED MESSAGE-----

I use nntpget command which comes with INN-1.5.1 for news article
sucking like the following. (actual script is more complex for the
error handling)

for i in $(cat /usr/lib/news/etc/subscriptions)
do
        nntpget -t '970101 000000' -n comp.os.linux.misc news | inject.pl
        if [ $? != 0 ]; then
                cleanup
        fi
done

Although when nntpget detects error it exits with exit(1), the
variable $? is always 0.

Q.1)
        How can I get exit status of the command in the pipeline
        from the shell?

Because I cannot find the solution, I tried to use the named pipe like
the following:

mkfifo /tmp/fifo$$
inject.pl < /tmp/fifo$$ &

for i in $(cat /usr/lib/news/etc/subscriptions)
do
        nntpget -t '970101 000000' -n comp.os.linux.misc news > /tmp/fifo$$
        if [ $? != 0 ]; then
                cleanup
        fi
done

This one seems to be able to detect the exit status of nntpget, but
frequently stop with unexpected exit status or seems deadlock.

Q.2)
        I misunderstand the usage of the named pipe, or Linux named pipe is
        broken?

If you write a followup, please CC: to my e-mail address too.

Here is my configuration:

Linux kernel-2.0.27
libc-5.4.17
bash-1.14.7
GNU fileutils 3.12

Thanks in advance.

-----BEGIN PGP SIGNATURE-----
Version: 2.6.3ia
Charset: noconv

iQCVAwUBMubpBEPp6IK9NdONAQGEXgQAr8iCMcZKI36N8g9wwhAigrdl8xWIR+jV
ILfmOKCy1jH8+rg0Xl/XqEZ62E/3CW66LK8E7k6Pig3HgQcZ+LmlRGcIBuTFGQi7
sdvNLDyY7ihjkxwONqtfzFuab2zJUDcf51iNAnhyh8c+g1lU7UkCLrlCnBKpByA6
o95l4+WFhwM=
=o57c
-----END PGP SIGNATURE-----

 
 
 

exit status of the command in pipe from shell & named pipe

Post by B.A.McCau.. » Sat, 25 Jan 1997 04:00:00



>for i in $(cat /usr/lib/news/etc/subscriptions)
>do
>    nntpget -t '970101 000000' -n comp.os.linux.misc news | inject.pl
>    if [ $? != 0 ]; then
>            cleanup
>    fi
>done

>Although when nntpget detects error it exits with exit(1), the
>variable $? is always 0.

Yes, the exit status of a pipeline is the exit status of the last
command.

Quote:>Q.1)
>    How can I get exit status of the command in the pipeline
>    from the shell?

In bash (Note: for readers outside comp.os.linux.*, bash is the
default Linux shell) you can have much finer control over your
plubbing using ">()" and "<()".

foo | bar

Becomes:

foo > >( bar )

There is a danger with this approach that the next command will be
executed before "bar" completes.

If you need the exit status from more than one command in the pipe
life becomes more tricky.

--

 .  _\\__[oo       from       | Phones: +44 121 471 3789 (home)

.  l___\\    /~~) /~~[  /   [ | PGP-fp: D7 03 2A 4B D8 3A 05 37...
 # ll  l\\  ~~~~ ~   ~ ~    ~ | http://wcl-l.bham.ac.uk/~bam/


 
 
 

exit status of the command in pipe from shell & named pipe

Post by Icarus Spar » Sat, 25 Jan 1997 04:00:00




Quote:

>for i in $(cat /usr/lib/news/etc/subscriptions)
>do
>    nntpget -t '970101 000000' -n comp.os.linux.misc news | inject.pl
>    if [ $? != 0 ]; then
>            cleanup
>    fi
>done

This program reads all of /usr/lib/news/etc/subscriptions into memory,
when only one line is needed at a time. It is better to write it as

while read i
do
        nntpget -t '970101 000000' -n comp.os.linux.misc news | inject.pl
        if [ $? != 0 ]; then
                cleanup
        fi
done < /usr/lib/news/etc/subscriptions

Quote:>Although when nntpget detects error it exits with exit(1), the
>variable $? is always 0.

>Q.1)
>    How can I get exit status of the command in the pipeline
>    from the shell?

The exit value of a pipeline is the exit value of the last command, for
all normal shells. I see that Bash2.0 now returns an array of exit
values, and of course 'rc' and 'es' always returned a list.

So you have to get involved with tricks. As you have a particular example,
we do not need the full power of the file redirection abilities of the shell.

exec >&3
while read i
do
        status=`( nntpget -t '970101 000000' -n comp.os.linux.misc news
                echo $? >&4 ) | inject.pl >&3 4>&1`
        if [ $status != 0 ]; then
                cleanup
        fi
done < /usr/lib/news/etc/subscriptions

 
 
 

exit status of the command in pipe from shell & named pipe

Post by Boyd Rober » Sat, 25 Jan 1997 04:00:00



Quote:>        nntpget -t '970101 000000' -n comp.os.linux.misc news | inject.pl

>Although when nntpget detects error it exits with exit(1), the
>variable $? is always 0.

I recall that the exit status of the pipeline is that of the last command.
inject.pl probably exits with 0 when it reads EOF.

--

``Not only is UNIX dead, but it's starting to smell really bad.''  -- rob

 
 
 

exit status of the command in pipe from shell & named pipe

Post by Shigeru IKED » Sun, 26 Jan 1997 04:00:00



> In bash (Note: for readers outside comp.os.linux.*, bash is the
> default Linux shell) you can have much finer control over your
> plubbing using ">()" and "<()".

> foo | bar

> Becomes:

> foo > >( bar )

> There is a danger with this approach that the next command will be
> executed before "bar" completes.

Hmmm. I did not know this structure. I will try this.

Quote:> If you need the exit status from more than one command in the pipe
> life becomes more tricky.

Thanks for the followup.
 
 
 

exit status of the command in pipe from shell & named pipe

Post by Shigeru IKED » Sun, 26 Jan 1997 04:00:00


-----BEGIN PGP SIGNED MESSAGE-----


> Newsgroups: comp.os.linux.development.apps,comp.unix.questions,comp.unix.programmer,comp.unix.shell
> Path: js3guj.ampr.org!news.uni-stuttgart.de!news.belwue.de!news-stu1.dfn.de!news-kar1.dfn.de!news-fra1.dfn.de!news.apfel.de!news.radio.cz!CESspool!news.maxwell.syr.edu!newsfeeds.sol.net!newspump.sol.net!howland.erols.net!feed1.news.erols.com!news.nl.innet.net!INnl.net!feed1.news.innet.be!INbe.net!cold.news.pipex.net!pipex!rill.news.pipex.net!pipex!warwick!niss!bath.ac.uk!ccsis

> Organization: Bath University Computing Services, UK

> Date: Fri, 24 Jan 1997 15:28:18 GMT
> Lines: 45
> Xref: js3guj.ampr.org comp.os.linux.development.apps:411 comp.unix.questions:236 comp.unix.programmer:141 comp.unix.shell:78

> This program reads all of /usr/lib/news/etc/subscriptions into memory,
> when only one line is needed at a time. It is better to write it as

Thanks for the correction. I will modify the script.

Quote:> >Q.1)
> >       How can I get exit status of the command in the pipeline
> >       from the shell?

> The exit value of a pipeline is the exit value of the last command, for
> all normal shells. I see that Bash2.0 now returns an array of exit
> values, and of course 'rc' and 'es' always returned a list.

> So you have to get involved with tricks. As you have a particular example,
> we do not need the full power of the file redirection abilities of the shell.

> exec >&3
> while read i
> do
>    status=`( nntpget -t '970101 000000' -n comp.os.linux.misc news
>            echo $? >&4 ) | inject.pl >&3 4>&1`
>    if [ $status != 0 ]; then
>            cleanup
>    fi
> done < /usr/lib/news/etc/subscriptions

Thanks for the suggestion. I will try this.

Regards,
Shigeru IKEDA

-----BEGIN PGP SIGNATURE-----
Version: 2.6.3ia
Charset: noconv

iQCVAwUBMulwp0Pp6IK9NdONAQG1FgP5AVqJSYN+xrIhjvSuZSfEWyMZvITFeBj0
jRMHsDkF9gmU9w3XlOkPpvv2XqfWtSuxVCZP87fir92NoAEGqJ0o2kT2edX21mwn
nnRQ582+VGrBkWOoh68M3ZYyEEOiHJUKmiyjS2yepCVn4rRB0qnkz50jcrDgQEH1
17pWqkV7PJU=
=az77
-----END PGP SIGNATURE-----

 
 
 

1. Difficulty retrieving exit status of command before pipe

A colleague was trying to capture the exit status from a command before
a pipe.

Here is a vastly simplified example

ls -1 v* | sort -r

My first attempt to capture exit status was:

( ls -1 v*; echo $? > ls.out) | sort -r

This works fine, but we wondered if it could be done without writing to
a file.

Next attempt:

export DOG
DOG=7
( ls -1 v*; DOG=$?; ) | sort -r

Of course, variable did not survive subshell, so we tried:

export DOG
DOG=7
{ ls -1 v*; DOG=$?; } | sort -r
echo $DOG

This ALSO did not work, although leaving the pipe off mysteriously
works:

export DOG
DOG=7
{ ls -1 v*; DOG=$?; }
echo $DOG

Here was another work-around

export DOG="dog.$$"; ( ls -1 v*; echo $? > $DOG ) | sort -r; echo $DOG;
rm $DOG

We tried this example under ksh, sh, and bash on HP-UX, Solaris, and
Linux.
What useful function is this apparently defective logic possibly
performing?
Why would adding a pipe be an invitation to throw away variables
already set?
Is this a race condition?

Bradley Slavik
(ten tod sld ta erif)

2. Adaptec 2842

3. Get exit status of first command in pipe.

4. Distri linux with support for Geforce2 MX 32MO

5. Obtaining the exit code (status) of command piped to tee

6. : news.randori.com rec.crafts.metalworking:289584Re: DWM Comic series ...

7. Exit status of first command when using pipe

8. steve chaney mocks people with cancer

9. Bash shell is always returning last exit status of the pipe

10. Any compatibility between Unix Named Pipes and NT Named Pipes??

11. getting status from first command in a piped command line

12. dump pipe gzip pipe ssh pipe dd... blocksize?

13. Help needed pipes and named pipes