Why conditionals branch script logic
📖 Scenario: You are writing a simple script to check if a folder exists on your computer. Depending on whether the folder is there or not, the script will print a different message. This helps you understand how conditionals can change what your script does.
🎯 Goal: Build a bash script that uses a conditional if statement to check if a folder named my_folder exists. If it exists, print Folder exists. If it does not exist, print Folder does not exist.
📋 What You'll Learn
Create a variable called
folder with the value my_folderCreate a variable called
exists_message with the value Folder existsCreate a variable called
not_exists_message with the value Folder does not existUse an
if statement with the test [ -d "$folder" ] to check if the folder existsPrint
$exists_message if the folder existsPrint
$not_exists_message if the folder does not exist💡 Why This Matters
🌍 Real World
Scripts often need to check if files or folders exist before doing tasks like backups or installations.
💼 Career
Understanding conditionals is essential for automation, system administration, and writing reliable scripts.
Progress0 / 4 steps