• Skip to main content
  • Skip to primary sidebar
  • Home
  • WordPress
  • web Hosting
  • linux
  • mysql
  • nginx
  • apache2
  • devops

Raju Ginni

wordpress tutorials seo hosting etc

You are here: Home / Linux sysadmin tutorials linux system administrator / If-Else Statements in Shell Scripting

If-Else Statements in Shell Scripting

The if statement allows the script to make decisions based on conditions. It can be used with elif (else-if) and else to handle multiple conditions.

Table of Contents

Toggle
    • πŸš€ Key Takeaways
  • 1️⃣ Basic If-Else Syntax
  • 2️⃣ Example: Checking a Number
  • 3️⃣ Example: Checking if a File Exists
  • 4️⃣ Example: Checking User Permissions
  • 5️⃣ Example: Nested If Conditions
  • 6️⃣ Example: Checking if a User is Root
  • 7️⃣ Example: Checking Internet Connectivity

πŸš€ Key Takeaways

βœ… Use if [ condition ] for decision-making
βœ… Use elif for multiple conditions
βœ… Use else for a default action
βœ… Test files, numbers, and strings with built-in operators
βœ… $? helps check command success/failure


1️⃣ Basic If-Else Syntax

bash
if [ condition ]; then
# Code executes if condition is true
elif [ another_condition ]; then
# Code executes if elif condition is true
else
# Code executes if no condition is true
fi

2️⃣ Example: Checking a Number

bash
#!/bin/bash

echo -n "Enter a number: "
read num

if [ $num -gt 10 ]; then
echo "The number is greater than 10."
elif [ $num -eq 10 ]; then
echo "The number is exactly 10."
else
echo "The number is less than 10."
fi

πŸ“Œ Operators Used:

  • -eq β†’ Equal to

  • -ne β†’ Not equal to

  • -gt β†’ Greater than

  • -lt β†’ Less than

  • -ge β†’ Greater than or equal to

  • -le β†’ Less than or equal to


3️⃣ Example: Checking if a File Exists

bash
#!/bin/bash

echo -n "Enter a filename: "
read file

if [ -f "$file" ]; then
echo "The file '$file' exists."
else
echo "The file '$file' does not exist."
fi

πŸ“Œ File Test Operators:

  • -f filename β†’ Checks if the file exists

  • -d directory β†’ Checks if the directory exists

  • -r filename β†’ Checks if the file is readable

  • -w filename β†’ Checks if the file is writable

  • -x filename β†’ Checks if the file is executable


4️⃣ Example: Checking User Permissions

bash
#!/bin/bash

echo -n "Enter filename: "
read file

if [ ! -e "$file" ]; then
echo "File does not exist."
elif [ -r "$file" ] && [ -w "$file" ]; then
echo "You have read and write permissions for $file."
elif [ -r "$file" ]; then
echo "You have only read permission for $file."
elif [ -w "$file" ]; then
echo "You have only write permission for $file."
else
echo "You do not have permission for $file."
fi

πŸ“Œ ! -e checks if the file does not exist.


5️⃣ Example: Nested If Conditions

bash
#!/bin/bash

echo -n "Enter a number: "
read num

if [ $num -gt 0 ]; then
if [ $num -lt 10 ]; then
echo "The number is positive and less than 10."
else
echo "The number is positive and greater than or equal to 10."
fi
else
echo "The number is zero or negative."
fi


6️⃣ Example: Checking if a User is Root

bash
#!/bin/bash

if [ "$USER" == "root" ]; then
echo "You are the root user."
else
echo "You are not the root user."
fi

πŸ“Œ String Comparison Operators:

  • = β†’ Equal to

  • != β†’ Not equal to

  • -z string β†’ Checks if string is empty

  • -n string β†’ Checks if string is not empty


7️⃣ Example: Checking Internet Connectivity

bash
#!/bin/bash

ping -c 1 google.com &> /dev/null

if [ $? -eq 0 ]; then
echo "Internet is working!"
else
echo "No internet connection."
fi

πŸ“Œ $? stores the exit status of the last executed command (0 = success, non-zero = failure).


Primary Sidebar

Linux sysadmin tutorials linux system administrator

  • top 10 apt & apt-get commands (most used) apt vs apt-get
  • If-Else Statements in Shell Scripting
  • linux commands pdf (files & Directories, zip & unzip process, search etc)
  • Find Files with Specific Text on Linux grep find command
  • linux performance tuning inode limit file descriptors tco, kernel etc
  • Variables and Data Types in Shell Scripting
  • Top 10 most used Cat commands with examples (create, view, append files)
  • Ip tables / ufw / firewall d commands for block port ip rate limiting
  • Top 10 zip / tar commands to compress & extract files in linux
  • TOP 10 mv & cp commands in linux to move & copy files in Linux
  • Top 10 GREP Commands in linux to search files directory words strings
  • lsof netstat commands to know listening ports in linux 3 ways
  • Upgrade Ubuntu from 18.04 (19.10) to 20.04 LTS command line or gui server | desktop
  • 3 Ways (SCP, rsync, Sftp) linux server migration between two remote server apache nginx
  • linux system specs commands (CPU, Memory, Disk )speed, type. manufacture
  • linux sysctl command tweaks & hardening
  • linux security limits.conf deciding user limits process limits for nginx server
  • ulimit linux unlimited command unlimto set & know user limits open files file descriptor max user process etc.
  • red hat linux certification cost jobs salary syllabus courses fees
  • ufw firewall commads allow port enable disable ubuntu 20.04
  • ddos attack prevention
  • change ssh port in linux - avoid sshd ddos attacks
  • ping command
  • memcached install ubuntu wordpress
  • check linux version (lsb_release -a) ubuntu debian 32 or 64 bit
  • rsync command linux with examples comparison to scp
  • how to uninstall package in linux ubuntu rpm, yum apt-get
  • increase open file limit linux File descriptor ft nginx , mysql, lemp
  • remove repository ubuntu
  • htop commad memory details virtual vs shard vs resident
  • chown command in Linux with Examples
  • Kill PHP process
  • VIrtual Memory vs RSS Memory vs Shared memory in Linux
  • oom killer fixing it by configuration linux ubuntu
  • Install Lemp nginx mysql php fpm Stack on Debian 11 with repository
  • connect two remote servers linux command line
  • auto start after oom killer Mysql & php fpm nginx etc ubuntu wth systemd or cron job
  • load average Linux 1, 5, 15 min 2,4,8 cores explained
  • Control Structures in Shell Scripting
  • Shell Scripting Roadmap for Beginners to Advanced
  • awk commands with practical examples
  • Shell Scripting Tutorial for Beginners πŸš€
  • find Command in Linux with Examples
  • sed Command in Linux with Examples (Beginner to Advanced)
  • Linux Text processing commands in with Examples
  • linux disk management commands
  • fdisk command in linux with examples
  • how to add a new disk in linux
  • Linux mount Command with Examples
  • fstab options with examples
  • Top 50 Shell Scripting Interview Questions and Answers
  • Linux Networking Interview Questions and Answers
  • Linux Networking Commands Cheat Sheet with Examples pdf
  • Netstat & SS Commands cheat sheet with examples Interview Questions
  • Nmap Cheat Sheet – Network Scanning & Security
  • Bash Brackets ([], (), {}, $( ), $(( ))) – Types, Uses & Examples

hi i am raju ginni, primalry i manage wordpress websites on GCP cloud platform as a cloud engineer, and create content on passionate things.
you can follow me on youtbe

Β© 2025 - All Rights Reserved Disclaimer & Privacy Policy