Multiple test commands and if constructs

These type of constructs enable us to execute the second command depending on the success or failure of the first command:

command1    &&    command2 
command1    ||    command2 

Let's write script if_13.sh. In this script, we will ask the user to input two numbers. Then, the if statement will evaluate two expressions. If both are true, then the command after then will be executed; otherwise, commands after else will be called:

#!/bin/bash 
echo "Enter the first number" 
read val_a 
echo "Enter the Second number" 
read val_b 
 
if [ $val_a == 1 ] && [ $val_b == 10 ] 
then 
  echo "testing is successful" 
else 
  echo "testing is not successful" 
fi 

Let's test the program:

    $ chmod +x if_13.sh
    $ ./if_13.sh
  

The following will ...

Get Learning Linux Shell Scripting - Second Edition 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.