L2 Support Engineer · Fintech · Week 3
Week 3 Day 1 Day 2
Week 3 · Day 1 & Day 2

Bash Basics &
Automation Scripts

Instead of typing the same commands every single day, you write a script that does it for you automatically. That's bash scripting — and as an L2 engineer it saves you hours every week.

Day 1 — Bash Basics Day 2 — Automation Variables echo / read Loops if/else
Day 1 Bash Basics
01 The Simple Idea First
Real-life Analogy

Imagine you go to the same coffee shop every morning and order the exact same thing — large coffee, no sugar, extra shot. Every day you say the same words.

Now imagine you write it on a card and just hand it to the cashier. That card is a bash script.

Instead of typing 10 Linux commands every morning to check server health, you write those 10 commands in a script file — and next time you just run one command and it does everything automatically.

What is Bash?

Bash is the language of the Linux terminal. When you type commands like grep, tail, or df -h, you are using Bash. A Bash script is a file that contains a list of these commands so you can run them all at once by executing just one file.

As an L2 engineer you'll use scripts to automate health checks, send alerts, search logs, and restart services — saving hours of manual work every week.

02 Day 1 Concepts
📄
Concept 1
Script Structure — How Every Script Starts
"The blueprint of every bash script"
Structure
What it is: Every bash script has a specific structure. It always starts with a special line called the shebang — this tells Linux "this file is a bash script, run it with bash." Without it, Linux doesn't know how to run your file.
basic-script-structure.sh
#!/bin/bash # ← This is the shebang. ALWAYS first line.
# This is a comment — bash ignores this line
# Comments help YOU understand the script later

# Your commands go below
echo "Script is running..."
df -h
free -h
💡 After creating the script, you must give it permission to run: chmod +x script.sh — then run it with: ./script.sh
📢
Concept 2
echo — Print Text to the Screen
"The print statement of bash — shows output"
Output
What it does: echo prints text to the screen. You use it to show messages, display results, or tell the user what the script is doing at each step. Think of it as the script's way of talking to you.
echo examples
#!/bin/bash

echo "Hello World" # prints: Hello World
echo "Checking disk space now..." # prints the message
echo "Script finished successfully" # use to mark script end

# echo with no text prints a blank line (useful for spacing)
echo ""
📦
Concept 3
Variables — Store and Reuse Values
"A named box that holds a value you can use later"
Variables
What it is: A variable is a name that stores a value. Instead of typing the same thing over and over, you store it in a variable once and use the name. To use a variable's value, put a $ in front of the name.
variables.sh
#!/bin/bash

# Creating a variable — NO spaces around the = sign
SERVER_NAME="Payment-Server-01"
LOG_PATH="/var/logs/payment.log"
MAX_DISK=90

# Using the variable — put $ before the name
echo "Checking server: $SERVER_NAME"
echo "Log file is at: $LOG_PATH"
echo "Max disk allowed: $MAX_DISK%"

# Store command output in a variable
CURRENT_DATE=$(date)
echo "Today is: $CURRENT_DATE"
⚠️ Common mistake: Never put spaces around = when creating a variable. NAME = "Ali" will FAIL. It must be NAME="Ali"
⌨️
Concept 4
read — Take Input from the User
"Ask the user a question and store their answer"
Input
What it does: read pauses the script and waits for the user to type something. Whatever they type gets stored in a variable. This makes your scripts interactive — instead of hardcoding values, you ask the user.
read-input.sh
#!/bin/bash

# Ask for input and store it
echo "Enter the transaction ID to search:"
read TXN_ID

echo "Searching for: $TXN_ID"
grep "$TXN_ID" /var/logs/payment.log

# Ask for server name
echo "Enter server name to check:"
read SERVER
echo "Checking $SERVER now..."
💡 L2 Use: Build a script that asks you for a Transaction ID, then automatically searches all log files for it. One script, one input — saves 5 minutes of manual grep commands.
03 Day 1 Lab — Hello Script

What you'll build

A simple interactive script that greets you by name, shows the current date and time, and displays basic server health — all in one go. Fully performable on your Kali Linux VirtualBox.

🔬 Lab: Build Your First Bash Script

Kali Linux · VirtualBox
01
Open terminal and create the script file
Navigate to your home directory and create a new script file using vi.
terminal
cd ~
vi hello.sh
02
Type the script — press i to enter edit mode first
Copy this exactly into vi. Press i to start typing, then paste or type the script.
hello.sh
#!/bin/bash

# Step 1 — Greet the user
echo "============================="
echo " L2 Engineer Health Check "
echo "============================="
echo ""

# Step 2 — Ask for the engineer's name
echo "What is your name?"
read NAME
echo "Hello $NAME! Starting health check..."
echo ""

# Step 3 — Show date and time
NOW=$(date)
echo "Date and Time : $NOW"
echo ""

# Step 4 — Show disk space
echo "--- Disk Space ---"
df -h
echo ""

# Step 5 — Show RAM
echo "--- Memory Usage ---"
free -h
echo ""

echo "Health check complete. Have a good shift $NAME!"
03
Save the file and give it permission to run
Press Esc, then type :wq to save. Then make it executable.
terminal
# Give the script permission to run
chmod +x hello.sh

# Run it!
./hello.sh
→ Script will ask your name, then show date, disk space and RAM — all in one go ✅
Day 2 Loops & Automation Scripts
04 The Simple Idea First
Real-life Analogy

Think of a security guard doing rounds in a building. Every hour they check the same 10 rooms in the same order. They don't decide differently each time — they follow a fixed pattern.

A loop is that fixed pattern. It tells the script: "do this action for each item in this list" or "keep doing this until the condition changes."

And if/else is the guard's decision: "If the door is locked — move on. If the door is open — raise an alert."

05 Day 2 Concepts
🔀
Concept 5
if / else — Make Decisions in Your Script
"If this is true — do this. Otherwise — do that."
Logic
What it does: if/else lets your script make decisions. If a condition is true, run one block of commands. If not, run a different block. This is how you make intelligent scripts — like "if disk is above 90%, send an alert."
if-else syntax
#!/bin/bash

# Basic structure
if [ condition ]; then
  # commands if condition is TRUE
else
  # commands if condition is FALSE
fi # fi closes the if block — always needed

# Real example — check if a file exists
if [ -f "/var/logs/payment.log" ]; then
  echo "Log file found. Checking for errors..."
  grep "ERROR" /var/logs/payment.log
else
  echo "WARNING: Log file not found!"
fi
comparison operators
# Number comparisons
-eq # equal to → [ $A -eq $B ]
-ne # not equal to → [ $A -ne $B ]
-gt # greater than → [ $A -gt 90 ]
-lt # less than → [ $A -lt 10 ]

# File checks
-f # file exists → [ -f "/path/file" ]
-d # directory exists → [ -d "/path/dir" ]
💡 L2 Use: if [ $DISK -gt 90 ]; then echo "ALERT: Disk critical!" — your script can now automatically alert you when disk is dangerously full.
🔁
Concept 6
for loop — Repeat an Action for Each Item
"Do this same thing for every item in a list"
Loop
What it does: A for loop repeats a block of commands for each item in a list. Instead of writing the same command 10 times for 10 servers, you put the servers in a list and the loop runs the command for each one automatically.
for loop syntax
#!/bin/bash

# Basic structure
for ITEM in list_of_items; do
  # commands to run for each item
done

# Example — check disk space on multiple servers
for SERVER in payment-server db-server api-server; do
  echo "Checking: $SERVER"
  echo "--- done ---"
done

# Example — search multiple log files
for LOGFILE in $(ls /var/logs/*.log); do
  echo "Searching $LOGFILE..."
  grep "ERROR" $LOGFILE
done
💡 L2 Use: After an S1, search for errors across 20 different log files automatically — the loop does it for each file without you typing grep 20 times.
Concept 7
while loop — Keep Running Until Condition Changes
"Keep doing this as long as something is true"
Loop
What it does: A while loop keeps running as long as a condition remains true. Useful for monitoring — keep checking the service every 10 seconds until it comes back up.
while loop syntax
#!/bin/bash

# Keep checking disk every 30 seconds
while true; do
  echo "Disk check at: $(date)"
  df -h
  sleep 30 # wait 30 seconds before next check
done
06 Day 2 Lab — Automate Service Restart Script

What you'll build

A real-world automation script that checks if a service is running, shows disk and memory status, searches logs for errors, and if disk is above a threshold — shows a warning. This is a script you would actually use as an L2 engineer. Fully performable on Kali Linux.

🔬 Lab: Build a Service Health Check & Auto-Alert Script

Kali Linux · VirtualBox
01
Create the script file
Open a new script file in your terminal.
terminal
cd ~
vi health-check.sh
02
Write the full automation script
Press i to enter edit mode, then type this script exactly.
health-check.sh — Full Script
#!/bin/bash

# ========================================
# L2 Engineer — Server Health Check Script
# ========================================

# Variables
DISK_LIMIT=80 # alert if disk above 80%
LOG_FILE="~/payment-service.log"
REPORT_FILE="~/health-report.txt"

echo "========================================="
echo " Server Health Check — $(date)"
echo "========================================="

# STEP 1 — Check disk space with if/else alert
echo ""
echo "[1] Disk Space Check:"
DISK_USED=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
echo "Current disk usage: $DISK_USED%"

if [ $DISK_USED -gt $DISK_LIMIT ]; then
  echo "⚠️ WARNING: Disk above $DISK_LIMIT% — Please clean up logs!"
else
  echo "✅ Disk is OK — below threshold"
fi

# STEP 2 — Check RAM
echo ""
echo "[2] Memory Check:"
free -h

# STEP 3 — Search log file for errors using loop
echo ""
echo "[3] Searching logs for errors..."

if [ -f $LOG_FILE ]; then
  ERROR_COUNT=$(grep -c "ERROR" $LOG_FILE)
  echo "Total errors found: $ERROR_COUNT"
  if [ $ERROR_COUNT -gt 0 ]; then
    echo "⚠️ Errors detected — listing them:"
    grep "ERROR" $LOG_FILE
  else
    echo "✅ No errors found in log"
  fi
else
  echo "Log file not found at $LOG_FILE"
fi

# STEP 4 — Check multiple services using a loop
echo ""
echo "[4] Checking services status:"
for SERVICE in ssh cron; do
  if $(systemctl is-active --quiet $SERVICE); then
    echo "✅ $SERVICE is RUNNING"
  else
    echo "❌ $SERVICE is STOPPED — needs attention!"
  fi
done

echo ""
echo "========================================="
echo " Health check complete."
echo "========================================="
03
Save, give permission, and run
Press Esc → :wq to save. Then make it executable and run it.
terminal
chmod +x health-check.sh
./health-check.sh
→ Script runs all 4 checks automatically — disk alert if above 80%, memory, log errors, service status ✅
04
Schedule it to run automatically every day
Use crontab to schedule the script to run every morning at 8 AM automatically.
crontab
# Open crontab editor
crontab -e

# Add this line — runs health-check.sh every day at 8 AM
0 8 * * * /home/kali/health-check.sh >> /home/kali/health-report.txt
→ Script now runs automatically every morning at 8 AM and saves output to health-report.txt ✅
07 Quick Cheat Sheet — Both Days
⌨️ Bash Scripting Cheat Sheet
#!/bin/bashAlways the first line of every script
echo "text"Print text to the screen
NAME="Ali"Create a variable — no spaces around =
echo $NAMEUse a variable — always $ before name
read VARNAMEAsk user for input and store it
VAR=$(command)Store command output in a variable
if [ $A -gt 90 ]If A is greater than 90
if [ -f "file" ]If file exists
for X in list; doLoop through a list of items
while true; doKeep looping until manually stopped
chmod +x script.shGive script permission to run
./script.shRun the script
08 Real L2 Scenarios
01

Every morning you manually run df -h, free -h, and grep ERROR logs on 5 servers. With a script, you run one command and it checks all 5 servers automatically using a for loop — saving 20 minutes every morning.

02

During an S1, someone asks "how many ERROR lines are in each log file?" Instead of checking each file one by one, your loop script searches all log files in the folder and prints the error count for each — in 5 seconds.

03

You want to be automatically alerted when disk goes above 85%. You write a script with an if/else check, schedule it with crontab to run every hour, and it prints a warning message when the threshold is crossed. No more manual checking.

04

A client reports a transaction failed. You have the TXN ID. Instead of typing the same grep command across 10 log files, your script asks for the TXN ID using read, then loops through all log files and searches for it — giving you the result in seconds.

✅ Week 3 · Day 1 & 2 Outcomes