Skip to content

Instantly share code, notes, and snippets.

@svieira
Last active March 20, 2024 06:08
Show Gist options
  • Save svieira/66129199bdab056ee92d to your computer and use it in GitHub Desktop.
Save svieira/66129199bdab056ee92d to your computer and use it in GitHub Desktop.

Revisions

  1. svieira revised this gist Mar 20, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions script-example.sh
    Original file line number Diff line number Diff line change
    @@ -47,9 +47,9 @@ while [ "${1+defined}" ]; do
    -d|--option-d) option_d=on;;
    # Allow getopts-style collections of short args
    # This will allow -de and -ed to both parse correctly
    -d*) set -- "-d" "-${1##-d}" "$@";;
    -d*) _rest="${1##-d}"; set -- "-d" "-$_rest" "$@";;
    -e|--option-e) option_e=on;;
    -e*) set -- "-e" "-${1##-e}" "$@";;
    -e*) _rest="${1##-e}"; set -- "-e" "-$_rest" "$@";;
    *) echo "Unknown option $1" >&2 && exit 1;;
    esac
    shift
  2. svieira revised this gist Mar 20, 2024. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion script-example.sh
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ function opt() {
    echo "-$1?(=*)|--$2?(=*)"
    }


    # See https://argbash.dev/ for more patterns
    while [ "${1+defined}" ]; do
    case $1 in
    -h|--help) echo "$HELP" && exit 0;;
    @@ -44,6 +44,12 @@ while [ "${1+defined}" ]; do
    fi;;
    # Using code to define actual pattern (requires shopt -s extglob)
    @($(opt c chatty))) echo "Will chat now" && exit 0;;
    -d|--option-d) option_d=on;;
    # Allow getopts-style collections of short args
    # This will allow -de and -ed to both parse correctly
    -d*) set -- "-d" "-${1##-d}" "$@";;
    -e|--option-e) option_e=on;;
    -e*) set -- "-e" "-${1##-e}" "$@";;
    *) echo "Unknown option $1" >&2 && exit 1;;
    esac
    shift
  3. svieira revised this gist Apr 5, 2023. 1 changed file with 10 additions and 4 deletions.
    14 changes: 10 additions & 4 deletions script-example.sh
    Original file line number Diff line number Diff line change
    @@ -27,17 +27,23 @@ HELP
    option_a=
    option_b="$SOME_SYSTEM_VARIABLE"

    function opt() {
    echo "-$1?(=*)|--$2?(=*)"
    }


    while [ "${1+defined}" ]; do
    case $1 in
    -h|--help) echo "$HELP" && exit 0;;
    -a?(=*)|--option-a?(=*)) if [[ "$1" = *=* ]];
    then option_a="${1#*=}";
    else option_a="$2"; shift;
    fi;;
    # Simple options
    -a|--option-a) option_a="$2"; shift;
    # Options that support being called with =
    -b?(=*)|--option-b?(=*)) if [[ "$1" = *=* ]];
    then option_b="${1#*=}";
    else option_b="$2"; shift;
    fi;;
    # Using code to define actual pattern (requires shopt -s extglob)
    @($(opt c chatty))) echo "Will chat now" && exit 0;;
    *) echo "Unknown option $1" >&2 && exit 1;;
    esac
    shift
  4. svieira revised this gist Apr 5, 2023. 1 changed file with 13 additions and 3 deletions.
    16 changes: 13 additions & 3 deletions script-example.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    #!/usr/bin/env bash -f

    shopt -s extglob

    SCRIPT_NAME=`basename $0`

    read -rd '' HELP << HELP
    @@ -28,9 +30,17 @@ option_b="$SOME_SYSTEM_VARIABLE"
    while [ "${1+defined}" ]; do
    case $1 in
    -h|--help) echo "$HELP" && exit 0;;
    -a|--option-a) option_a=$2; shift;;
    -b|--option-b) option_b=$2; shift;;
    -a?(=*)|--option-a?(=*)) if [[ "$1" = *=* ]];
    then option_a="${1#*=}";
    else option_a="$2"; shift;
    fi;;
    -b?(=*)|--option-b?(=*)) if [[ "$1" = *=* ]];
    then option_b="${1#*=}";
    else option_b="$2"; shift;
    fi;;
    *) echo "Unknown option $1" >&2 && exit 1;;
    esac
    shift
    done
    done

    echo "$option_a and $option_b"
  5. svieira revised this gist Mar 9, 2016. No changes.
  6. svieira revised this gist Mar 9, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bash-tricks.sh
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ done

    # Reading a file line by line
    while read var1 var2 etc; do
    perform --commands --with $var1 $var2 $etc
    perform --commands --with $var1 $var2 $etc
    done < some-file.ext

    # The for loop with test syntax
  7. svieira revised this gist Mar 9, 2016. 2 changed files with 30 additions and 8 deletions.
    23 changes: 15 additions & 8 deletions bash-tricks.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Looping per line of input
    some | command | sequence | while read var1 var2; do

    # something here
    done

    # Reading a file line by line
    @@ -10,11 +10,11 @@ done < some-file.ext

    # The for loop with test syntax
    for f in *; do
    if [[ -d "$f" ]]; then
    pushd "$f";
    echo "Do work";
    popd;
    fi;
    if [[ -d "$f" ]]; then
    pushd "$f";
    echo "Do work";
    popd;
    fi;
    done

    # Test for a pattern match
    @@ -27,5 +27,12 @@ cat <(what magic is this?)
    # http://unix.stackexchange.com/questions/99423/appending-to-same-array-in-various-loops-only-last-values-remain-bash-4
    some_array=()
    while some test; do
    some_array+=("$some_variable")
    done < <(some command producing output)
    some_array+=("$some_variable")
    done < <(some command producing output)

    # if __name__ == '__main__': equivalent
    if [[ ${BASH_SOURCE[0]} != $0 ]]; then
    # We were sourced, not run
    else
    # We are being run, go, go, go!
    fi
    15 changes: 15 additions & 0 deletions commands.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    # Useful command sequences

    # Find the first command that is defined
    # For example, this uses the GNU brew-installed utilities before the normal readline on Mac
    # while on a system without it (or Linux it will use the normal readline)
    type -p greadlink readlink | head -1

    # Alter a remote's URL
    git remote -v | cut -d' ' -f 1 | cut -f 1-2 | sed 's/something/something-else/g' | xargs -t -n 2 git remote set-url;

    # Run sed on a set of files containing a pattern
    grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'

    # Run sed with alternative delimiters (any character will work)
    sed 's_http://some/url_http://some/replacement_g'
  8. svieira revised this gist Aug 15, 2015. 1 changed file with 36 additions and 0 deletions.
    36 changes: 36 additions & 0 deletions script-example.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/usr/bin/env bash -f

    SCRIPT_NAME=`basename $0`

    read -rd '' HELP << HELP
    $SCRIPT_NAME - what the script does
    Usage:
    $SCRIPT_NAME
    [-a|--option-a A] - the first option
    (default: some sensible default)
    [-b|--option-b B] - another option
    Falls back on \$SOME_SYSTEM_VARIABLE
    positional-args ... - A longer description
    spanning several lines
    Examples:
    * $SCRIPT_NAME -a an -b example # With a description
    HELP

    [[ $# -lt 1 ]] && echo "$HELP" && exit 1;

    option_a=
    option_b="$SOME_SYSTEM_VARIABLE"

    while [ "${1+defined}" ]; do
    case $1 in
    -h|--help) echo "$HELP" && exit 0;;
    -a|--option-a) option_a=$2; shift;;
    -b|--option-b) option_b=$2; shift;;
    *) echo "Unknown option $1" >&2 && exit 1;;
    esac
    shift
    done
  9. svieira revised this gist Mar 29, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions bash-tricks.sh
    Original file line number Diff line number Diff line change
    @@ -24,6 +24,7 @@ done
    cat <(what magic is this?)

    # Load an array with data
    # http://unix.stackexchange.com/questions/99423/appending-to-same-array-in-various-loops-only-last-values-remain-bash-4
    some_array=()
    while some test; do
    some_array+=("$some_variable")
  10. svieira revised this gist Mar 29, 2015. 1 changed file with 18 additions and 1 deletion.
    19 changes: 18 additions & 1 deletion bash-tricks.sh
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    # Looping per line of input
    some | command | sequence | while read var1 var2; do

    done

    # Reading a file line by line
    while read var1 var2 etc; do
    perform --commands --with $var1 $var2 $etc
    @@ -10,4 +15,16 @@ for f in *; do
    echo "Do work";
    popd;
    fi;
    done
    done

    # Test for a pattern match
    [[ $some-var =~ ^(some|regex)$ ]] && echo "Yep, it's a match!"

    # Process substitution: http://tldp.org/LDP/abs/html/process-sub.html
    cat <(what magic is this?)

    # Load an array with data
    some_array=()
    while some test; do
    some_array+=("$some_variable")
    done < <(some command producing output)
  11. svieira revised this gist Jun 10, 2014. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions bash-tricks.sh
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,4 @@ for f in *; do
    echo "Do work";
    popd;
    fi;
    done

    # Alter a remote's URL
    git remote -v | cut -d' ' -f 1 | cut -f 1-2 | sed 's/something/something-else/g' | xargs -t -n 2 git remote set-url;
    done
  12. svieira revised this gist Jun 10, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions bash-tricks.sh
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    /* Reading a file line by line */
    # Reading a file line by line
    while read var1 var2 etc; do
    perform --commands --with $var1 $var2 $etc
    done < some-file.ext

    /* The for loop with test syntax */
    # The for loop with test syntax
    for f in *; do
    if [[ -d "$f" ]]; then
    pushd "$f";
    @@ -12,5 +12,5 @@ for f in *; do
    fi;
    done

    /* Alter a remote's URL */
    # Alter a remote's URL
    git remote -v | cut -d' ' -f 1 | cut -f 1-2 | sed 's/something/something-else/g' | xargs -t -n 2 git remote set-url;
  13. svieira revised this gist Jun 10, 2014. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion bash-tricks.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,16 @@
    /* Reading a file line by line */
    while read var1 var2 etc; do
    perform --commands --with $var1 $var2 $etc
    done < some-file.ext
    done < some-file.ext

    /* The for loop with test syntax */
    for f in *; do
    if [[ -d "$f" ]]; then
    pushd "$f";
    echo "Do work";
    popd;
    fi;
    done

    /* Alter a remote's URL */
    git remote -v | cut -d' ' -f 1 | cut -f 1-2 | sed 's/something/something-else/g' | xargs -t -n 2 git remote set-url;
  14. svieira created this gist May 6, 2014.
    4 changes: 4 additions & 0 deletions bash-tricks.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    /* Reading a file line by line */
    while read var1 var2 etc; do
    perform --commands --with $var1 $var2 $etc
    done < some-file.ext