Was "feature suggestion: ability to expand a set of elements of an
array or characters of a scalar, given their indices"

On Fri, Jun 28, 2024 at 5:29 PM Zachary Santer <zsan...@gmail.com> wrote:

> ( 2 )
> A script that I've written more recently generates some arrays
> describing everything it needs to do before it starts making updates. [...]

> And, yes, I am trying to do some complicated stuff in bash in the most
> reasonable way I can find. [...]

Speaking of, I finally got a chance to run this thing today. Has a
problem with an anonymous pipe in a recursive function call been
resolved since bash 4.2?


set -o nounset -o noglob +o braceexpand
shopt -s lastpipe

main () {
  # initialize arrays
  recursive_function .
  # loop through arrays
}

recursive_function () {
  local entry_path="${1}"
  local starting_PWD="${PWD}"
  local path
  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}"
}

main "${@}"


Looks like, at the end of the while loop in the first call to
recursive_function (), I get
/path/to/the/script: line <the line recursive_function () begins on>:
wait_for: No record of process <pid>.

recursive_function () is called from main () and then calls itself
twice from within the while loop in the first call, in my case.

PIPESTATUS[@] isn't doing what I would expect either. Sometimes it's a
one-element array. Sometimes it's two. It was giving me an exit status
of 1 at element 0 and no element 1, when I would expect "command" to
give an exit status of 0, but not every time.

Wound up doing


set -o nounset -o noglob +o braceexpand -o pipefail
shopt -s lastpipe

main () {
  # initialize arrays
  recursive_function .
  # loop through arrays
}

recursive_function () {
  local entry_path="${1}"
  local starting_PWD="${PWD}"
  local path
  if ! \
    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
        true
      }
    #
  then
    printf '%s\n' "command failed" >&2
    error='true'
  fi
  cd -- "${starting_PWD}"
}

main "${@}"


To work around the PIPESTATUS thing, but that hasn't made bash's error
message go away.

Not sure how I could come up with a repeat-by for this thing. It
already worked recursively before I modified it to track everything it
needed to do and then go back and do it. I don't think this error
message was present then. (This script produces a lot of output,
though.) I wasn't referencing PIPESTATUS[0] yet either. No way to run
this in a more recent version of bash.

I can try
while [...] done < <( command this-file )
later, to see if it *is* bash being unable to wait for the first
element of the pipeline in the first call to recursive_function (),
like it feels like it is.

Reply via email to