[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Help-bash Digest, Vol 113, Issue 29
From: |
Greg Wooledge |
Subject: |
Re: Help-bash Digest, Vol 113, Issue 29 |
Date: |
Tue, 16 Mar 2021 11:15:55 -0400 |
On Tue, Mar 16, 2021 at 03:01:13PM +0000, Budi wrote:
> How can we return from a Bash line code doing tee, ie. making another
> process, within function, to return back to the function caller ?
>
> this not work at all
>
> fn(){
>
> find ~+ -regextype posix-extended -iregex '.+/foo.*(bar|baz)' | tee >(
> read u &&{ echo $u was found ;return 1;} )
> echo Not seen
> }
The primary issue that I see here is you're trying to use "return"
inside a child process.
The process substitution >(...) runs a child process in the background.
For most practical purposes, you can treat this just like any old command
that you run in the background using &. It has an exit status, which
you can set by calling "exit". But it's not a function call, so using
"return" doesn't make sense.
The second issue I see is that you seem to have your exit codes all
backwards. You're returning "1" (failure) for the case where some
file was found. Presumably you're allowing the function to return "0"
(success) if no file was found, based on the "echo Not seen" at the end.
An exit status of 0 should be used for the success case, and nonzero
for error/failure cases.
Now, trying to reverse engineer your code futher, it *looks* like what
you're trying to do is run find, and have it return an exit status based
on whether the list of files that it found was empty or not.
As I'm sure you're aware, find doesn't do that. It always returns 0
(success) unless very specific failures happen. The lack of any files
meeting the match criteria is not considered a failure.
I'm sure there are many clever ways to get around this, but running tee
as a background child process is not one of them.
The first answer that occurs to me would be to read find's results into
a temp file, or an array, and return failure if the result set is empty.
If you'd like to search for other ways, by all means feel free. I'm sure
lots of other people have wanted this in the past, so I'd imagine there's
a plethora of prior art available on the Internet.