Skip to main content
Techlogia — AI and Web Development Berlin

Practise this module for free on a real VM

Sign up for free
 All courses
Free to read – no sign-up

Shell scripting basics

You write Bash scripts with variables, parameters, conditions, loops and exit codes – and finally build a log report for a web server. The checker really runs every script.

Duration: 90 minLevel: BeginnerExercises: 10

First scripts: variables and parameters

A shell script is a text file with commands that Bash runs in order. Everything you type in the terminal can go into a script.

Your working folder is ~/skripte (already created). The checker runs your scripts with bash FILE and compares the output – so always test yourself with bash ~/skripte/FILE or ./FILE.

Exercises

  1. 1. Your first script

    Create hallo.sh in ~/skripte. The first line is the shebang #!/bin/bash, below it echo prints Hallo Welt.

    cd ~/skripte
    nano hallo.sh
    chmod +x hallo.sh
    ./hallo.sh
  2. 2. Variables and command output

    Write variablen.sh:

    1. Create the variable kurs with the value Shell-Scripting (no spaces around =).
    2. Store the output of whoami in the variable benutzer using $(…).
    3. Print:
    Ich lerne Shell-Scripting.
    Angemeldet als: student

    Both values must come from the variables. Skeleton:

    #!/bin/bash
    kurs="Shell-Scripting"
    benutzer=$(____)
    echo "Ich lerne ____."
    echo "Angemeldet als: ____"
  3. 3. Using the parameter $1

    Write gruss.sh that greets the first parameter:

    $ bash gruss.sh Ada
    Hallo Ada!

    The checker runs your script with different names. Skeleton:

    #!/bin/bash
    echo "Hallo ____!"
  4. 4. Counting parameters with $#

    Write parameter.sh. It prints two lines: how many parameters were passed and which one was first. Example: bash parameter.sh rot gruen blau prints Anzahl: 3 and below it Erster: rot.

Conditions, loops and exit codes

Scripts become useful when they can decide and repeat. Every command reports through its exit code whether it worked (0) or not (anything else). echo $? shows the code of the last command.

Exercises

  1. 1. Condition: does the file exist?

    Write datei-check.sh PATH:

    • If the file exists: print vorhanden: PATH, exit code 0.
    • Otherwise: print fehlt: PATH, exit code 1.
    $ bash datei-check.sh /etc/hostname ; echo "Exit: $?"
    vorhanden: /etc/hostname
    Exit: 0

    Skeleton:

    #!/bin/bash
    if [ ____ "$1" ]; then
      echo "vorhanden: $1"
      exit 0
    else
      echo "fehlt: $1"
      exit ____
    fi
  2. 2. Loop: count from 1 to N

    Write zaehle.sh N that prints the numbers 1 to N, one per line, using a loop. Example: bash zaehle.sh 3 prints 1, 2 and 3, each on its own line.

  3. 3. Evaluating exit codes with $?

    Write suche.sh WORD FILE. It searches for the word with grep -q and then evaluates $?:

    • found ($? is 0): print gefunden, exit code 0
    • not found: print nicht gefunden, exit code 1

    Example: bash suche.sh root /etc/passwd prints gefunden.

Project: log report for the web server

/var/log/lab-web/ contains two access logs of a web server. Each line is one request:

198.51.100.23 - - [20/Nov/2026:08:05:03 +0100] "GET /kontakt.html HTTP/1.1" 304 0

Field 1 is the IP address, field 9 the status code. Look at the files with less before you start.

Exercises

  1. 1. Log report project: guard the call

    From now on you build log-report.sh LOGFILE step by step. It analyses the access logs in /var/log/lab-web/. First the guard:

    • Not exactly one parameter: message Aufruf: … on stderr (>&2), exit code 1.
    • File not readable: message on stderr, exit code 2.

    Nothing appears on stdout in either case. Skeleton:

    #!/bin/bash
    if [ $# ____ 1 ]; then
      echo "Aufruf: $0 LOGDATEI" ____
      exit 1
    fi
    if [ ! -r "$1" ]; then
      echo "Datei nicht lesbar: $1" >&2
      exit ____
    fi
  2. 2. Log report project: requests and 404 errors

    Extend log-report.sh. It prints the number of all requests (= lines) and the number of requests with status code 404 – for zugriffe.log the lines Anfragen: 24 and Fehler 404: 3. The checker uses zugriffe.log and zugriffe-alt.log – fixed numbers stand out.

  3. 3. Log report project: most frequent IP address

    Finally log-report.sh prints the IP address with the most requests, for zugriffe.log Top-IP: 198.51.100.23. Only the address, without the counter.

Knowledge check: shell scripting

Six short questions without a VM – works on your phone too.

Related courses

Now practice it yourself

Reading is good – doing is better. Practise this module on a real Linux VM, right in your browser. A free account is all it takes.

Start for free

Lab content under CC BY 4.0 – free to use with attribution (© TechLogia).

How do you like this page?