Skip to content
Hands-on IT IT Drills

Day 5: Arrays and Options

Goal: Hold lists in arrays, count with associative arrays, read output with mapfile and parse options with getopts. At the end, notes-check checks several endpoints with options and help text. · Duration: approx. 75 min · Prerequisite: Day 4

Briefing

  • An array holds a list of values, each one stays separate – even with spaces. "${liste[@]}" returns all elements, ${#liste[@]} their count.
  • An associative array (declare -A) maps keys to values: ideal for counting.
  • mapfile -t reads the lines of an output into an array.
  • getopts parses short options such as -u url -t 3 -v. Whatever is left afterwards are the actual arguments.
 notes-check -t 2 -v /healthz /notes ─► getopts: timeout=2, verbose=1
                                      └─► array endpunkte=(/healthz /notes)
                                          └─► declare -A ergebnis[ok|fehler]++

Drill 1: Arrays

source ~/sh-lab/setup/env.sh
mkdir -p ~/sh-lab/tag05 && cd ~/sh-lab/tag05
dienste=(db notes-api "nginx proxy")
dienste+=(backup)
echo "Anzahl: ${#dienste[@]}"
echo "erstes: ${dienste[0]}, letztes: ${dienste[-1]}"
for d in "${dienste[@]}"; do printf '[%s] ' "$d"; done; echo
for d in ${dienste[@]}; do printf '[%s] ' "$d"; done; echo
echo "Teil: ${dienste[*]:1:2}"
unset 'dienste[1]'
echo "nach unset: ${dienste[*]} – Indizes: ${!dienste[*]}"

Expected:

Anzahl: 4
erstes: db, letztes: backup
[db] [notes-api] [nginx proxy] [backup] 
[db] [notes-api] [nginx] [proxy] [backup] 
Teil: notes-api nginx proxy
nach unset: db nginx proxy backup – Indizes: 0 2 3

The same lesson as on Day 2 again: without quotes, nginx proxy falls apart into two words. "${liste[@]}" is to arrays what "$@" is to arguments. After unset, the array has a gap – the indices are not renumbered.

Save your progress? Sign up for free

Drill 2: Counting with associative arrays

codes=(200 200 404 201 200 503 404 200)
declare -A anzahl
for c in "${codes[@]}"; do
  anzahl[$c]=$((${anzahl[$c]:-0} + 1))
done
for c in "${!anzahl[@]}"; do echo "$c ${anzahl[$c]}"; done | sort
echo "Gibt es 500? ${anzahl[500]:-nein}"
[[ -v anzahl[404] ]] && echo "404 kam vor"

Expected:

200 4
201 1
404 2
503 1
Gibt es 500? nein
404 kam vor
  • ${!anzahl[@]} returns the keys. Their order is random – hence sort.
  • ${anzahl[$c]:-0} returns 0 for a key that doesn’t exist yet and keeps set -u quiet.
  • [[ -v name[key] ]] checks whether an entry exists.

POSIX: sh has no arrays. The only array there is the arguments, set with set -- a b c.

Save your progress? Sign up for free

Drill 3: mapfile

mapfile -t container < <(docker ps --format '{{.Names}}' | sort)
echo "${#container[@]} Container: ${container[*]}"
mapfile -t -n 2 zeilen < /etc/os-release
printf '%s\n' "${zeilen[@]}"

Expected:

3 Container: notes-db-1 notes-notes-api-1 notes-proxy-1
PRETTY_NAME="Ubuntu 26.04.1 LTS"
NAME="Ubuntu"

-t removes the line ending, -n 2 reads at most two lines. Previously, you would have needed a while read loop for that.

Save your progress? Sign up for free

Drill 4: notes-check with getopts

cat > ~/sh-lab/werkzeug/bin/notes-check <<'EOF'
#!/usr/bin/env bash
# notes-check – checks endpoints of notes
set -euo pipefail
source "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/../lib/lib.sh"

usage() {
  cat <<HILFE
Aufruf: $PROG [-u url] [-t sekunden] [-v] [endpunkt…]
  -u url       Adresse von notes (Standard: \$NOTES_URL)
  -t sekunden  Zeitlimit je Anfrage (Standard: 3)
  -v           jeden Endpunkt einzeln melden
  -h           diese Hilfe
Ohne Endpunkte: /healthz /notes /version
Exit-Code: 0 alle ok, 1 mindestens einer gescheitert, 2 falsche Bedienung
HILFE
}

url="$(notes_url)"
timeout=3
verbose=0
while getopts ":u:t:vh" opt; do
  case "$opt" in
    u) url="$OPTARG" ;;
    t) timeout="$OPTARG" ;;
    v) verbose=1 ;;
    h) usage; exit 0 ;;
    :) die "Option -$OPTARG braucht einen Wert" 2 ;;
    *) die "unbekannte Option -$OPTARG (Hilfe: -h)" 2 ;;
  esac
done
shift $((OPTIND - 1))

