• Bash: Passing Variables To coproc Subprocess

    From Lawrence =?iso-8859-13?q?D=FFOlivei@21:1/5 to All on Fri Aug 22 06:43:54 2025
    I was trying to write a Bash script to clean up the old checkpoints in
    my Jupyter notebooks directories, which have been accumulating for
    some years. The first version looked like this:

    BASEDIR=~/Documents/Jupyter\ Notebooks/
    CHECKPOINTSDIR=.ipynb_checkpoints
    NRDAYS=30 # delete everything older than this

    collect_checkpoints_dirs()
    {
    local -n arr="$1"
    arr=()
    coproc collector { find "$BASEDIR" -type d -name "$CHECKPOINTSDIR" -print0; }
    # must ensure while-loop runs in this process
    while read -u ${collector[0]} -rd '' line; do
    arr[${#arr[*]}]="$line"
    done
    wait $collector_PID
    } # collect_checkpoints_dirs

    collect_checkpoints_dirs dirs

    echo "${dirs[@]}"
    for dir in "${dirs[@]}"; do
    find "$dir" -maxdepth 1 -type f -mtime +${NRDAYS} -print0 | xargs -0 rm -fv
    done

    but it didn’t work. I figured out that the BASEDIR and CHECKPOINTSDIR variables were not being properly expanded inside the coproc command.
    If I substitute those values literally

    coproc collector { find ~/Documents/Jupyter\ Notebooks/ -type d -name .ipynb_checkpoints -print0; }

    then it works.

    How would I pass variables into that coproc command?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to [email protected] on Fri Aug 22 12:42:57 2025
    In article <10893ja$1dihv$[email protected]>,
    Lawrence DOliveiro <[email protected]d> wrote:
    I was trying to write a Bash script to clean up the old checkpoints in
    my Jupyter notebooks directories, which have been accumulating for
    some years. The first version looked like this:
    ...
    but it didnt work. I figured out that the BASEDIR and CHECKPOINTSDIR >variables were not being properly expanded inside the coproc command.
    If I substitute those values literally

    coproc collector { find ~/Documents/Jupyter\ Notebooks/ -type d -name
    .ipynb_checkpoints -print0; }

    then it works.

    How would I pass variables into that coproc command?

    First, I think you should switch to using "mapfile" instead of "coproc/read". "mapfile" is extremely good for this sort of thing and I tend to use it in
    all my scripts. In fact, the main script that I've been developing for
    almost 5 years now, uses "coproc" at its core because when I first started, "coproc" looked interesting - and I hadn't really gotten used to "mapfile"
    at that point. Not likely to go back and change it at this point, but,
    looking back on it, I think if I had it to do all over again, I wouldn't
    use "coproc" at all.

    Try:

    mapfile -t < <(find "$BASEDIR" -type d -name "$CHECKPOINTSDIR")

    Note: This will fail if you have directory names with newlines in them, but then again, if you have directory names with newlines, you're in pretty bad straits already...

    Second, my first reaction to your problem was that you need to export the variables (if you think it through, that kinda makes sense), but I just did
    the following little test and it did the right thing:

    $ foo=bar
    $ coproc { echo "$foo"; }; read -u$COPROC; echo $REPLY
    bar
    $

    So, I don't know...

    --
    What can you do with this besides printing sarcastic and obscene messages to the
    screens of lusers at login or logout?

    From the man page for "strfile(1)".

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to [email protected] on Fri Aug 22 14:32:02 2025
    In article <[email protected]>,
    Christian Weisgerber <[email protected]> wrote:
    On 2025-08-22, Kenny McCormack <[email protected]> wrote:

    Try:

    mapfile -t < <(find "$BASEDIR" -type d -name "$CHECKPOINTSDIR")

    Note: This will fail if you have directory names with newlines in them,

    mapfile -d '' -t < <(find ... -print0)

    As I said, if you have directory names with newlines in them, then you have more serious problems than I can help you with.

    --
    https://en.wiktionary.org/wiki/res_ipsa_loquitur

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Weisgerber@21:1/5 to Kenny McCormack on Fri Aug 22 14:12:29 2025
    On 2025-08-22, Kenny McCormack <[email protected]> wrote:

    Try:

    mapfile -t < <(find "$BASEDIR" -type d -name "$CHECKPOINTSDIR")

    Note: This will fail if you have directory names with newlines in them,

    mapfile -d '' -t < <(find ... -print0)

    --
    Christian "naddy" Weisgerber [email protected]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to [email protected] on Tue Aug 26 14:03:35 2025
    In article <10893ja$1dihv$[email protected]>,
    Lawrence DOliveiro <[email protected]d> wrote:
    I was trying to write a Bash script to clean up the old checkpoints in
    my Jupyter notebooks directories, which have been accumulating for
    some years. The first version looked like this:

    I wonder if OP is still reading this thread. I'm curious how it all worked out. In particular, I'd like to know if there *is* any problem passing (un-exported) variables into coprocs.

    --
    Meatball Ron wants to replace the phrase "climate change" with the phrase "energy dominance" in policy discussions.

    Yeah, like that makes a lot of sense...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)