Write a script that reads a CSV of `username,group` and creates the users idempotently.
IntermediateHands-onBash scripting tasks
Answer
#!/usr/bin/env bash
set -euo pipefail
while IFS=, read -r user group; do
[[ -z "$user" || "$user" == "username" ]] && continue # skip blanks/header
getent group "$group" >/dev/null || groupadd "$group"
if id "$user" &>/dev/null; then
usermod -aG "$group" "$user"
echo "updated $user"
else
useradd -m -s /bin/bash -G "$group" "$user"
echo "created $user"
fi
done < users.csv
Key trick → Check first with id/getent, then act. That makes the script safe to run again, which is the same idea Ansible modules are built on.
More on Bash scripting tasks
Write the safe header you start every Bash script with, and explain each part.BeginnerWrite a script that alerts when any filesystem is above 80% usage.BeginnerParse an Nginx access log and print the top 10 client IPs by request count.BeginnerWrite a backup script that tars a directory with a timestamp and keeps only the last 7 backups.IntermediateWrite a health-check loop that polls a URL until it returns 200, with a timeout.IntermediateParse command-line flags `-e <env>`, `-v` (verbose) and `-h` in a Bash script.Intermediate