need curl
endpunkte=("$@")
((${#endpunkte[@]} > 0)) || endpunkte=(/healthz /notes /version)

declare -A ergebnis=([ok]=0 [fehler]=0)
for e in "${endpunkte[@]}"; do
  code=$(curl -s -o /dev/null -w '%{http_code}' --max-time "$timeout" "$url$e") || true
  if [[ $code == 2?? ]]; then
    ergebnis[ok]=$((ergebnis[ok] + 1))
  else
    ergebnis[fehler]=$((ergebnis[fehler] + 1))
  fi
  ((verbose)) && echo "$code $e"
done
echo "${ergebnis[ok]} ok, ${ergebnis[fehler]} Fehler"
((ergebnis[fehler] == 0))
EOF
chmod +x ~/sh-lab/werkzeug/bin/notes-check
shellcheck -x ~/sh-lab/werkzeug/bin/notes-check && echo sauber
notes-check
notes-check -v /healthz /admin; echo "Exit: $?"
notes-check -x; echo "Exit: $?"
notes-check -t; echo "Exit: $?"
notes-check -h | head -2

Expected:

sauber
3 ok, 0 Fehler
200 /healthz
404 /admin
1 ok, 1 Fehler
Exit: 1
12:30:02 notes-check: FEHLER unbekannte Option -x (Hilfe: -h)
Exit: 2
12:30:02 notes-check: FEHLER Option -t braucht einen Wert
Exit: 2
Aufruf: notes-check [-u url] [-t sekunden] [-v] [endpunkt…]
  -u url       Adresse von notes (Standard: $NOTES_URL)
  • In the optstring ":u:t:vh", a : after a letter means “needs a value”, the : at the very front means “handle errors yourself”: then ? (unknown) or : (value missing) ends up in opt, and the letter in OPTARG.
  • shift $((OPTIND - 1)) removes the processed options; afterwards "$@" contains only the endpoints.
  • || true after curl: an unreachable server should be counted, not end the script.
  • The last line ((ergebnis[fehler] == 0)) is the script’s exit code.
  • In the help text, the here document without '…' replaces variables like $PROG. \$ leaves a $ as it is.

POSIX: getopts exists in every sh. It doesn’t know long options like --timeout; you parse those yourself with case "$1" in --timeout) … and shift.

Save your progress? Sign up for free

Drill 5: Arrays for commands

A command with options that are added depending on the situation belongs in an array – not in a string:

cat > curlopts <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
opts=(-s -o /dev/null -w '%{http_code} %{time_total}s\n')
[[ ${LANGSAM:-0} == 1 ]] && opts+=(--limit-rate 1k)
opts+=(-A "notes check")
curl "${opts[@]}" "$NOTES_URL/healthz"
printf '%q ' curl "${opts[@]}"; echo
EOF
bash curlopts

Expected:

200 0.001234s
curl -s -o /dev/null -w %\{http_code\}\ %\{time_total\}s\\n -A notes\ check 

printf '%q' shows each word the way you would have to type it into the shell: the user agent notes check has remained one argument. In a string variable opts="-A notes check", it would have fallen apart in the call.

Save your progress? Sign up for free

Checkpoint

notes-check -v -t 1 /healthz /notes

Expected:

200 /healthz
200 /notes
2 ok, 0 Fehler
notes-check -u http://127.0.0.1:9 >/dev/null; echo "Exit: $?"

Expected:

Exit: 1
bash -c 'declare -A a=([x]=1); a[y]=2; echo "${#a[@]}"'

Expected:

2

Challenge

notes-check should get an option -q: no output, only the exit code – for cron and until loops.

Note: Three places: optstring, case and the output at the end.

Solution

cd ~/sh-lab/werkzeug/bin
f=notes-check
sed -i 's/":u:t:vh"/":u:t:vqh"/' $f
sed -i 's/^    v) verbose=1 ;;$/&\n    q) quiet=1 ;;/' $f
sed -i 's/^verbose=0$/&\nquiet=0/' $f
sed -i 's/^echo "\${ergebnis\[ok\]} ok/((quiet)) || &/' $f
sed -i 's/^  -v .*$/&\n  -q           keine Ausgabe, nur Exit-Code/' $f
grep -n -E 'quiet|-q ' notes-check
shellcheck -x notes-check && notes-check -q; echo "Exit: $?"
notes-check -q /admin; echo "Exit: $?"

Expected:

12:  -q           keine Ausgabe, nur Exit-Code
22:quiet=0
28:    q) quiet=1 ;;
50:((quiet)) || echo "${ergebnis[ok]} ok, ${ergebnis[fehler]} Fehler"
Exit: 0
Exit: 1

sed -i changes the file in place; in the replacement, & stands for the matched text. It works the same way in an editor – you get to know sed properly on Day 7.

Save your progress? Sign up for free

Enterprise Note

  • Every tool has -h. A script without help is no longer used after three months – or used wrongly.
  • Build commands in arrays, never in string variables with eval. eval runs whatever is in the variable, including injected commands.
  • Check the Bash version: Associative arrays exist from Bash 4 on. macOS still ships Bash 3.2; a script for Macs checks BASH_VERSINFO or points to Homebrew Bash.