• Bash: Passing Variables To coproc Subprocess

    From Lawrence =?iso-8859-13?q?D=FFOlivei@3:633/280.2 to All on Friday, August 22, 2025 16:43:54
    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?

    --- MBSE BBS v1.1.2 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)
  • From Kenny McCormack@3:633/280.2 to All on Friday, August 22, 2025 22:42:56
    In article <10893ja$1dihv$1@dont-email.me>,
    Lawrence DOliveiro <ldo@nz.invalid> 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)".

    --- MBSE BBS v1.1.2 (Linux-x86_64)
    * Origin: The official candy of the new Millennium (3:633/280.2@fidonet)
  • From Christian Weisgerber@3:633/280.2 to All on Saturday, August 23, 2025 00:12:28
    On 2025-08-22, Kenny McCormack <gazelle@shell.xmission.com> 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 naddy@mips.inka.de

    --- MBSE BBS v1.1.2 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Kenny McCormack@3:633/280.2 to All on Saturday, August 23, 2025 00:32:02
    In article <slrn10agumd.2p61.naddy@lorvorc.mips.inka.de>,
    Christian Weisgerber <naddy@mips.inka.de> wrote:
    On 2025-08-22, Kenny McCormack <gazelle@shell.xmission.com> 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

    --- MBSE BBS v1.1.2 (Linux-x86_64)
    * Origin: The official candy of the new Millennium (3:633/280.2@fidonet)
  • From Kenny McCormack@3:633/280.2 to All on Wednesday, August 27, 2025 00:03:34
    In article <10893ja$1dihv$1@dont-email.me>,
    Lawrence DOliveiro <ldo@nz.invalid> 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...

    --- MBSE BBS v1.1.2 (Linux-x86_64)
    * Origin: The official candy of the new Millennium (3:633/280.2@fidonet)