Scripting Basics
Why Learn Shell & Python Scripting?
- Efficiency: Automate repetitive command-line tasks in seconds.
- Reproducibility: Ensure the same steps run identically every time.
- Portability: Scripts work across environments (Linux, macOS, WSL).
Bash Scripting Fundamentals
Shebang & Execution Permissions
#!/usr/bin/env bash
- Place at the top of your script to select the interpreter.
- Make executable: chmod +x script.sh.
Variables & Parameter Expansion
NAME="MyApp"
echo "Deploying $NAME version ${VERSION:-latest}"
- Use ${VAR:-default} for safe defaults.
- Quote variables ("$VAR") to prevent word-splitting.
Control Structures
if [[ -f "/etc/config.yml" ]]; then
echo "Config exists"
else
echo "Missing config"
exit 1
fi
for file in *.log; do
gzip "$file"
done
- if/then/else, for, while, case blocks provide flow control.