Skip to main content
added 75 characters in body
Source Link
Wadih M.
  • 1.9k
  • 1
  • 18
  • 24

If you're using grep with set -euo pipefail and don't want to exit if no record is found (exit code = 1), but still want it to fail if it's another exit code, you can use this:

#!/bin/bash
set -euo pipefail

echo "anything" | { grep e || test $? = 1; } | { grep e2 || test $? = 1; }

The greps inside the pipes will ONLY ignore exit status = 1. This way you don't need to lose the benefits of using set -euo pipefail.

In this example I purposely included two piped greps that could fail to illustrate the concept.

Very well explained onRefer to myrdd's post there.

If you're using grep with set -euo pipefail and don't want to exit if no record is found (exit code = 1), but still want it to fail if it's another exit code, you can use this:

#!/bin/bash
set -euo pipefail

echo "anything" | { grep e || test $? = 1; } | { grep e2 || test $? = 1; }

The greps inside the pipes will ONLY ignore exit status = 1.

In this example I purposely included two piped greps that could fail to illustrate the concept.

Very well explained on myrdd's post there.

If you're using grep with set -euo pipefail and don't want to exit if no record is found (exit code = 1), but still want it to fail if it's another exit code, you can use this:

#!/bin/bash
set -euo pipefail

echo "anything" | { grep e || test $? = 1; } | { grep e2 || test $? = 1; }

The greps inside the pipes will ONLY ignore exit status = 1. This way you don't need to lose the benefits of using set -euo pipefail.

In this example I purposely included two piped greps that could fail to illustrate the concept.

Refer to myrdd's post there.

Source Link
Wadih M.
  • 1.9k
  • 1
  • 18
  • 24

If you're using grep with set -euo pipefail and don't want to exit if no record is found (exit code = 1), but still want it to fail if it's another exit code, you can use this:

#!/bin/bash
set -euo pipefail

echo "anything" | { grep e || test $? = 1; } | { grep e2 || test $? = 1; }

The greps inside the pipes will ONLY ignore exit status = 1.

In this example I purposely included two piped greps that could fail to illustrate the concept.

Very well explained on myrdd's post there.