Write a Log Error Summary Script

ShellBeginner
Practice Now

Introduction

Three application logs need the same simple check: count lines that begin with ERROR and print one result per file. Repeating the commands manually is unnecessary when a short loop can handle every .log file.

The preceding guided lab built this exact kind of loop step by step. This challenge asks for one small reusable script, without extra copying, report directories, or error-handling requirements.

Create the Error Summary Script

Current Situation

/home/labex/project/log-automation/logs contains api.log, healthy.log, and worker.log.

Scope

  • Create /home/labex/project/log-automation/process-logs.sh.
  • Its first argument is the directory containing the logs.
  • Process files ending in .log.
  • For each file, count lines beginning with ERROR.
  • Print one line in the format filename errors=count.

Your Goal

Leave one executable script that prints the correct error summary when run as ./process-logs.sh logs.

Acceptance Criteria

  • The script starts with #!/bin/bash and is executable.

  • ./process-logs.sh logs prints:

    api.log errors=1
    healthy.log errors=0
    worker.log errors=2
    

Hints

Which guided example is the closest match?

Review the loop that iterated over "$log_dir"/*.log, counted ^ERROR lines, and printed the basename with its count.

Summary

You turned a familiar log-counting loop into one short reusable Bash script.

✨ Check Solution and Practice