help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to best differ redirection errors from command exit status


From: #!microsuxx
Subject: Re: how to best differ redirection errors from command exit status
Date: Sat, 23 Nov 2024 04:16:52 +0100

to try tp answer the emails topic line
u can exec redictuons to files in the begnning
before the main cmds
ans if fhat fails ..

On Sat, Nov 23, 2024, 1:55 AM Philippe Cerfon <philcerf@gmail.com> wrote:

> 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.
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]