DevOps/Cloud By GK
  • DevOps/Cloud Introduction
  • 🚩AWS
    • Create AWS Account
    • Launch EC2 Instance
  • 🚩Linux
    • User Managment
      • Adding the User to Sudo group
      • visudo
    • Directory / File Management
      • Vi
      • Changing Permissions (chmod)
      • Filtering IO/File
    • Memory Management
    • Process Management
    • Network Management
    • Service Management
    • Environmental Variables
      • $PATH
    • Utilities
    • SHELL PROGRAMMING
      • Hello World
      • Variables
      • Command Line arguments
      • Conditional Statements
      • Array
      • Loops
      • Functions
      • AWK
      • sed
      • Example Useful Scripts
      • Basic -> Advanced
  • 🚩git
    • Introduction
    • Creating GitHub Account
    • Installation
      • Git Bash In Windows
      • Git In VS Code
      • Git In Eclipse
      • Git CLI in Linux
  • Creating Git Remote Repository
  • Git Commands L -> R
  • Git Commands L <- R
  • Git Branch Commands
  • Using SSH (Password Less)
  • 🚩maven
    • Introduction
    • Installation
    • POM.xml
    • Goals
  • 🚩SonarQube
    • Introduction
    • Sonar Cloud
    • Creating Project In SonarCloud
    • Installing Sonar-Scanner
    • Triggering Scan
  • 🚩Nexus
    • Introduction
    • Installation of Nexus
    • Creating Repository
    • Uploading Artifacts to Repository
  • 🚩Jenkins
    • Introduction
    • Installation of Jenkins
    • First Job In Jenkins
    • Git + Jenkins
      • WebHook
    • Maven + Jenkins
    • Sonar-Scanner + Jenkins
    • Nexus + Jenkins
Powered by GitBook
On this page
  1. Linux
  2. SHELL PROGRAMMING

Conditional Statements

Here's an example of a shell script that demonstrates conditional statements:

#!/bin/bash

# Get user input
echo "Enter a number:"
read number

# Conditional statements
if [ "$number" -gt 0 ]; then
    echo "The number is positive."
elif [ "$number" -lt 0 ]; then
    echo "The number is negative."
else
    echo "The number is zero."
fi

In this script, we have the following conditional statement-related operations:

  • Get User Input: We use the read command to prompt the user to enter a number, and store the input in the number variable.

  • Conditional Statements: We use the if, elif, and else keywords to create conditional blocks. Inside the blocks, we use square brackets [ ] to enclose the conditions. In this example, we check if the number is greater than zero ("$number" -gt 0), less than zero ("$number" -lt 0), or equal to zero (default case). Depending on the condition that evaluates to true, the corresponding message is printed.

  • elif (Else If): If the first condition is false, the script checks the next condition using elif. You can have multiple elif blocks to test additional conditions.

  • else: If none of the previous conditions are true, the else block is executed.

When you run the script, it prompts you to enter a number. Based on the number entered, it evaluates the conditions and displays the appropriate message indicating whether the number is positive, negative, or zero.

For example, if you enter 10, the output will be:

The number is positive.

If you enter -5, the output will be:

The number is negative.

And if you enter 0, the output will be:

The number is zero.

You can customize and expand this script by adding more conditions, nested if-else statements, and additional actions based on the conditions. Conditional statements allow you to make decisions and control the flow of execution in your shell scripts based on specific conditions.

PreviousCommand Line argumentsNextArray

Last updated 2 years ago

🚩