40

Is there some similar option in dash shell corresponding to pipefail in bash?

Or any other way of getting a non-zero status if one of the commands in pipe fail (but not exiting on it which set -e would).

To make it clearer, here is an example of what I want to achieve:

In a sample debugging makefile, my rule looks like this:

set -o pipefail; gcc -Wall $$f.c -o $$f 2>&1 | tee err; if [ $$? -ne 0 ]; then vim -o $$f.c err; ./$$f; fi;

Basically it runs opens the error file and source file on error and runs the programs when there is no error. Saves me some typing. Above snippet works well on bash but my newer Ubunty system uses dash which doesn't seem to support pipefail option.

I basically want a FAILURE status if the first part of the below group of commands fail:

gcc -Wall $$f.c -o $$f 2>&1 | tee err

so that I can use that for the if statement.

Are there any alternate ways of achieving it?

Thanks!

4
  • 1
    Why not just get rid of the tee? if gcc -Wall $$f.c -o $$f >$$f.log 2>&1; then cat $$f.log; ./$$f; else vim -o $$f.c $$f.log; fi (Alternatively, install bash on Ubunty. It's only an apt-get away.)
    – rici
    Jul 20, 2013 at 4:17
  • @rici Thanks! I was using tee because I also wanted the stderr output printed to screen - mainly to see if there were any warnings. But if there is no other way I'll probably switch to what you suggested. About installing bash: I had assumed (without any research) that dash is a step forward from bash so I didn't want to switch back but now googling a little bit about it seems that might not be necessarily so. I'll read a little more about the differences and then decide. Thank you once again for both your pointers!
    – Lavya
    Jul 20, 2013 at 5:00
  • The "stock" answer for POSIX implementations of PIPESTATUS / pipefail is in the comp.unix.shell FAQ Q11
    – dan3
    Sep 12, 2013 at 21:28
  • 1
    I'd hoped that the set -e mentioned above would provide me with a dash equivalent of set -o pipefail but not so: $ dash -c 'set -e; false | cat'; echo $? 0 $ Jun 27, 2015 at 17:02

2 Answers 2

6

I ran into this same issue and the bash options of set -o pipefail and ${PIPESTATUS[0]} both failed in the dash shell (/bin/sh) on the docker image I'm using. I'd rather not modify the image or install another package, but the good news is that using a named pipe worked perfectly for me =)

mkfifo named_pipe
tee err < named_pipe &
gcc -Wall $$f.c -o $$f > named_pipe 2>&1
echo $?

See this answer for where I found the info: https://stackoverflow.com/a/1221844/431296

3

The Q.'s sample problem requires:

I basically want a FAILURE status if the first part of the ... group of commands fail:

Install moreutils, and try the mispipe util, which returns the exit status of the first command in a pipe:

sudo apt install moreutils

Then:

if mispipe "gcc -Wall $$f.c -o $$f 2>&1" "tee err" ; then \
     ./$$f 
else 
     vim -o $$f.c err 
fi

While 'mispipe' does the job here, it is not an exact duplicate of the bash shell's pipefail; from man mispipe:

   Note that some shells, notably bash, do offer a
   pipefail option, however, that option does not
   behave the same since it makes a failure of any
   command in the pipeline be returned, not just the
   exit status of the first.
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.