patchThe GNU Bourne-Again SHell - Patches: patch #10273, Choose connector for new lines...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

patch #10273: Choose connector for new lines instead of ;

Submitter:  Hugues <izissise>
Submitted:  Mon 29 Aug 2022 10:21:49 AM UTC
   
 
Category:  None Priority:  5 - Normal
Status:  Wont Do Privacy:  Public
Assigned to:  None Open/Closed:  Open

Wed 14 Sep 2022 05:04:32 PM UTC, comment #2: 

Hello, I agree that some of these usages aren't useful,
the most interesting one is undoubtedly `BASH_CMD_CONNECTOR='&&'`
It allows writing scripts that stop at the first unhandled error.

The alternative (writing '&& \' at the end of each line) is heavier but also error-prone and harder to read.

It is also impossible to dynamically rewrite functions using eval
to add '&&' at the desired positions while keeping correct precedence of command groups.

If you're willing to proceed, I could re-purpose my proposal to be
an option to 'eval' that could be used this way

eval --connector '&&' "$(declare -fp fun)"


For some context, we are routinely writing scripts that are distributed
to many nodes in a cloud infrastructure and
it would really help us to write more robust scripts.

Example of the problem we're trying to solve:

#!/bin/bash

fail() { false; }  # mock cmd failure

BASH_CMD_CONNECTOR='&&'

stop_on_fail() {
    rm -f /tmp/bashtest/file
    mkdir -p /tmp/bashtest
    fail # <--- with BASH_CMD_CONNECTOR='&&' no suppressed errors
    touch /tmp/bashtest/file
}

stop_on_fail || true # continue

[[ -f /tmp/bashtest/file ]] \
    && echo "[-] File found, error suppressed" \
    || echo "[+] File not found, error not suppressed"

------
OUTPUT BASH_CMD_CONNECTOR=''
echo "[-] File found, error suppressed
OUTPUT BASH_CMD_CONNECTOR='&&'
echo "[+] File not found, error not suppressed


Thanks for your time,
Hugues

Hugues <izissise>
Mon 29 Aug 2022 03:22:17 PM UTC, comment #1: 

Thanks for the suggestion. I don't think this is generally useful, and the ongoing support issues will outweigh the benefits. I'm not going to apply the patch.

Chet Ramey <chet>
Group administrator
Mon 29 Aug 2022 10:21:49 AM UTC, original submission:  

Add BASH_CMD_CONNECTOR special variable which set the
command connectors for new lines.

This allow users to write scripts that will fail at the first
unhandled error using BASH_CMD_CONNECTOR='&&' without
the fuss of adding it on each lines and makes reading easier.

Another use case is having a list of command executed in //
with BASH_CMD_CONNECTOR='&'

Also a function that would fallback to the first working command


config_fallback() { local BASH_CMD_CONNECTOR='||'; {
        [[ -f /config ]] && echo /config
        [[ -f /home/u/config ]] && echo /home/u/config
        [[ -f /deprecated ]] && echo /deprecated
        echo "Couldn't find config"
} }



Another example script

#!/bin/bash

fallback_cmds() { local BASH_CMD_CONNECTOR='||'; {
        [[ -f /aaaaaaaaaaa ]] && echo a
        [[ -f /bbbbbbbbbbb ]] && echo b
        [[ -f /ccccccccccc ]] && echo c
        echo "Couldn't find a b or c" # 0
} }

parallel_it() { local BASH_CMD_CONNECTOR='&'; {
        sleep 5
        sleep 5
        sleep 5
        sleep 5
        sleep 5
} }

parallel_wait() {
        echo "Waiting 5*5s in //"
        parallel_it
        wait
        wait
        wait
        wait
        wait
}

no_err() { local BASH_CMD_CONNECTOR='&&'; {
        echo a
        echo b
        [[ -d / ]]
        echo c
        ( exit 6; ) # 6
        echo "Not reached"
} }

pipeline_it() { local BASH_CMD_CONNECTOR='|'; {

        echo "X"
        cat
        sed 's/X/xXx/'
} }


print_exec() {
        echo "$@"
        "$@"
}

print_exec fallback_cmds
print_exec no_err || echo "result ${?}"
print_exec pipeline_it
print_exec parallel_wait


You might need to touch parse.y to fix compilation error

Hugues <izissise>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by chet (Posted a comment)
  • -email is unavailable- added by izissise (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

     

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2022-08-29 chet StatusNone Wont Do
    2022-08-29 izissise Attached File- Added 0001-Choose-connector-for-new-lines-instead-of.patch, #53620

    Back to the top

    Powered by Savane 3.13-5884.
    Corresponding source code