Re: Can redirection change exit status of a pipeline?



In article <1172104032.976564.189440@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
clarkbt36@xxxxxxxxxxxxx wrote:

Hello folks,

Forgive me an example from C++ programming, but my question is about
the Linux "sh" shell (I believe it is bash).

I have a program that executes the "system" library function like
this:

int status = system("/bin/ps -ef |/bin/grep -v grep|/bin/grep
ImplRepo_Service");

Now this ImplRepo_Service process is guaranteed to be running on the
system at the time this "system" function is executed, so I expected
the exit status (after appropriate manipulation of the "status" return
value from the "system" function) to be 0. It actually is, however, 1.

I was wondering whether the problem is that I am not redirecting the
output of the final grep in the pipeline to /dev/null. I found that
when I modified the pipeline to redirect the standard output of the
final grep to a file, that I then actually get the correct exit status
of 0.

Can anybody tell me, is my guess correct? And can redirecting the
output of a pipeline ever change the exit status of the pipeline?

If writing to standard output fails, the command is likely to exit with
a failure status code. So it depends on where the non-redirected output
is going to.

--
Barry Margolin, barmar@xxxxxxxxxxxx
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
.



Relevant Pages

  • Can redirection change exit status of a pipeline?
    ... Forgive me an example from C++ programming, ... I have a program that executes the "system" library function like ... I was wondering whether the problem is that I am not redirecting the ... output of the final grep in the pipeline to /dev/null. ...
    (comp.unix.shell)
  • Re: Infinite Loops and Explicit Exits
    ... a method of evaluation which enabled people knowledgeable about film, ... While both programming and movies are evolutionary, ... The only compiler that has EXIT PERFORM in out-of-line in Chuck's ... > END-IF ...
    (comp.lang.cobol)
  • Re: When it will be freed?
    ... easier to understand with multiple return (or exit()) points. ... I liked a sig I saw recently about goto - something like "I tell my ... goto in C programming, unless it was for expository reasons. ...
    (comp.lang.c)
  • Re: Idiom wanted: do-while
    ... structured programming definition -- every block of code has one entry point ... Break statements clearly violate the "one exit point" ... most not being privy to original design considerations, ...
    (comp.lang.ruby)
  • Re: Looking for a way to convert dec to bin
    ... KINDS of code that doesn't use Exit For/Exit Do, ... immensely...were you expecting anything less than structured programming? ... I don't know that I'd call myself a zealot. ... having satisfied the conditions for getting out of the loop. ...
    (microsoft.public.vb.general.discussion)