Bash Projects
Project 1: Server Health Checker Combines: functions, conditions, exit codes, file handling. #!/usr/bin/env bash set -euo pipefail …
Project 1: Server Health Checker Combines: functions, conditions, exit codes, file handling. #!/usr/bin/env bash set -euo pipefail …
Command Injection: The #1 Bash Security Risk # DANGEROUS: user input concatenated directly into a command read -p "Enter filename: " …
The Standard Production Script Header #!/usr/bin/env bash set -euo pipefail IFS=$'\n\t' # safer default word-splitting (newline/tab …
kubectl Output Formats for Scripting kubectl get pods -o json # full JSON, parse with jq kubectl get pods -o …
Writing a Proper Docker Entrypoint Script #!/usr/bin/env bash set -euo pipefail # Wait for a dependency before starting the main process …
Prerequisites: CLI Configured, jq Installed aws sts get-caller-identity # confirm which account/role your credentials belong to aws …
Tailing and Filtering Logs Live tail -f /var/log/app.log | grep --line-buffered "ERROR" # --line-buffered keeps grep piping live, …
Running a Remote Command ssh [email protected] "systemctl restart myapp" ssh -i ~/.ssh/deploy_key [email protected] …
Waiting for a Service to Become Available #!/usr/bin/env bash wait_for_port() { local host="$1" port="$2" …
The Crontab Format * * * * * command │ │ │ │ │ │ │ │ │ └── day of week (0-6, Sunday=0) │ │ │ └──── month (1-12) │ │ └────── day of month …
What Signals Are (Recap) Signals are how the OS or another process asks a running process to do something — most commonly, to stop. Scripts …
Running a Command in the Background long_task.sh & # runs in the background, script continues immediately echo "Started with PID …
set -x: Trace Every Command as It Runs #!/usr/bin/env bash set -x # print each command (with expanded variables) before executing it …
Exit Codes: The Universal Success/Failure Signal Every command returns a numeric exit status when it finishes: 0 means success, any non-zero …
Why This Matters for Bash Scripting Bash itself has weak native string-manipulation tools for complex tasks — real scripts almost always …
The Three Standard Streams flowchart LR STDIN["stdin (0)"] --> CMD[Command] CMD --> STDOUT["stdout (1)"] CMD --> STDERR["stderr (2)"] Every …
Checking Files Before Using Them FILE="config.yaml" if [ -f "$FILE" ]; then echo "Exists and is a regular file" fi [ …
Positional Parameters #!/usr/bin/env bash # ./deploy.sh myapp production echo "Script name: $0" echo "First arg: $1" # myapp …
Defining and Calling Functions greet() { echo "Hello, $1!" } greet "World" # "Hello, World!" # Alternative syntax …
Indexed Arrays SERVERS=("web1" "web2" "web3") SERVERS[3]="web4" # append at a specific index …
for Loops # Iterate over a fixed list for env in dev staging production; do echo "Deploying to $env" done # Iterate over a numeric …
Basic if / elif / else ENVIRONMENT="staging" if [ "$ENVIRONMENT" = "production" ]; then echo "Deploying to …
Arithmetic Operators A=10 B=3 echo $((A + B)) # 13 echo $((A - B)) # 7 echo $((A * B)) # 30 echo $((A / B)) # 3 — integer division, no …
Concatenation FIRST="Hello" SECOND="World" GREETING="$FIRST $SECOND" # simple concatenation via adjacency …
Basic Input: read echo "Enter your name:" read NAME echo "Hello, $NAME" read -p "Enter your name: " NAME # prompt on …
Declaring & Using Variables NAME="deploy" echo $NAME # unquoted — works, but risky (see quoting below) echo "$NAME" # …
What Is Bash Scripting? Bash (Bourne Again SHell) is both an interactive shell and a scripting language. A Bash script is just a text file …
Problem Statement Your team has 20 dev/staging EC2 instances that run 24/7 but are only used during business hours (8 AM – 8 PM). Each …