0
0
Bash Scriptingscripting~20 mins

Logical operators (-a, -o, !) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Logical Operators in Bash Scripts
📖 Scenario: You are writing a simple bash script to check conditions on numbers, similar to checking if a number is in a certain range or if a file exists. This helps automate decisions in scripts.
🎯 Goal: Build a bash script that uses logical operators -a (and), -o (or), and ! (not) to test multiple conditions together.
📋 What You'll Learn
Create a variable num with the value 15
Create a variable file with the value example.txt
Use if statements with logical operators -a, -o, and !
Print messages based on combined conditions
💡 Why This Matters
🌍 Real World
Logical operators help automate decisions in scripts, like checking if files exist or if values meet certain criteria before running commands.
💼 Career
Understanding bash logical operators is essential for system administrators, DevOps engineers, and anyone automating tasks on Linux or Unix systems.
Progress0 / 4 steps
1
Set up variables
Create a variable called num and set it to 15. Create another variable called file and set it to example.txt.
Bash Scripting
Need a hint?

Use = to assign values without spaces, and quotes for the filename.

2
Add a threshold variable
Create a variable called threshold and set it to 10.
Bash Scripting
Need a hint?

Assign 10 to threshold without spaces.

3
Use logical operators in if statements
Write an if statement that checks if num is greater than threshold -a the file named by file does not exist (use ! and -e). Inside the if, print "Number is big and file missing".
Bash Scripting
Need a hint?

Use [ condition ] with spaces, and combine conditions with -a. Use ! -e "$file" to check file absence.

4
Add an else if with -o operator and print output
Add an elif that checks if num is less than or equal to threshold -o the file named by file exists (use -e). Inside this elif, print "Number is small or file exists". Add an else that prints "No conditions met". Finally, run the script so it prints the output.
Bash Scripting
Need a hint?

Use elif [ condition ] with -o for OR. Use else for fallback. Run script to see output.