What if...else

Let's take one of our previous examples and examine it in more detail:

#!/bin/bash    echo "Hello user, please give me a number between 10 and 20: "read user_inputif [ ${user_input} -ge 10 ] && [ ${user_input} -le 20 ]thenecho "Great! The number ${user_input} is what we were looking for!"elseecho "The number ${user_input} is not what we are looking for..."fi  

As an exercise to ease its comprehension, let's try to write it in natural language:

  1. Print a greeting asking for a number between 10 and 20
  2. Read the user input and save it in the user_input variable
  3. If the value of user_input is greater or equal to 10 and the value of user_input is less or equal to 2, then print an OK message to the user
  1. Otherwise (else), if the conditions ...

Get Mastering Bash now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.