Skip to content

Instantly share code, notes, and snippets.

@Juravenator
Last active August 3, 2021 07:45
Show Gist options
  • Save Juravenator/c5f4b090f6e43f6a3432592f06e6e07f to your computer and use it in GitHub Desktop.
Save Juravenator/c5f4b090f6e43f6a3432592f06e6e07f to your computer and use it in GitHub Desktop.
Some bash examples, including how to start a script
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
IFS=$'\n\t\v'
cd `dirname "${BASH_SOURCE[0]:-$0}"`
echo test >/dev/null 2>&1 # these
echo test &>/dev/null # all
&>/dev/null echo test # do the same
# echo to stderr
>&2 echo test
#functions
function cleanup() {
rm -rf junk
}
function fail() {
>&2 echo $1 # echo the first arg to stderr
exit ${2:-1} # exit $2 or exit 1
}
cleanup
# variables & default values
X=${1:-"default"} # X=$1, or "default" if $1 not set
Y=${2:-} # Y=$2, or nothing (use to bypass nounset)
: ${Z:="default"} # Z="default" if it was unset before
# https://stackoverflow.com/a/16753536/4415398
# +--------------------+----------------------+-----------------+-----------------+
# | Expression | parameter | parameter | parameter |
# | in script: | Set and Not Null | Set But Null | Unset |
# +--------------------+----------------------+-----------------+-----------------+
# | ${parameter:-word} | substitute parameter | substitute word | substitute word |
# | ${parameter-word} | substitute parameter | substitute null | substitute word |
# | ${parameter:=word} | substitute parameter | assign word | assign word |
# | ${parameter=word} | substitute parameter | substitute null | assign word |
# | ${parameter:?word} | substitute parameter | error, exit | error, exit |
# | ${parameter?word} | substitute parameter | substitute null | error, exit |
# | ${parameter:+word} | substitute word | substitute null | substitute null |
# | ${parameter+word} | substitute word | substitute word | substitute null |
# +--------------------+----------------------+-----------------+-----------------+
# indirection
A=one
B=A
C=${!B} # C=one
# test for empty variable
# this is equivalent to `test -z "$Y"`
# so you can get help with `man test`
if [[ -z "${Y:-}" ]]; then
echo "Y not set"
fi
# does a command exist
if ! CMD_PATH=$(command -v commandname); then
fail "commandname is not installed"
fi
# retrieve exit code from individual pipe sections
# '!' bypasses pipefail
! echo "my command" | grep "whatever"
if [[ ${PIPESTATUS[1]} -ne 0 ]]; then
echo "grep failed" >&2 # echo to stderr
>&2 echo "grep failed" # works too
fi
# start & stop a background process
sleep 10s &
PID=$! # PID of last started background process
! ps -p $PID &> /dev/null # this will fail if background process failed to start
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
fail "no background process to kill" 1
else
kill $PID
fi
# ask for input
DEFAULT_Z="Z"
read -p "Type a Z (default ${DEFAULT_Z}): " MY_Z
MY_Z=${MY_Z:-${DEFAULT_Z}}
# math
NUM=$((NUM+1))
NUM=$((${NUM}**2))
# search and replace in one or more files
FILELIST=$'a.sh\tb.sh'
! sed -i -- "s|my search|${MY_Z}|g" ${FILELIST} &>/dev/null # ignore both stdout and stderr
# search for a regex in a file, only print the match, trim newline at the end
cat myfile | grep -Eo 'http://[a-zA-Z0-9-]*.domain.com' | tr -d '\012\015'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment