Linux Command Line · Lesson 43 of 43
Final project: a log analysis report
Combine everything from the course into one tool: a Bash script that analyses a log file and prints a clear report with counts, errors and users.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will finish the course by building something real. The tool you write reads a log file and prints a short report that a person can act on, and it uses nearly every idea from the previous lessons. When you are done, you will have a script you can adapt to your own logs.
The goal
Given a log file where each line looks like this:
2026-01-15 10:02:40 ERROR database timeoutthe script log-report.sh should print:
- the file name, the number of lines, and the time of the first and last entry;
- how many lines each level (INFO, WARN, ERROR) has;
- the most frequent error messages;
- the users who logged in;
- and exit with status
1when the number of errors is above a limit set with-t, so thatcronor another script can react.
We build it in small steps, running each one before moving on. This is how experienced people write scripts: add a little, test it, repeat.
Step 1: the skeleton and argument checks
Start with the shebang, strict mode, a helper to fail clearly, and the argument handling. getopts parses options such as -t 2:
cd ~/project
cat > log-report.sh <<'SCRIPT'
#!/bin/bash
# log-report.sh - summarise a log file.
set -euo pipefail
die() { echo "error: $*" >&2; exit 2; }
threshold=-1
while getopts "t:h" opt; do
case "$opt" in
t) threshold="$OPTARG" ;;
h) echo "usage: $0 [-t MAX_ERRORS] LOGFILE"; exit 0 ;;
*) die "usage: $0 [-t MAX_ERRORS] LOGFILE" ;;
esac
done
shift $((OPTIND - 1))
[ $# -eq 1 ] || die "usage: $0 [-t MAX_ERRORS] LOGFILE"
log="$1"
[ -r "$log" ] || die "cannot read $log"
[[ "$threshold" =~ ^-?[0-9]+$ ]] || die "-t needs a whole number"
echo "Log report for $log"
SCRIPT
chmod +x log-report.sh
./log-report.sh logs/app.log
./log-report.sh
./log-report.sh missing.log
./log-report.sh -hLog report for logs/app.log
error: usage: ./log-report.sh [-t MAX_ERRORS] LOGFILE
error: cannot read missing.log
usage: ./log-report.sh [-t MAX_ERRORS] LOGFILEThe script now handles a good call, a missing argument, a missing file and a help request. Errors go to standard error and use status 2 for misuse, and -h exits 0. A default threshold of -1 means "no limit".
Step 2: the summary lines
Add the number of lines and the first and last timestamps. awk can read the first two fields of the first and last line, and wc counts the lines. We append these lines to the end of the script:
cat >> log-report.sh <<'SCRIPT'
lines=$(wc -l < "$log")
first=$(head -n 1 "$log" | awk '{print $1, $2}')
last=$(tail -n 1 "$log" | awk '{print $1, $2}')
echo "Lines: $lines"
echo "Period: $first to $last"
SCRIPT
./log-report.sh logs/app.logLog report for logs/app.log
Lines: 8
Period: 2026-01-15 10:00:01 to 2026-01-15 10:04:30Step 3: counts per level
The third field of each line is the level. The classic sort | uniq -c pattern counts them, and awk formats the result:
cat >> log-report.sh <<'SCRIPT'
echo "By level:"
awk '{print $3}' "$log" | sort | uniq -c | awk '{printf " %-6s %d\n", $2, $1}'
SCRIPT
./log-report.sh logs/app.logLog report for logs/app.log
Lines: 8
Period: 2026-01-15 10:00:01 to 2026-01-15 10:04:30
By level:
ERROR 2
INFO 5
WARN 1printf "%-6s %d" pads the level name to six characters, so the numbers line up in a column.
Step 4: repeated errors and users
For the errors, keep only the lines with ERROR, drop the timestamp and level with cut, and count identical messages. For users, grep -o pulls the name out of the login lines:
cat >> log-report.sh <<'SCRIPT'
errors=$(grep -c ' ERROR ' "$log" || true)
echo "Errors: $errors"
if [ "$errors" -gt 0 ]; then
echo "Most frequent error messages:"
grep ' ERROR ' "$log" | cut -d' ' -f4- | sort | uniq -c | sort -nr | head -n 3 | sed 's/^ */ /'
fi
echo "Users who logged in:"
users=$(grep -o 'user [a-z]* logged in' "$log" | awk '{print $2}' | sort -u | tr '\n' ' ')
echo " ${users:-none}"
SCRIPT
./log-report.sh logs/app.logLog report for logs/app.log
Lines: 8
Period: 2026-01-15 10:00:01 to 2026-01-15 10:04:30
By level:
ERROR 2
INFO 5
WARN 1
Errors: 2
Most frequent error messages:
2 database timeout
Users who logged in:
asha benNotice || true after grep -c. With set -e and pipefail, grep finding no matches would end the script, but a log with no errors is a perfectly good result, so we accept that status. The default ${users:-none} prints none when nobody logged in.
Step 5: the exit status
Finally, turn the error count into an exit status, so that other tools can use the result:
cat >> log-report.sh <<'SCRIPT'
if [ "$threshold" -ge 0 ] && [ "$errors" -gt "$threshold" ]; then
echo "RESULT: too many errors ($errors > $threshold)" >&2
exit 1
fi
exit 0
SCRIPT
./log-report.sh -t 5 logs/app.log > /dev/null
echo "status with limit 5: $?"
./log-report.sh -t 1 logs/app.log > /dev/null
echo "status with limit 1: $?"status with limit 5: 0
RESULT: too many errors (2 > 1)
status with limit 1: 1The report went to /dev/null so we could look at the status only. With a limit of 5 the two errors are fine, and with a limit of 1 they are too many, so the script exits with 1 and prints a warning on standard error.
The finished script
Here is the whole file, assembled from the steps above:
cat log-report.sh#!/bin/bash
# log-report.sh - summarise a log file.
set -euo pipefail
die() { echo "error: $*" >&2; exit 2; }
threshold=-1
while getopts "t:h" opt; do
case "$opt" in
t) threshold="$OPTARG" ;;
h) echo "usage: $0 [-t MAX_ERRORS] LOGFILE"; exit 0 ;;
*) die "usage: $0 [-t MAX_ERRORS] LOGFILE" ;;
esac
done
shift $((OPTIND - 1))
[ $# -eq 1 ] || die "usage: $0 [-t MAX_ERRORS] LOGFILE"
log="$1"
[ -r "$log" ] || die "cannot read $log"
[[ "$threshold" =~ ^-?[0-9]+$ ]] || die "-t needs a whole number"
echo "Log report for $log"
lines=$(wc -l < "$log")
first=$(head -n 1 "$log" | awk '{print $1, $2}')
last=$(tail -n 1 "$log" | awk '{print $1, $2}')
echo "Lines: $lines"
echo "Period: $first to $last"
echo "By level:"
awk '{print $3}' "$log" | sort | uniq -c | awk '{printf " %-6s %d\n", $2, $1}'
errors=$(grep -c ' ERROR ' "$log" || true)
echo "Errors: $errors"
if [ "$errors" -gt 0 ]; then
echo "Most frequent error messages:"
grep ' ERROR ' "$log" | cut -d' ' -f4- | sort | uniq -c | sort -nr | head -n 3 | sed 's/^ */ /'
fi
echo "Users who logged in:"
users=$(grep -o 'user [a-z]* logged in' "$log" | awk '{print $2}' | sort -u | tr '\n' ' ')
echo " ${users:-none}"
if [ "$threshold" -ge 0 ] && [ "$errors" -gt "$threshold" ]; then
echo "RESULT: too many errors ($errors > $threshold)" >&2
exit 1
fi
exit 0Read it from top to bottom. Every line is something you learned in this course: the shebang and strict mode, a function, getopts with case, shift, tests with [ ] and [[ ]], command substitution, awk, sort, uniq, cut, sed, tr, a here document, redirection to standard error, and exit codes.
Try it on a bigger log
The script only reads its input, so you can point it at any log with the same layout. Try it on a copy that you made larger:
for i in 1 2 3; do cat logs/app.log; done > big.log
./log-report.sh big.log | head -n 6Log report for big.log
Lines: 24
Period: 2026-01-15 10:00:01 to 2026-01-15 10:04:30
By level:
ERROR 6
INFO 15Ideas to extend it
- Add a
-l LEVELoption that prints only the lines of one level. - Count the errors per minute with
cut -c1-16 | uniq -c, to find bursts. - Accept several files and print one report for each, using
for f in "$@". - Print the report as Markdown or CSV when
-f mdor-f csvis given. - Schedule it with
cronevery morning and mail or save the result. - Run it through
shellcheck, a linter that finds quoting and logic mistakes in shell scripts.
Common mistakes
Writing the whole script before running any of it. Run after every step.
Forgetting || true when a missing match is acceptable. set -e ends the script.
Mixing report text and error messages on one stream. Keep errors on standard error.
Trusting the input format. Real logs have odd lines. Check your assumptions with awk '{print NF}' file | sort | uniq -c.
Exercise
Add the -l LEVEL option described above and test it. Then run the script on a real log from your own machine, for example one under /var/log that you can read, and fix any assumption that does not hold. Finally, write down which lessons of this course each part of your script came from.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.
Final exam and certificate
Finish all 43 lessons to unlock a 15-question exam on Linux Command Line. Pass it and you get a certificate with a public link you can share.
Sign in to take the exam