Many ways we can achieve this. Below are some ways to do this.
1)Taking input from user, using read command
2)Through positional parameters
Through read command:
#!/bin/bash
read -p “Please enter two values, with spaces between them: ” NUM1 NUM2
printf “The sum of two given numbers is %dn” $((NUM1+NUM2))
Explanation: The script takes two values from read command pass it to bash/ksh arithmetic operator $(()), which will take care of adding them.
Through positional parameters
#!/bin/bash
[[ $# -ne 2 ]] && { echo “Please execute the script as ./$0 num1 num2”;exit; }
printf “The sum of two given numbers is %dn” $(($1+$2))
Explanation: The first line check for user inputs, and exit’s if user do not given two numbers. The second line will do automatic operations.
In our next post we will see how to add multiple numbers.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018