Last active
          March 20, 2024 06:08 
        
      - 
      
- 
        Save svieira/66129199bdab056ee92d to your computer and use it in GitHub Desktop. 
Revisions
- 
        svieira revised this gist Mar 20, 2024 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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*) _rest="${1##-d}"; set -- "-d" "-$_rest" "$@";; -e|--option-e) option_e=on;; -e*) _rest="${1##-e}"; set -- "-e" "-$_rest" "$@";; *) echo "Unknown option $1" >&2 && exit 1;; esac shift 
- 
        svieira revised this gist Mar 20, 2024 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 
- 
        svieira revised this gist Apr 5, 2023 . 1 changed file with 10 additions and 4 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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;; # 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 
- 
        svieira revised this gist Apr 5, 2023 . 1 changed file with 13 additions and 3 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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?(=*)) 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 echo "$option_a and $option_b" 
- 
        svieira revised this gist Mar 9, 2016 . No changes.There are no files selected for viewing
- 
        svieira revised this gist Mar 9, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 done < some-file.ext # The for loop with test syntax 
- 
        svieira revised this gist Mar 9, 2016 . 2 changed files with 30 additions and 8 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; 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) # if __name__ == '__main__': equivalent if [[ ${BASH_SOURCE[0]} != $0 ]]; then # We were sourced, not run else # We are being run, go, go, go! fi This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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' 
- 
        svieira revised this gist Aug 15, 2015 . 1 changed file with 36 additions and 0 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 
- 
        svieira revised this gist Mar 29, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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") 
- 
        svieira revised this gist Mar 29, 2015 . 1 changed file with 18 additions and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 # 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) 
- 
        svieira revised this gist Jun 10, 2014 . 1 changed file with 1 addition and 4 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,4 @@ for f in *; do echo "Do work"; popd; fi; done 
- 
        svieira revised this gist Jun 10, 2014 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,9 @@ # 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 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 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; 
- 
        svieira revised this gist Jun 10, 2014 . 1 changed file with 13 additions and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 /* 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; 
- 
        svieira created this gist May 6, 2014 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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