Division by zero using shell - shell

I'm writing a shell program using while commands to prevent division by zero and to exit the program when the user types 99. Everytime i run it i get an error for the input of the second number which in turn does not give me the answer. Below is my attempt at this problem:
#!/bin/bash
firstNum=0
secondNum=0
answer=0
while true firstNum != 99, secondNum != 99 do
read -p "Enter first number" firstNum
read -p "Enter second number" secondNum
echo "first num $firstNum"
echo "second Num $secondNum"
if ["$secondNum" = "0"]
then
exit 1
else
echo "first number / second Number = $((firstNum/secondNum))"
echo "Answer = $answer"
fi
do
exit
done
And the following is the error message that i get
./example.sh: 10: ./example.sh: [2: not found
Any help at all is greatly appreciated.
Thanks

Looks like a few typos.
Replace this:
if ["$secondNum" = "0"] then
exit 1
then
echo "1st number / 2nd number = $((firstNum/secondNum))"
echo "Answer = $answer"
fi
with this:
if [ "$secondNum" = "0" ]
then
exit 1
else
echo "1st number / 2nd number = $((firstNum/secondNum))"
echo "Answer = $answer"
fi
Bash is finicky. Ensure then is on the next line and not same line as the if, you also had then instead of else on your 3rd line.

Your code can be simplified
while true; do
read -p "Enter first number: " firstNum
[[ "$firstNum" -eq 99 ]] && break
read -p "Enter second number: " secondNum
case "$secondNum" in
99) break ;;
0) echo "div by zero, try again"; continue ;;
esac
echo "first number / second Number = $(bc -l <<< "$firstNum/$secondNum")"
done
I'm calling out to bc to get floating point arithmetic. With the shell you're limited to integers only.
<<< is a "here-string", a string to pass to bc's stdin

Your code looks pretty good to me, but I changed the POSIX shell test around a bit to test for values less than 99 instead of 99 itself. And there was a stray do near the bottom of the script which I removed. Bash has [[]] style test syntax too which you may want to switch to at some point.
#!/bin/bash
firstNum=0
secondNum=0
answer=0
while [ "$firstNum" -lt 99 -a "$secondNum" -lt 99 ]
do
read -p "Enter first number " firstNum
read -p "Enter second number " secondNum
printf "first num $firstNum\n"
printf "second Num $secondNum\n"
if [ "$secondNum" = 0 ]
then
printf "$secondNum was a zero, exiting!\n"
exit 1
else
printf "first num / second Num = \$(($firstNum / $secondNum))\n"
answer=$(( firstNum / secondNum))
printf "Answer = $answer\n"
fi
done
If you are interested in learning the Bash test syntax [[]] (it's a bit more powerful than POSIX shell's test(1) []) here is an example which uses it:
#!/bin/bash
firstNum=0
secondNum=0
answer=0
while [[ "$firstNum" < 99 && "$secondNum" < 99 ]]
do
read -p "Enter first number " firstNum
read -p "Enter second number " secondNum
printf "first num $firstNum\n"
printf "second Num $secondNum\n"
if [[ "$secondNum" == 0 ]]
then
printf "$secondNum was a zero, exiting!\n"
exit 1
else
printf "first num / second Num = \$(($firstNum / $secondNum))\n"
answer=$(( firstNum / secondNum))
printf "Answer = $answer\n"
fi
done
It may also be advisable to move the tests into the body of the while loop and instead use the convention 'while [[ 1 ]]' to create a continuous loop. Here is an example doing that (using bash test syntax [[]]) and using bc so the script supports floating point arithmetic:
#!/bin/bash
firstNum=0
secondNum=0
answer=0
while [[ 1 ]]
do
read -p "Enter first number " firstNum
read -p "Enter second number " secondNum
printf "first num $firstNum\n"
printf "second Num $secondNum\n"
case "$firstNum" in
99) printf "Invalid values\n"
exit 1
;;
esac
case "$secondNum" in
99) printf "Invalid values\n"
exit 1
;;
0) printf "Invalid values\n"
exit 1
;;
esac
answer=$(printf "$firstNum / $secondNum\n" | bc -l)
printf "Floating point supported answer using bc: = $answer \n"
done

Related

Bash script. Accept integer only if is from range

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.

Bash sum numbers sequence

I'm trying to SUM a sequence.
Asking for a first number and a second bigger number, the result would be like this:
1st number: 2
2nd bigger number: 6
2+3+4+5+6=20
I'm the whole evening stack, I have to use for and seq
#!/bin/sh
echo -n "Enter number 1: "
read A
echo -n "Enter a number greater o equal than $A: "
read B
J=0
if [ $B -ge $A ]
then
for i in `seq $A $B`
do
RES=$i=$i+$J
#I don't know what to do
done
exit 0
else
echo "wrong"
exit 1
fi
exit 0
This prints out that requested part (I think you already know how to add if/else).
read -p "Enter number 1: " a
read -p "Enter a number greater or equal than $a: " b
high=$(( b-1 ))
sum=0
str=""
for i in $(seq $a ${high}); do
str="${str}${i}+"
sum=$(( j+=i ))
done
sum=$(($sum+$b))
str="${str}${b}=${sum}"
echo $str

Error getting an output from a users selection bash shell in Linux

He friends. I am creating a script file to be able to read users input and display an output based on the question i am asking. When i run the choices separately they work fine but cant figure out why they will not display when i put pieces together.
#!/bin/bash
while test $loop = "y"
do
echo "$CLEAR"
clear
tput cup 5 12 ; echo "L- List all wines by name in alphabetical order."
tput cup 6 12 ; echo "C- Count all wines in each type and list all 5 types with their count."
tput cup 10 12; echo "E- Exit the program."
read choice || continue
case $choice in
[Ll]) file="/home/students/domie/wine.txt"; sort -t ":" -k2 $file;; #no output here
[Cc]) ./count ;; #look below for count script
[Ee]) clear; exit ;; #this works when script runs
*) tput cup 14 4; echo "Invalid Code,Press Enter and Try Again"; read choice ;;
esac
done
and the count script:
$vi count
while IFS=":" read -ra line; do
if (( ${line[2]} == 2 )); then
IFS=":" && echo "${line[*]}"
(( count++ ))
fi
done < /home/students/domie/wine.txt
echo "Count = $count"
EDITED:
wine.txt
Try this:
#!/bin /bash
file="wine.txt"
count() {
# Replace below with your counting logic ..
while IFS=":" read -ra line; do
if (( ${line[2]} == 2 )); then
IFS=":" && echo "${line[*]}"
(( count++ ))
fi
done < "$file"
echo "Count = $count"
}
pause() {
read -p "$*"
}
while :; do
clear
echo "L- List all wines by name in alphabetical order."
echo "C- Count all wines in each type and list all 5 types with their count."
echo "E- Exit the program."
read -p "Enter Choice: " choice || continue
case $choice in
[Ll]) sort -t ":" -k2,2 "$file" ; pause 'Press [Enter] key to continue...';;
[Cc]) count ; pause 'Press [Enter] key to continue...';;
[Ee]) exit ;;
*) pause "Invalid Code, Press Enter and Try Again" ;;
esac
done

First Bash script problems with case statement

