bug-bash
[Top][All Lists]
Advanced

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

Re: anonymous pipes in recursive function calls


From: konsolebox
Subject: Re: anonymous pipes in recursive function calls
Date: Sat, 29 Jun 2024 16:39:02 +0800

On Sat, Jun 29, 2024 at 6:39 AM Zachary Santer <zsanter@gmail.com> wrote:
>   command this-file |
>     while IFS='' read -r -d '' path; do
>       cd -- "${starting_PWD}/${path}"
>       if [[ -r this-file ]]; then
>         recursive_function "${entry_path}/${path}"
>       fi
>       # fill arrays
>       # there is another anonymous pipe here, as well
>     done
>   #
>   if (( PIPESTATUS[0] != 0 )); then
>     printf '%s\n' "command failed" >&2
>     error='true'
>   fi
>   cd -- "${starting_PWD}"
> }

You can avoid pipe recursions by storing the output first in an array.

There's also no need to use an error flag variable.  Just make sure
return calls are chained.

-------------------------
local temp
command this-file | readarray -d '' temp # -d is a Bash 5.0 feature.

if (( PIPESTATUS[0] != 0 )); then
  printf '%s\n' "command failed" >&2
  return 1
fi

for path in "${temp[@]}"; do
  cd -- "${starting_PWD}/${path}" || return
  if [[ -r this-file ]]; then
    recursive_function "${entry_path}/${path}" || return
  fi
  ...
done

cd -- "${starting_PWD}"
-------------------------

You can also just use a 'die' function if cleanups aren't needed.
Anything is valid in a contained code.


-- 
konsolebox



reply via email to

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