I want to write a program which can add two numbers.
The program should contain the if else condition which can describe the range of numbers.
This is what i have done so far:
Echo"enter two numbers "
Read num1 num2
Sum=$((num1 + num2))
Echo" The sum is = $sum"
As you can see, your problems were all typographical errors.
echo "enter two numbers "
read num1 num2
if [ $num1 -lt 0 ] ; then
echo num1 out of range
exit
fi
if [ $num2 -lt 0 ] ; then
echo num2 out of range
exit
fi
sum=$((num1 + num2))
echo " The sum is = $sum"
Related
I have tried assigning a variable to calculate two variables using addition but does not work
XML
echo "Enter 1st number"
read num1
echo "Enter 2nd number"
read num2
sum-'expr $num1 + num2'
echo "Summation = $sum"
echo "Enter no"
read num1
if test $num1 -gt 10
then
echo "Number is greater than 10"
fi
if test $num1 -1t 10
then
echo "Number is less than 10"
fi
However, this error always pops up
line 5: sum-expr $num1 + num2: command not
Please help
There are 2 errors in your implementation:
1. sum-'expr $num1 + num2' --> sum=$((num1 + num2))
2. if test $num1 -1t 10 --> if test "${num1}" -lt 10
Here is my suggestion
#!/bin/bash
echo "Enter 1st number"
read -r num1
echo "Enter 2nd number"
read -r num2
sum=$((num1 + num2))
echo "Summation = ${sum}"
echo "Enter no"
read -r num1
if test "${num1}" -gt 10
then
echo "Number is greater than 10"
fi
if test "${num1}" -lt 10
then
echo "Number is less than 10"
fi
#!/bin/bash
read –p “Enter the number you seek ” NUM
for VALUE in $#; do
if [ $VALUE –eq $NUM ]; then COUNT=$((COUNT+1)); fi
done
echo $NUM appeared $COUNT times
Can the above script be modified so that :
input two inputs from the user, instead of just NUM, and count the number of parameters that are equal to or in between the two. For instance, if the user inputs 12 and 20, we would have 5 matches (12, 18, 19, 12, 18). Assume the first input is less than the second (that is, you do not need to worry about the user inputting 20 and then 12)
This is what i am coming up with:
#1/bin/bash
read - p "Enter two numbers ' NUM1 NUM2
for VALUE in $#; do
if [$VALUE <= $NUM1 ] && [ $VALUE => $NUM2 ]; then COUNT=$((COUNT+1))
fi
done
echo we have $COUNT numbers between $NUM1 and $NUM2, including $NUM1 and $NUM2
#1 is a typo for #!.
You need a space after [.
Your conditions are backwards. When value is between num1 and num2, it's greater than or equal to num1, but you have less than or equal.
The numeric comparison tests are -ge and -le, since > and < are I/O redirection operators in shell scripts.
You have to use balanced quotes around a string; if it starts with " it has to end with ", not '.
You should always quote variables unless you have a specific need to allow word splitting of the expansion.
#!/bin/bash
COUNT=0
read - p "Enter two numbers " NUM1 NUM2
for VALUE in "$#"; do
if [ "$VALUE" -ge "$NUM1" ] && [ "$VALUE" -le "$NUM2" ]; then COUNT=$((COUNT+1))
fi
done
echo "we have $COUNT numbers between $NUM1 and $NUM2, including $NUM1 and $NUM2"
I want to print prime numbers using for loop in shell script. Please provide some suggestions.
actually I could able to print odd numbers from range of 1 to 100, but now I am unable to print prime numbers. Provide some script to print prime numbers
# cat prime.sh
#!/bin/bash
for ((i=1; i<=100; i++))
do
output=$(( $i % 2))
if [ $output -ne 0 ]
then
echo "We got odd numbers: $i"
fi
done
The below code returns the list of prime numbers within a specified range.
the below command returns the prime numbers between 10 to 30
./filename.sh 10 30
#!/bin/bash
# Check if a number is prime
# function to return whether prime or not
check_prime() {
current_number=$1
flag=0
i=2
while test $i -le `expr $current_number / 2`
do
if test `expr $current_number % $i` -eq 0
then
flag=1
fi
i=`expr $i + 1`
done
if test $flag -eq 0
then echo $current_number
fi
}
# Assign From and to number
from_number=$1
to_number=$2
for (( number=$from_number; number<=$to_number; number++ ))
do
check_prime $number
done
This code will Print all prime numbers upto given number & also print count of it
hope it helps !!
#!/bin/bash
echo "enter a number upto which you want the prime numbers"
read num
count=0
for (( n=2; n<=$num; n++ ))
do
t=1
if [ $n -lt 2 ]
then echo "Please give other numbers than 0 and 1"
else
#echo $n
for (( i=2; i<$n; i++ ))
do
#echo $n
if (($n%i==0))
then
#echo "$n is not prime number"
t=0
break;
fi
done
#echo $t
if [ $t == 1 ]
then
echo "$n is a prime number"
count=`expr $count + 1`
fi
fi
done
echo "Total prime numbers upto $num are $count "
I have bash script with basic arithmetic operations - Addition, Subtraction, Division and Multiplication.
#! bin/bash
input="yes"
while [[ $input = "yes" ]]
do
PS3="Press 1 for Addition, 2 for subtraction, 3 for multiplication and 4 for division: "
select math in Addition Subtraction Multiplication Division
do
case "$math" in
Addition)
echo "Enter first no:"
read num1
echo "Enter second no:"
read num2
result=`expr $num1 + $num2`
echo Answer: $result
break
;;
Subtraction)
echo "Enter first no:"
read num1
echo "Enter second no:"
read num2
result=`expr $num1 - $num2`
echo Answer: $result
break
;;
Multiplication)
echo "Enter first no:"
read num1
echo "Enter second no:"
read num2
result=`expr $num1 * $num2`
echo Answer: $result
break
;;
Division)
echo "Enter first no:"
read num1
echo "Enter second no:"
read num2
result=$(expr "scale=2; $num1/$num2" | bc)
echo Answer = $result
break
;;
*)
echo Choose 1 to 4 only!!!!
break
;;
esac
done
done
How to make that values for #num1 and #num2 are accepted only if they are numbers in certain range. For example 0 to 10. So if I enter for $num1 or $num2 lets say 500 there will be message to enter valid value?
You can create a simple function to get a number in range:
get_number() {
local lo=$1 up=$2 text=${3:-Enter a number: } num
shopt -s extglob
until
read -p "$text" num
[[ $num = ?(-)+([0-9]) ]] && (( $lo <= 10#$num && 10#$num <= $up ))
do
echo "Invalid input!" >&2
done
echo "$((10#$num))"
}
num1=$(get_number 10 15 "Enter first number: ")
num2=$(get_number -10 20) # use default prompt
Works for integers only. You might also consider inputting the numbers before the case command to avoid redundant code.
num1 =0
num2 =0
I want to print OK if both num1 and num2 are 0 in a single if statement.
In POSIX shell you can do:
#!/bin/sh
if [ $num1 -eq 0 -a $num2 -eq 0 ]; then
echo "ok"
fi
If you can use bash then you can do:
#!/bin/bash
if (( num1 == 0 && num2 == 0)); then
echo "ok"
fi
If you want to save keystrokes and don't mind being a bit cryptic you could also do:
#!/bin/bash
if ! (( num1 | num2 )); then
echo "ok"
fi
Alternatively, you can use case/esac without worrying about different styles of if/else
case "$num1$num2" in
"00" ) echo "ok";;
* ) echo "not ok";;
esac