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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.