Bash script to compare two numbers.



  • Take into account the main readme for this section. README first.

    We are only using the -gt and -eq operators. See others available at: https://www.tldp.org/LDP/abs/html/comparison-ops.html

    Code.

    $ cat learnbash.sh 
    #!/bin/bash
    
    read -p "Enter a number between 1 and 10: " Firstnumber
    read -p "Enter a number between 1 and 10: " Secondnumber
    
    
    if [ "$Firstnumber" -gt "$Secondnumber" ]
    then
       echo "Hey, that's not fair!"
    elif [ "$Firstnumber" -eq "$Secondnumber" ]
    then
       echo "Equal pay rules"
    else
       echo "Back to the drawing board"
    fi
    

    Run the script. test all possibilities.

    $ ./learnbash.sh 
    Enter a number between 1 and 10: 3
    Enter a number between 1 and 10: 5
    Back to the drawing board
    $ ./learnbash.sh 
    Enter a number between 1 and 10: 5
    Enter a number between 1 and 10: 2
    Hey, that's not fair!
    $ ./learnbash.sh 
    Enter a number between 1 and 10: 5
    Enter a number between 1 and 10: 5
    Equal pay rules
    


© Lightnetics 2024