Skip to lesson

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.

Beginner to intermediate·25 min·Everyday workflow·Updated 2026-11-24

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:

text
2026-01-15 10:02:40 ERROR database timeout

the 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 1 when the number of errors is above a limit set with -t, so that cron or 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:

bash
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 -h
result
Log 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] LOGFILE

The 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:

bash
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.log
result
Log report for logs/app.log
Lines:  8
Period: 2026-01-15 10:00:01 to 2026-01-15 10:04:30

Step 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:

bash
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.log
result
Log 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

printf "%-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:

bash
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.log
result
Log 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 ben

Notice || 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:

bash
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: $?"
result
status with limit 5: 0
RESULT: too many errors (2 > 1)
status with limit 1: 1

The 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:

bash
cat log-report.sh
result
#!/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 0

Read 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:

bash
for i in 1 2 3; do cat logs/app.log; done > big.log
./log-report.sh big.log | head -n 6
result
Log 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   15

Ideas to extend it

  • Add a -l LEVEL option 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 md or -f csv is given.
  • Schedule it with cron every 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.

0 / 4 understood
01 Why does the script check its arguments before doing any work?
02 What does sort | uniq -c | sort -nr produce in this project?
03 Why does the script print its report to standard output but its errors to standard error?
04 How can another program use the script's result?

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
Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses