Practise this module for free on a real VM
Sign up for freeShell 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.
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. Your first script
Create
hallo.shin~/skripte. The first line is the shebang#!/bin/bash, below itechoprintsHallo Welt.cd ~/skripte nano hallo.sh chmod +x hallo.sh ./hallo.sh2. Variables and command output
Write
variablen.sh:- Create the variable
kurswith the valueShell-Scripting(no spaces around=). - Store the output of
whoamiin the variablebenutzerusing$(…). - Print:
Ich lerne Shell-Scripting. Angemeldet als: studentBoth values must come from the variables. Skeleton:
#!/bin/bash kurs="Shell-Scripting" benutzer=$(____) echo "Ich lerne ____." echo "Angemeldet als: ____"- Create the variable
3. Using the parameter $1
Write
gruss.shthat greets the first parameter:$ bash gruss.sh Ada Hallo Ada!The checker runs your script with different names. Skeleton:
#!/bin/bash echo "Hallo ____!"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 blauprintsAnzahl: 3and below itErster: 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. 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: 0Skeleton:
#!/bin/bash if [ ____ "$1" ]; then echo "vorhanden: $1" exit 0 else echo "fehlt: $1" exit ____ fi- If the file exists: print
2. Loop: count from 1 to N
Write
zaehle.sh Nthat prints the numbers 1 to N, one per line, using a loop. Example:bash zaehle.sh 3prints1,2and3, each on its own line.3. Evaluating exit codes with $?
Write
suche.sh WORD FILE. It searches for the word withgrep -qand then evaluates$?:- found (
$?is 0): printgefunden, exit code 0 - not found: print
nicht gefunden, exit code 1
Example:
bash suche.sh root /etc/passwdprintsgefunden.- found (
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 0Field 1 is the IP address, field 9 the status code. Look at the files with less before you start.
Exercises
1. Log report project: guard the call
From now on you build
log-report.sh LOGFILEstep 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- Not exactly one parameter: message
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 – forzugriffe.logthe linesAnfragen: 24andFehler 404: 3. The checker useszugriffe.logandzugriffe-alt.log– fixed numbers stand out.3. Log report project: most frequent IP address
Finally
log-report.shprints the IP address with the most requests, forzugriffe.logTop-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 freeLab content under CC BY 4.0 – free to use with attribution (© TechLogia).
