[x3d-public] Bash function to catch return value

Leonard Daly Leonard.Daly at realism.com
Sat Jul 22 19:35:48 PDT 2023


2> starts a new file. 2>> appends to an existing file (and creates it if 
need be). Since the pipe is a single operation for the purpose of STDERR 
redirection, 2> ensures that the file is empty when first written to.

I suppose you could call a function after the pipe with redirection, 
passing it the exit status ($?) and error file (/tmp/pipe-$$.err). If 
the function returns, then things were OK. Something bad causes function 
to exit script with error code.

Leonard


On 7/22/2023 7:22 PM, John Carlson wrote:
> Thanks Leonard, i was hoping to use a function because i have a whole 
> list of pipelines to run in my script.
>
> Something like:
>
> catcher “oops” (sed ‘s/ //‘ | tr “\n” “ “ | ….)
> catcher…
>
> I don’t want a script for catcher.  If a function won’t suffice, a 
> preprocessor will.
>
> You probably want 2>> instead of 2> but I’m willing to hear your 
> reasoning.
>
> Thanks for your help!
>
> On Sat, Jul 22, 2023 at 9:01 PM Leonard Daly 
> <Leonard.Daly at realism.com> wrote:
>
>     Not sure why you would want a script as this is almost always done
>     in-line.
>
>     $? contains the exit status. 0 is almost always success (I don't
>     know where it is not) and != 0 is failure. Error message is
>     written to STDERR unless you direct a command to fail silently.
>
>     So something like
>
>     (A | B | C ...) 2> /tmp/pipe-$$.err if [ $? -ne 0 ] then  
>     exitCode=$?   cat /tmp/pipe-$$.err   exit $exitCode fi
>
>     Passing the piped command to a function can be dangerous/difficult
>     because of shell interpolation and quoting. Also note that the
>     piped output (STDOUT) is printed on the default (at the time of
>     the pipe) output (probably console), and pipe-$$.err is never
>     cleaned up (deleted). That is not really bad because most systems
>     delete files after 24-48 hours in /tmp. If there are no errors,
>     then /tmp/pipe-$$.err is never created.
>
>     Additional useful info:
>     https://unix.stackexchange.com/questions/164482/pipe-redirect-a-group-of-commands
>
>     Leonard Daly
>
>
>
>     On 7/21/2023 10:15 PM, John Carlson wrote:
>>     Linux folks:
>>
>>     Does anyone have a bash function (not a separate script) to check
>>     the exit status of the passed in  pipeline or passed in exit
>>     status, print out the passed in error message, and exit the
>>     script if the exit status indicates failure?
>>
>>     Thanks!
>>
>>     Should be fairly easy to write?
>>
>>     John
>>
>>     _______________________________________________
>>     x3d-public mailing list
>>     x3d-public at web3d.org
>>     http://web3d.org/mailman/listinfo/x3d-public_web3d.org
>     -- 
>     *Leonard Daly*
>     3D Systems Engineering
>     President, Daly Realism - /Creating the Future/
>     _______________________________________________
>     x3d-public mailing list
>     x3d-public at web3d.org
>     http://web3d.org/mailman/listinfo/x3d-public_web3d.org
>
>
> _______________________________________________
> x3d-public mailing list
> x3d-public at web3d.org
> http://web3d.org/mailman/listinfo/x3d-public_web3d.org
-- 
*Leonard Daly*
3D Systems Engineering
President, Daly Realism - /Creating the Future/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://web3d.org/pipermail/x3d-public_web3d.org/attachments/20230722/1a093adf/attachment-0001.html>


More information about the x3d-public mailing list