Introduction
Imagine you are driving a car using a GPS. The GPS doesn't just give one list of instructions; it reacts to the road. If there is traffic, it re-routes you. If you miss a turn, it recalculates.
In scripting, we call this conditional logic. Without it, a script is just a blind list of commands. With it, the script can inspect the current situation and choose what to do next.
This is one of the foundations of reliable automation. Real systems are not always in the same state, so good scripts must react instead of assuming.
In this lab, you will add a "brain" to your scripts using if and else.
You will teach your script to:
- Check Reality: Look if a file actually exists before trying to open it.
- Make Decisions: Do one thing if the answer is "Yes", and another if "No".
- Check ID: Verify if the user has permission (Root) to run the script.
Check if a File Exists
A good script acts like a careful driver: it looks before it leaps. One of the most common checks in Linux is: "Does this file exist?"
We use the -f flag (File check) inside an if block. Think of if as a security guard. It only lets the code inside pass if the condition is met.
This is your first real example of a script making a decision based on system state. That is a major step from "run commands in order" to "inspect the environment, then choose an action." Much of administration scripting is built on exactly this pattern.
First, go to your project folder:
cd ~/project
Create a new script named check_file.sh:
nano check_file.sh
Type this code. We will check if data.txt exists.
#!/bin/bash
FILE="data.txt"
if [ -f "$FILE" ]; then
echo "$FILE exists."
fi
Decoding the Logic:
if [ ... ]; then: This is the Guard. It asks a question inside the brackets.-f "$FILE": The Question. "Is 'data.txt' a real file?"echo ...: The Action. This only happens if the Guard says "Yes".fi: "Finish If". This tells Linux the conditional section is over.
The bracket form [ ... ] is Bash's common way to run a test. Here it is checking a file property. Linux and Bash provide many such test operators, and learning them is what lets scripts reason about files, strings, numbers, and permissions.
Save (Ctrl+O) and Exit (Ctrl+X).
Make it executable:
chmod +x check_file.sh
Test 1: The Invisible File
Run the script now:
./check_file.sh
Nothing happens. Silence.
Why? Because data.txt doesn't exist. The Guard said "No", so the echo command was skipped completely.
Test 2: The Visible File
Now, create the file:
touch data.txt
Run the script again:
./check_file.sh
Now the Guard says "Yes", and you see:
data.txt exists.
Add an Else Block
Silence is confusing. If a script does nothing, you don't know if it failed or just finished. We need a Plan B.
In logic, this is the "Fork in the Road".
- Path A (if): The file exists. Good.
- Path B (else): The file is missing. Let's fix it.
Let's modify our script to create the missing file automatically.
First, delete the test file so we perform a real test:
rm data.txt
Open your script:
nano check_file.sh
Add the else block as shown below:
#!/bin/bash
FILE="data.txt"
if [ -f "$FILE" ]; then
echo "$FILE exists."
else
echo "$FILE does not exist. Creating it now..."
touch "$FILE"
fi
The Logic:
If if is True -> Do the first part.
If if is False -> Do the else part.
This kind of script is often closer to real operations work than a script that only prints information. Instead of merely reporting that something is missing, it corrects the situation. That is why conditional logic is the basis of many setup and maintenance scripts.
Save and Exit.
Test the Logic
Run the script. data.txt is missing, so it should take Path B:
./check_file.sh
Output:
data.txt does not exist. Creating it now...
Run it again immediately. Now data.txt is there, so it should take Path A:
./check_file.sh
Output:
data.txt exists.
Your script is now "Idempotent"—a fancy word meaning you can run it 100 times, and it will always ensure the correct result (the file exists) without crashing.
That word appears often in DevOps. An idempotent action can be repeated safely because the end state matters more than how many times you run the command. This makes automation more reliable, especially when jobs are retried.
Check for Root Permissions
Some doors in Linux are locked. Only the superuser (Root) has the Key. If you try to run a system-maintenance script as a normal user, it will fail with "Permission Denied".
A smart script checks your ID card before trying to open the door.
In Linux, every user has a number (UID).
- Root is always UID 0.
- Normal users are usually 1000+.
Let's write a script that checks your ID badge.
Create check_root.sh:
nano check_root.sh
Type this code:
#!/bin/bash
if [ "$UID" -eq 0 ]; then
echo "You are running this script as root."
else
echo "You are running this script as a regular user."
fi
Decoding:
$UID: The variable holding your current ID number.-eq: "EQuals". (We use-eqfor numbers,=for words).
This is another example of test syntax. When comparing numbers inside [ ... ], Bash uses operators like -eq, -ne, -lt, and -gt. These are different from string comparisons because the shell needs to know whether it is comparing words or numeric values.
Save and Exit. Make executable:
chmod +x check_root.sh
The Test
Since you are logged in as labex, you are a regular user.
Run it normally:
./check_root.sh
The script sees your non-zero ID and says:
You are running this script as a regular user.
Now, let's borrow the Superuser's cape using sudo:
sudo ./check_root.sh
sudo switches your ID to 0 for just this one command. The script notices and replies:
You are running this script as root.
This is how professional installation scripts work—they refuse to run unless you are root!
The goal is not to demand root for everything, but to check privileges before attempting protected actions. A well-written script fails early with a clear explanation instead of running halfway and producing confusing permission errors later.
Summary
You have just given your scripts a brain!
In this lab, you learned:
- The Guard (
if): Asking questions like "Does this file exist?" (-f). - The Fork (
else): Providing a backup plan if the answer is "No". - The ID Check: Using number comparison (
-eq) to verify user permissions.
Now your scripts are no longer just lists of commands; they are intelligent programs that adapt to their environment.



