Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Instead of trying to juggle conditional traps, it's far simpler to exit conditionally or send a signal when commands or command groups fail. set -Eeuo pipefail shopt -s lastpipe # Allow a subs...
#2: Post edited
- Instead of trying to juggle conditional traps, it's far simpler to exit conditionally or send a signal when commands or command groups fail.
- ```
- set -Eeuo pipefail
- shopt -s lastpipe
- # Allow a subshell to signal the parent process for a fatal error.
- trap 'exit 2' USR1
- if test -z "$*"; then
- grep foo
- # grep's exit codes are assumed to be compatible with the wrapper's.
- else
- { find "$@" -type f || kill -USR1 "$$"; } \
- | xargs grep foo || exit 1
# Since the xargs grep is not in a subshell, we can exit 1 directly from it# without needing a second signal.- fi
- ```
- (Side note: you don't; need; to go nuts; with semicolons! They are equivalent to line terminators, so they're never necessary at the end of a line or a trap handler.)
- Instead of trying to juggle conditional traps, it's far simpler to exit conditionally or send a signal when commands or command groups fail.
- ```
- set -Eeuo pipefail
- shopt -s lastpipe
- # Allow a subshell to signal the parent process for a fatal error.
- trap 'exit 2' USR1
- if test -z "$*"; then
- grep foo
- # grep's exit codes are assumed to be compatible with the wrapper's.
- else
- { find "$@" -type f || kill -USR1 "$$"; } \
- | xargs grep foo || exit 1
- fi
- ```
- (Side note: you don't; need; to go nuts; with semicolons! They are equivalent to line terminators, so they're never necessary at the end of a line or a trap handler.)
#1: Initial revision
Instead of trying to juggle conditional traps, it's far simpler to exit conditionally or send a signal when commands or command groups fail.
```
set -Eeuo pipefail
shopt -s lastpipe
# Allow a subshell to signal the parent process for a fatal error.
trap 'exit 2' USR1
if test -z "$*"; then
grep foo
# grep's exit codes are assumed to be compatible with the wrapper's.
else
{ find "$@" -type f || kill -USR1 "$$"; } \
| xargs grep foo || exit 1
# Since the xargs grep is not in a subshell, we can exit 1 directly from it
# without needing a second signal.
fi
```
(Side note: you don't; need; to go nuts; with semicolons! They are equivalent to line terminators, so they're never necessary at the end of a line or a trap handler.)
