• Bash: Passing Variables To coproc Subprocess

    From Lawrence =?iso-8859-13?q?D=FFOliveiro?=@ldo@nz.invalid to comp.unix.shell on Fri Aug 22 06:43:54 2025
    From Newsgroup: comp.unix.shell

    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 didnrCOt 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?
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From gazelle@gazelle@shell.xmission.com (Kenny McCormack) to comp.unix.shell on Fri Aug 22 12:42:57 2025
    From Newsgroup: comp.unix.shell

    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)".
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Christian Weisgerber@naddy@mips.inka.de to comp.unix.shell on Fri Aug 22 14:12:29 2025
    From Newsgroup: comp.unix.shell

    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
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From gazelle@gazelle@shell.xmission.com (Kenny McCormack) to comp.unix.shell on Fri Aug 22 14:32:02 2025
    From Newsgroup: comp.unix.shell

    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
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From gazelle@gazelle@shell.xmission.com (Kenny McCormack) to comp.unix.shell on Tue Aug 26 14:03:35 2025
    From Newsgroup: comp.unix.shell

    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...
    --- Synchronet 3.21a-Linux NewsLink 1.2