Writing Script Programs

πŸ“š Hoofdstuk 25.3: Scriptprogramma's schrijven

──────────────────────────────
πŸ” Command Substitution
──────────────────────────────
β€’ Gebruik command substitution om de uitvoer van een commando op te slaan in een variabele.

Methodes:
- Backticks (ouderwets): `command`
- Aanbevolen: $(command)

Voorbeeld:
date_result=$(date)

──────────────────────────────
βž— Rekenen in Bash
──────────────────────────────
β€’ Alleen gehele getallen:
result=$[25 * 5]

β€’ Met floats via bc (Basic Calculator):
var1=$(echo "scale=4; 3.44 / 5" | bc)

β€’ Voor uitgebreidere berekeningen β†’ gebruik Z shell (zsh)

──────────────────────────────
❗ If-statements en testen
──────────────────────────────
Syntax:
if [ voorwaarde ]; then
# commando’s
fi

Veelgebruikte tests:
n1 -eq n2 β†’ Getallen zijn gelijk
str1 = str2 β†’ Strings zijn gelijk
-e bestand β†’ Bestand bestaat
-d bestand β†’ Is directory
-r bestand β†’ Is leesbaar
-s bestand β†’ Bestaat Γ©n is niet leeg

──────────────────────────────
πŸ”€ Case statement
──────────────────────────────
Gebruik bij meerdere keuzemogelijkheden:

case $variabele in
optie1) commando ;;
optie2 | optie3) commando ;;
*) standaard_commando ;;
esac

──────────────────────────────
πŸ”‚ Loops
──────────────────────────────
For-loop:
for var in lijst; do
echo $var
done

While-loop:
while [ voorwaarde ]; do
commando’s
done

Until-loop:
until [ voorwaarde ]; do
commando’s
done

──────────────────────────────
πŸ”€ Tekstmanipulatie & Wildcards
──────────────────────────────
Wildcards (globbing):
* β†’ willekeurig aantal tekens
? β†’ één willekeurig teken
[abc] β†’ één teken uit die reeks
Voorbeeld: c[au]t β†’ cat of cut

Handige tools:
β€’ read β†’ leest invoer van gebruiker of bestand
β€’ parameter expansion β†’ ${var} om manipulaties toe te passen
β€’ reguliere expressies β†’ voor geavanceerd zoeken/vervangen