0

I looked at Exiting a pipeline if a former command fails but I'm looking for a more general solution to this problem:

chlist -q -v -m -r $sourcelist | \
grep '^USER:' | \
chlist -q -v -f - $superlist

the problem is if the first chlist fails than no input into the 2nd chlist is very bad indeed.

Is there any solution to stopping the 2nd chlist from running other than putting the output of the first one into a temp file and then checking the exit status (eg not using a pipeline at all)?

3 Answers3

1

All commands in a pipeline are started asynchronously, so if you want to ensure that the last command doesn't run if there is no input you need to split up the pipeline as you suggest. Alternatively maybe chlist (which I don't know of) has an option to do that for you.

l0b0
  • 51,350
0

After messing around with this some more, I came up with the idea below and I think it might be a workable and pretty general solution:

( rm -f FAIL ; chlist -q -v -m -r $sourcelist || touch FAIL ) | \

( test -e FAIL || chlist -q -v -f - $superlist )

(I omitted the grep for clarity here, but this works fine with it in there)

Another, (cleaner?) variation:

( chlist -q -v -m -r $sourcelist || touch FAIL ) | \

( rm FAIL 2>/dev/null || chlist -q -v -f - $superlist )
  • This may work sometimes by bad luck, but definitely not in general. The pieces of a pipeline are run concurrently, and if your hardware supports it simultaneously, NOT sequentially. Try (sleep 1; touch foo) | ls foo; ls foo – dave_thompson_085 Dec 04 '18 at 12:03
  • Do not the short-circuit rules of || still apply? – John Hascall Dec 04 '18 at 12:42
  • Short-circuit is conditional but not concurrent; a||b must wait for a to complete before deciding whether to execute b. Try ((sleep 5;false)||touch foo)|(ls foo;date +D%F.%T);stat -cS%y foo and see the timestamp on foo (labelled S) is 5 seconds after the time displayed by date (labelled D) – dave_thompson_085 Dec 06 '18 at 16:42
0

How about appending a dummy line to the first command's output that satisfies the input requirements of the last one but doesn't trigger an action?

RudiC
  • 8,969