Just what it says. I'm trying to write a simple bash script from a question in a book. I'm trying to use the case statement, and for all I can tell I am using it correctly. However I keep getting "syntax error near unexpected token '$'in\r' case $i in.
No idea why. I'm also sure there are other problems in my code since this is my first script. The case statement is on line 10. Feel free to correct anything else, the end program will be building a grade data file for student grades, using a simple awk script to compute the average of the grades, and putting everything in the output file.
function getStudentData () {
i=0
while [ $i<5 ]
do
case $i in
[0]) echo -n "Enter student name\n"
read name
;;
[1]) echo -n "Enter Quiz grade\n"
read quiz
checkLimits $quiz
;;
[2]) echo -n "Enter homework grade\n"
read hw
checkLimits $hw
;;
[3]) echo -n "Enter midterm grade\n"
read midterm
checkLimits $midterm
;;
[4]) echo -n "Enter Final grade \n"
read final
checkLimits $final
;;
esac
done
}
function checkLimits ($grade) {
if [ grade <= 100 || grade >= 0 ]; then
$i--
fi
}
if [ $# -lt 2 ]; then
echo "Incorrect number of arguments"
exit 1
fi
#Check awk existance
if [ ! -e $2 ]; then
echo "Error, .awk file does not exist"
exit 1
fi
#flag for data file existing, and awk file
flag=0
#Check for data file existing
if [ ! -e $1 ];then
flag=0
else
flag=1
fi
ans="yes"
while [ $ans == "yes" || $ans == "y" ]
do
echo "Do you want to enter a student record?"
read ans
if [ $ans == "y" || $ans == "yes" ];then
getStudentData
else
echo "we done"
exit 1
fi
done
If you write it as a loop, then you should probably write:
function getStudentData () {
i=0
while [[ $i < 5 ]]
do
case $i in
0) echo -n "Enter student name: "
read name
;;
1) echo -n "Enter Quiz grade: "
read quiz
checkLimits $quiz
;;
2) echo -n "Enter homework grade: "
read hw
checkLimits $hw
;;
3) echo -n "Enter midterm grade: "
read midterm
checkLimits $midterm
;;
4) echo -n "Enter Final grade: "
read final
checkLimits $final
;;
esac
done
}
function checkLimits() {
if [[ $1 -gt 100 || $1 -lt 0 ]]
then ((i--))
fi
}
This fixes a number of problems:
The test condition in the while loop.
The non-obvious use of echo -n to suppress a newline plus the \n to add a newline.
The indentation.
The syntax of the checkLimits() function.
The syntax of the condition in the checkLimits() function.
Uninverts the inverted logic of the condition in the checkLimits() function.
Actually decrements the variable i.
It does not fix the reliance on the global variable $i, which is pretty ugly.
The purpose of the loop must be to go back if there's an error and get the data reentered. That's a clever idea. Unfortunately, you never increment $i, so it doesn't really work.
Here is some working code:
function getStudentData () {
for ((i = 0; i < 5; i++))
do
case $i in
0) echo -n "Enter student name: "
read name
;;
1) echo -n "Enter Quiz grade: "
read quiz
checkLimits $quiz
;;
2) echo -n "Enter homework grade: "
read hw
checkLimits $hw
;;
3) echo -n "Enter midterm grade: "
read midterm
checkLimits $midterm
;;
4) echo -n "Enter Final grade: "
read final
checkLimits $final
;;
esac
done
}
function checkLimits() {
if [[ $1 -gt 100 || $1 -lt 0 ]]
then ((i--))
fi
}
getStudentData
echo "Name = $name"
echo "Quiz = $quiz"
echo "Homework = $hw"
echo "Midterm = $midterm"
echo "Final = $final"
The output from a sample run:
Enter student name: Eileen
Enter Quiz grade: 92
Enter homework grade: 94
Enter midterm grade: 95
Enter Final grade: 97
Name = Eileen
Quiz = 92
Homework = 94
Midterm = 95
Final = 97

Creating a calculator script

