Loop Over the Logs
The logs folder is full of .log files. Write report.sh that loops over them and counts the errors in each, then prints a tidy summary.
How it works: the terminal below is a real Linux machine running in your browser. Click it and
type. Tasks tick themselves off as you complete them; questions are answered in the box under each task.
Press Reset any time for a fresh machine. Your password on this machine (for
sudo) is student.
Hints
Test the loop body on one file first: grep -c ERROR logs/app.log.
Quote "$f" so the file name is passed as one word.
Solution explanation
The for loop sets f to each .log file in turn. grep -c ERROR "$f" counts the matching lines in that file, and echo prints the name and count. Redirecting the whole script with > saves the summary to a file instead of the screen.
Show solution
cat <<'EOF' > report.sh
#!/bin/bash
for f in logs/*.log; do
n=$(grep -c ERROR "$f")
echo "$f: $n errors"
done
EOF
chmod +x report.sh
./report.sh
cat logs/*.log | grep -c ERROR
./report.sh > logs/summary.txt
cat logs/summary.txt