Bash Recipes¶
Run command with stdin as an input¶
Kubbectl apply from an inline yaml string.
Conditional run command¶
Run command based on output of another command.
set +e
networks="$(docker network inspect kind-tilt 2>&1)"
set -e
if [[ "$networks" == *"network kind-tilt not found"* ]]; then
docker network create --gateway="100.127.0.1" --ip-range="100.127.0.0/24" --subnet="100.127.0.0/16" kind-tilt
else
echo "kind-tilt network already exists"
fi
Sed¶
Get all on line text after a string¶
E.g. return value after the first occurrence of name:
from a file on Linux.
Replace text in a line, keeping before the match¶
E.g.
Remove all lines starting with string¶
E.g. remove all lines starting with "#@".
Check if a URL is available¶
E.g. check if google.com is contactable. Can be used to check if VPN is connected.
status_code="$(curl --write-out %{http_code} --silent --output -k /dev/null https://www.google.com)"
if [[ "$status_code" -ne 200 ]] ; then
echo "You are not connected to the VPN. Please connect and re-run this script"
else
exit 1
fi
Check if command is in path¶
E.g. check if ytt is available in the shell.
if ! [ -x "$(command -v ytt)" ]; then
echo -e 'ytt CLI not in path.\nSee https://github.com/carvel-dev/ytt' >&2
exit 1
fi
Do something if command fails¶
E.g. ff image is not in local registry copy image over.