I am trying to make a calculator with a bash script.
The user enters a number, chooses whether they wish to add, subtract, multiply or divide. Then the user enters a second number and is able to choose whether to do the sum, or add, subtract, multiply or divide again on a loop.
I cannot rack my head around this right now
echo Please enter a number
read number
echo What operation would you like to perform: 1: Add, 2: Subtract, 3: Multiple, 4: Divide
read operation
case $operation in
1) math='+';;
2) math='-';;
3) math='*';;
4) math='/';;
*) math='not an option, please select again';;
esac
echo "$number $math"
echo Please enter a number
read number2
echo What operation would you like to perform: 1: Add, 2: Subtract, 3: Multiple, 4: Divide, 5: Equals
read operation2
case $operation2 in
1)math2='Add';;
2)math2='Subtract';;
3)math2='Multiply';;
4)math2='Divide';;
5)math2='Equals';;
*)math2='not an option, please select again';;
esac
echo You have selected $math2
exit 0
This is what I have done so far, but can anyone help me work out how to loop back on the calculator?
The lesser-known shell builtin command select is handy for this kind of menu-driven program:
#!/bin/bash
while true; do
read -p "what's the first number? " n1
read -p "what's the second number? " n2
PS3="what's the operation? "
select ans in add subtract multiply divide; do
case $ans in
add) op='+' ; break ;;
subtract) op='-' ; break ;;
multiply) op='*' ; break ;;
divide) op='/' ; break ;;
*) echo "invalid response" ;;
esac
done
ans=$(echo "$n1 $op $n2" | bc -l)
printf "%s %s %s = %s\n\n" "$n1" "$op" "$n2" "$ans"
done
Sample output
what's the first number? 5
what's the second number? 4
1) add
2) subtract
3) multiply
4) divide
what's the operation? /
invalid response
what's the operation? 4
5 / 4 = 1.25000000000000000000
If I was going to get fancy with bash v4 features and DRY:
#!/bin/bash
PS3="what's the operation? "
declare -A op=([add]='+' [subtract]='-' [multiply]='*' [divide]='/')
while true; do
read -p "what's the first number? " n1
read -p "what's the second number? " n2
select ans in "${!op[#]}"; do
for key in "${!op[#]}"; do
[[ $REPLY == $key ]] && ans=$REPLY
[[ $ans == $key ]] && break 2
done
echo "invalid response"
done
formula="$n1 ${op[$ans]} $n2"
printf "%s = %s\n\n" "$formula" "$(bc -l <<< "$formula")"
done
Wrapping Code in a Loop
If you just want to wrap your code in a Bash looping construct, and are willing to hit CTRL-C to terminate the loop rather than do something more fancy, then you can wrap your code in a while-loop. For example:
while true; do
: # Your code goes here, inside the loop.
done
Just make sure to move your unconditional exit statement out of the body of the loop. Otherwise, the loop will terminate whenever it reaches that line.
For your ~/.bashrc:
alias bc="BC_ENV_ARGS=<(echo "scale=2") \bc"
Just use bc, with that alias it automatically works with two decimals instead of integers.
!/bin/bash
PS3="what's the operation? "
declare -A op=([add]='+' [subtract]='-' [multiply]='*' [divide]='/')
while true; do
read -p "what's the first number? " n1
read -p "what's the second number? " n2
select ans in "${!op[#]}"; do
for key in "${!op[#]}"; do
[[ $REPLY == $key ]] && ans=$REPLY
[[ $ans == $key ]] && break 2
done
echo "invalid response"
done
formula="$n1 ${op[$ans]} $n2"
printf "%s = %s\n\n" "$formula" "$(bc -l <<< "$formula")"
done
Please use the following script.
clear
sum=0
i="y"
echo " Enter one no."
read n1
echo "Enter second no."
read n2
while [ $i = "y" ]
do
echo "1.Addition"
echo "2.Subtraction"
echo "3.Multiplication"
echo "4.Division"
echo "Enter your choice"
read ch
case $ch in
1)sum=`expr $n1 + $n2`
echo "Sum ="$sum;;
2)sum=`expr $n1 - $n2`
echo "Sub = "$sum;;
3)sum=`expr $n1 \* $n2`
echo "Mul = "$sum;;
4)sum=`expr $n1 / $n2`
echo "Div = "$sum;;
*)echo "Invalid choice";;
esac
echo "Do u want to continue ?"
read i
if [ $i != "y" ]
then
exit
fi
done
#calculator
while (true) # while loop 1
do
echo "enter first no"
read fno
if [ $fno -eq $fno 2>/dev/null ]; # if cond 1 -> checking integer or not
then
c=1
else
echo "please enter an integer"
c=0
fi # end of if 1
if [ $c -eq 1 ]; #if 2
then
break
fi # end of if 2
done # end of whie 1
while(true) #while loop 2
do
echo "enter second no"
read sno
if [ $sno -eq $sno >/dev/null 2>&1 ] # if cond 3 -> checking integer or not
then
c=1
else
echo "please enter an integer"
c=0
fi # end of if 3
if [ $c -eq 1 ] # if cond 4
then
break
fi # end of if 4
done #2
while(true) # while loop 3
do
printf "enter the operation (add,div,mul,sub) of $fno and $sno\n"
read op
count=0
##addition
if [ $op = add ] #if cond 5
then
echo "$fno+$sno is `expr $fno + $sno`"
#multiplication
elif [ $op = mul ];
then
echo "$fno*$sno is `expr $fno \* $sno`"
#substraction
elif [ $op = sub ]
then
while(true) #while loop 3.1
do
printf "what do you want to do??? \n 1. $fno-$sno \n 2. $sno-$fno"
printf "\n press the option you want to perform?(1 or 2)\n"
read opt
if [ $opt = 1 ] #if cond 5.1
then
echo "$fno-$sno is `expr $fno - $sno`"
break
elif [ $opt = 2 ]
then
echo " $sno-$fno is `expr $sno - $fno`"
break
else "please enter valid options to proceed(1 or 2)";
clear
fi #end of if 5.1
done # end of 3.1
#division
elif [ $op = div ]
then
while(true) # whilw loop 3.2
do
printf "what do you want to do??? \n 1. $fno/$sno \n 2. $sno/$fno"
printf "\n press the option you want to perform?(1 or 2)\n"
read opt
if [ $opt = 1 ] #if cond 5.2
then
echo "$fno divided by $sno is `expr $fno / $sno`"
break
elif [ $opt = 2 ]
then
echo " $sno divided by $fno is `expr $sno / $fno`"
break
else
clear
fi #end of if 5.2
done # end of 3.2
else
echo "valid option please!!!"
count=1
fi # end of if 5
if [ $count -eq 0 ] #if cond 6
then
echo "Do you want to do more ops"
echo "(y/n)"
read ans
clear
if [ $ans = n ] # if 6.1
then
break
fi # end of if 6.1
fi #end of if 6
done #end of while 3

Resources