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.
Things you can do with bash
# Looping per line of input
some | command | sequence | while read var1 var2; do
# something here
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
for f in *; do
if [[ -d "$f" ]]; then
pushd "$f";
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
# 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
# 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'
#!/usr/bin/env bash -f
shopt -s extglob
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"
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;;
# 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;;
-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
done
echo "$option_a and $option_b"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment