diff --git a/docs/shells/bash.md b/docs/shells/bash.md index c8a0718..47a3933 100644 --- a/docs/shells/bash.md +++ b/docs/shells/bash.md @@ -1,5 +1,54 @@ # Bash +## arrays + +``` bash +# creates an array variable +ORPHANS=() + +# checks length +if [[ ${#ORPHANS[@]} -eq 0 ]]; then + echo "No orphans found." +else + echo "orphans:" + printf "%s\n" "${ORPHANS[@]}" +fi +``` + +## associative array + +an data structure that lets you store key → value pairs + +``` bash +# clean if already been used +unset PV_PATHS +# declare an associative array, an data structure that lets you store key → value pairs +declare -A PV_PATHS +for name in $(kubectl get pv -o jsonpath='{.items[*].metadata.name}'); do + path=$(kubectl get pv ${name} -o jsonpath='{.spec.local.path}' 2>/dev/null) + # check if path is no empty + [[ -n "${path}" ]] && PV_PATHS["${name}"]="${path}" +done +# print only keys +echo +echo "All Keys:" +printf "%s\n" "${!PV_PATHS[@]}" + +echo +echo "All Values:" +# print only values +printf "%s\n" "${PV_PATHS[@]}" + +echo +echo "Keys|Values:" +# print all +for key in "${!PV_PATHS[@]}"; do + printf "%s|%s\n" "$key" "${PV_PATHS[$key]}" +done +``` + +## misc + ```bash #!/bin/bash script_directory="$(dirname "$(readlink -f "$0")")"