[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
how to best differ redirection errors from command exit status
From: |
Philippe Cerfon |
Subject: |
how to best differ redirection errors from command exit status |
Date: |
Sat, 23 Nov 2024 01:54:31 +0100 |
Hey.
When doing something like:
utility >somefile
case "$?"
...
$? could be either the exit status from utility or some non-zero value
if the redirection failed (or further values when utility wasn't
found, etc.).
When it's important to know which exit status the utility itself gave,
if any, this may cause obviously problems, like with grep or diff,
where e.g. exit status 1 is not really an error, but no-match-found
respectively differences-found.
Is there better/cleaner/faster solution to this than the following:
( exec >somefile || exit 125; utility )
which of course assumes that utility doesn't use the exit status 125.
Or are there any cases where the above wouldn't work?
- It should work with set -e on/off/ignored, because the exit happen
in all three cases, so no need to do && instead of ; or would that be
better?
- The redirection set up via exec should also only be done within the
subshell, so no need to cleanup.
Downside is of course the extra subshell and that it gets ugly to
write this when e.g. in the first compound-list for if/while/etc.
Thanks,
Philippe.
- how to best differ redirection errors from command exit status,
Philippe Cerfon <=