Your Own Linux

Tech Blog

  • Home
  • About
  • Blog
  • Technology
  • Business
  • Contact Us

Your Own Linux

August 15, 2022 by Skyler Leave a Comment

This post will cover how to use nested if-else statements with bash scripting. Conditions are usually checked from inside the scripts, but it is frequently desirable to check for a condition outside the script and act accordingly. For example, you may need to check for some environmental variable that influences how your script does its job. In such cases, you should use a nested if statement.

Nested if statements are often used with the test command in order to execute one of two different tasks based on whether or not an optional condition is met. It is also possible to make decisions based on multiple conditions using more than two branches.

There are a few different ways to create the if statement in bash. The first is to use the keyword if. The second is using an elif statement. In both cases, the condition will be checked inside the script and any actions that are assigned to a particular branch inside that if statement will be taken when it is true.

The following example creates a simple if-else statement:

#!/bin/bash #This script checks for a specific directory present on the system if [[ ! -d “$1” ]]; then echo “Directory $1 not found. Terminating.” exit 1 fi else echo “Directory $1 exists.” fi

Here is an example of how you would use an elif statement in a script:

#!/bin/bash #This script checks for the existence of two directories if [[ ! -d “$1” ]]; then echo “Directory $1 not found. Terminating.” exit 1 fi elif [[ ! -d “$2” ]]; then echo “Directory $2 not found. Terminating. ” fi else if [[ ! -d “$3” ]]; then echo “Directory $3 not found. Terminating.” exit 1 fi fi

The if might be used to check for a specific condition:

#!/bin/bash #This script checks for a specific file present on the system if [[ ! -f “$1” ]]; then echo “File $1 not found. Terminating.” exit 1 fi else echo “File $1 exists. ” fi

The elif is used to check for a specific condition:

#!/bin/bash #Create a Log Report Template if [[ ! -f “$1” ]]; then echo “Please create LogReportTemplate.txt in the directory $1. ” fi elif [[ ! -f “$2” ]]; then echo “Please create LogReportTemplate2.txt in the directory $2. ” fi else if [[ ! -f “$3” ]]; then echo “Please create LogReportTemplate3.txt in the directory $3. ” fi

The elif can be used to check for multiple conditions, but in order to not affect the script’s overall flow, it should only be used when you want something to happen if some of the conditions are met. In the case of an if-else statement, the test command will work similarly.

Conclusion

Nested if-else statements are often used with the test command in order to execute one of two different tasks based on whether or not an optional condition is met. It is also possible to make decisions based on multiple conditions using more than two branches. In order to not affect the script’s overall flow, it should only be used when you want something to happen if some of the conditions are met.

Filed Under: Blog

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

About Us

Hello there! I’m Skyler a professional tech guy you can count on. I’m here to share with you my broad knowledge when it comes to tech stuff. I do some configuring and coding that might be helpful to you. Read More…

Connect With Us

  • Facebook
  • Instagram
  • LinkedIn
  • Twitter

Latest Post

  • 3 Fintech Trends That Make It Easier to Send Money
  • Top Jeep Upgrades for Beginners
  • Mint Global Marketing Shares: Marketing Technology Trends For 2023 (And Beyond)
  • 5 Ways Poor Cybersecurity Impacts Business
  • Digital Marketing Trends for Business

NewsLetter

Featured

3 Fintech Trends That Make It Easier to Send Money

Top Jeep Upgrades for Beginners

Mint Global Marketing

Mint Global Marketing Shares: Marketing Technology Trends For 2023 (And Beyond)

5 Ways Poor Cybersecurity Impacts Business

Digital Marketing Trends for Business

C:\Users\wajih\Downloads\pexels-photo-1191377.jpeg

10 Benefits of Doing Business in Japan

Things You Need To Know About Subscription Billing

Free Woman Holding Tablet Computer Stock Photo

Five Tech-Based Solutions every Business Needs to Expand

C:\Users\wajih\Downloads\pexels-photo-8475148.jpeg

Technology in Retail: 9 Technologies to Know About

How outsourcing accounting can help start up owners avoid burnout

About Us

Hello there! I’m Skyler a professional tech guy you can count on. I’m here to share with you my broad knowledge when it comes to tech stuff. I do some configuring and coding that might be helpful to you. Read More…

Latest Post

  • 3 Fintech Trends That Make It Easier to Send Money
  • Top Jeep Upgrades for Beginners
  • Mint Global Marketing Shares: Marketing Technology Trends For 2023 (And Beyond)
  • 5 Ways Poor Cybersecurity Impacts Business
  • Digital Marketing Trends for Business

NewsLetter

Connect With Us

  • Facebook
  • Instagram
  • LinkedIn
  • Twitter

Copyright © 2023 Your Own Linux