bash 创建计算器脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14350556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a calculator script
提问by J Homard
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.
我正在尝试用 bash 脚本制作一个计算器。用户输入一个数字,选择是否要加、减、乘或除。然后用户输入第二个数字,并能够选择是否进行求和,或者在循环中再次进行加、减、乘或除。
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?
这是我到目前为止所做的,但是谁能帮我弄清楚如何循环回计算器?
回答by Steven Penny
How about this
这个怎么样
calc ()
{
(( d = ))
echo $d
}
output
输出
$ calc '6 + 2'
8
$ calc '6 - 2'
4
$ calc '6 * 2'
12
$ calc '6 / 2'
3
回答by glenn Hymanman
The lesser-known shell builtin command select
is handy for this kind of menu-driven program:
鲜为人知的 shell 内置命令select
对于这种菜单驱动的程序非常方便:
#!/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:
如果我想使用 bash v4 功能和 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
回答by Todd A. Jacobs
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:
如果您只想将代码包装在Bash 循环结构中,并且愿意按 CTRL-C 来终止循环而不是做一些更花哨的事情,那么您可以将代码包装在一个 while 循环中。例如:
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.
只要确保将无条件exit
语句移出循环体即可。否则,只要到达该行,循环就会终止。
回答by Christian Dior Howard
!/bin/bash
!/bin/bash
PS3="what's the operation? " declare -A op=([add]='+' [subtract]='-' [multiply]='*' [divide]='/')
PS3="操作是什么?" 声明 -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
虽然是真的;do read -p "第一个数字是多少?" n1 read -p "第二个数字是多少?" n2 select ans in "${!op[@]}"; 为“${!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")" 完成
回答by sjas
This calculates uses up to 4 decimals (if needed), works without quotation marks.
此计算最多使用 4 位小数(如果需要),不带引号。
calc ()
{
echo "scale=4;$*" | bc -l
}
Only downside is, you have to escape *
, else the shell will use it for file expansion.
唯一的缺点是,您必须转义*
,否则外壳将使用它进行文件扩展。
Usage:
用法:
calc 1 + 2
calc 3 - 4
calc 44 \* 88
calc 77 / 234
This should do for most cases where you need a calculator fast.
这应该适用于您需要快速计算器的大多数情况。
回答by ahmed
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
回答by warmachine
#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