Alex Rivera | Logout

Combining pipe with exit status in bash shell script

Asked 2011-09-09T11:30:45.173
9

I have this bash shell-script command that causes date to run if make succeeds (terminates with zero exit status) and vice versa:

make && date

but now I want to process its output e.g.

make | sed s/a/A/

If I do

make | sed s/a/A/ && date

date runs even when make fails.

If I instead do

make && date | sed s/a/A/

the sed processes date's output instead of make's.

Would you know of any solution? Thanks!


P.S. I've tried these:

(make | sed s/a/A/) && date

date still runs when make fails.

(make && (date > /dev/null)) | sed s/a/A/

date doesn't run when make succeeds.

Edit
Report

1 Answer

1

Basically like this;

my_make () {
  local rc
  make >tmp
  rc=$?
  sed s/a/A/ tmp
  rm tmp
  return $rc
}

The temporary file should obviously be handled in a proper way; this is bare-bones in order to focus on the issue at hand. You could probably avoid a temp file altogether if you are clever with file descriptors, but it's Friday and I need more coffee.

answered 2011-09-09T11:51:37.493

Your Answer