modified: docs/shells/bash.md

This commit is contained in:
Márcio Fernandes
2026-07-19 10:54:33 +00:00
parent 1bcc95c592
commit 9545bc0f58
+49
View File
@@ -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")")"