2

Assuming that the inputs are given as command line arguments and if two numbers are not given show a error message as "command line arguments are missing".

Sample output:

addition of 1 and 2 is 3.

4
  • 3
    Show your attempt Commented May 26, 2017 at 7:34
  • echo "Enter two numbers" read num1 num2 sum=expr $num1 + $num2 echo "the sum of $num1 and $num2 is $sum" Commented May 26, 2017 at 7:42
  • What exactly is your problem? Commented May 26, 2017 at 19:30
  • Plz email me teh codez Commented May 29, 2017 at 3:42

5 Answers 5

3
#!/bin/bash
if [ $# -lt 2 ]
then
    echo "command line arguments are missing "
else
    echo $(($1+$2))
fi
Sign up to request clarification or add additional context in comments.

1 Comment

$[...] is an obsolete bash extension. It should never be used anywhere.
3

In awk:

echo 5 5 | awk  '{ print $1 + $2}'
10

Comments

2

actualNumber=720; incrementNo=1;

actualNumber=$(expr "$actualNumber" + "$incrementNo");

echo $actualNumber

Comments

0

DESCRIPTION: This script will read two integer value from users and give output as sum of two values SCRIPT:

#!/bin/bash

echo -n "Enter the first number : "
read num1
echo -n "Enter the second number : "
read num2
sum=`expr $num1 + $num2`
echo "sum of two value is $sum"

RUN:

sh sum.sh

Comments

0

Just to put all in one place.

num1=10 
num2=20 
 
sum=$(( $num1 + $num2 )) 
 
sum=`expr $num1 + $num2` 
 
sum=$[num1+num2] 
 
sum=$(echo $num1 $num2 | awk '{print $1 + $2}') 
 
sum=$(expr $num1 + $num2) 
 
sum=$(expr "$num1" + "$num2") 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.