Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Bash if else

farenda 2015-06-28 0

Problem:

Bash if else is the most fundamental control flow statement used almost in every script. In the following Bash tutorial we show how to use if-else on examples!

Solution:

The syntax of Bash if else statement is the following:

if tests; then
  consequent-commands;
[elif more-tests; then
  more-consequents;]
[else alternate-path;]
fi

The syntax sometimes is enough, but in most cases examples are better, so here we go:

#!/bin/bash
# $# is the number of parameters passed to the script
# "-eq" is for numeric comparison and means "equals"
if [ $# -eq 0 ]; then
    echo "No parameters passed to the script."
else
    echo "The params: $*"
fi

One thing worth noting is that test command is in square brackets “[]” and is separated from them by one space on each side.

Test for two conditions:

#!/bin/bash
os=$(uname -s)
# "-n" returns true if given string is not empty
# "==" is for string equality comparison
if [ -n "$os" ] && [ "$os" == "Linux" ]; then
  echo "Got Linux!"
elif [ "$os" == "darwin" ]; then
  echo "I'm on a Mac"
else
  echo "Got unknown OS: $os"
fi

Note that $os variable is within quotes during the tests. It’s needed, because if the var would be empty you would have “[ -n ]” as a test, which is incorrect syntax.

Another important thing is that in Bash commands on the same line have to be separated by semicolon “;”. This is why there’s a semicolon before then part of if statement. If you put then on the second line you won’t need to put semicolon.

In other tutorials we’ll explore different ways to compare things in Bash. Stay tuned! :-)

Share with the World!
Categories Bash Tags bash
Previous: Java add array to list
Next: Java Deque Stack Queue

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored