What you will build
Step 1: Write rotate.sh to the spec
Create rotate.sh in /workspace. It takes a source directory, a destination directory, and a keep count. It creates a time-stamped gzip tar archive, and it deletes archives beyond the keep count only after tar succeeds.
Write it with a heredoc, using the reference logic from the unit.
cat > rotate.sh <<'EOF'
#!/bin/bash
if [ "$#" -ne 3 ]; then echo "usage: rotate.sh SOURCE DEST KEEP" >&2; exit 2; fi
src=$1; dest=$2; keep=$3
mkdir -p "$dest" || exit 1
name="backup-$(date +%Y%m%d-%H%M%S-%N).tar.gz"
tar -czf "$dest/$name" -C "$src" . || exit 1
i=0
for f in $(ls -1 "$dest"/backup-*.tar.gz | sort -r); do
i=$((i + 1))
if [ "$i" -gt "$keep" ]; then rm -f -- "$f"; fi
done
EOFStep 2: Run three backups and keep two
Create a source directory named src with one file in it. Run rotate.sh three times with keep set to 2, with a one-second pause between runs, so that the archives get different names. Then count the archives in backups.
mkdir -p src && echo data > src/a.txt
bash rotate.sh src backups 2; sleep 1
bash rotate.sh src backups 2; sleep 1
bash rotate.sh src backups 2
ls backups | wc -lThe count printed at the end should be 2.
Files
rotate.sh: the finished script.walkthrough.sh: every step of the reference solution, in order.
How it is checked
Each step in the guided lab has a check script that runs on the lab machine after you finish. It passes only when the files and output it expects are there, so the lab cannot be completed by reading alone.