在BASH建立简单的计算器

时间:2015-07-01 14:20:32

标签: linux bash shell calculator

我正在学习Bash Shell Scripting作为Linux Foundation LFS101x.2的一个部分,并且有一个Lab来创建一个简单的Bash计算器。 实验室详细信息可在此处找到: Lab 5

我正在尝试通过以下方式运行脚本:

$ ./bashShellScriptingLab5.sh s 15 5

错误消息是:

Welcome to Calculator!
./bashShellScriptingLab5.sh: line 39: syntax error near unexpected token `exit'
./bashShellScriptingLab5.sh: line 39: ` exit 0'

这是我的bashShellScriptingLab5.sh:

#!/bin/bash

echo "Welcome to Calculator!"

if [ $# != 3 ];
then
        echo "Usage:"
        echo "Please enter a/s/m/d and two integers"
        exit 1
fi

addition () {
        echo "The result of adding " + $2 + " and " + $3 + " is:"
        d = expr $2 + $3
        echo $d
}

subtraction () {
        echo "The result of subtracting " + $2 + " and " + $3 + " is:"
        d = expr $2 - $3
        echo $d
}

multiplication () {
        echo "The result of multiply " + $2 + " and " + $3 + " is:"
        d = expr $2 * $3
        echo $d
}

division () {
        echo "The result of dividing " + $2 + " and " + $3 + " is:"
        d = expr $2 / $3
        echo $d
}

if [[ "$1" = "a" ]]
then
        addition()
        exit 0
elif [[ "$1" = "s" ]]
then
        subtraction()
        exit 0
elif [[ "$1" = "m" ]]
then
        subtraction()
        exit 0
elif [[ "$1" = "d" ]]
then
        division()
        exit 0
else
        echo "Usage:"
        echo "Please enter a/s/m/d and two integers"
        exit 1
fi

3 个答案:

答案 0 :(得分:2)

在这里退出一些错误,我将浏览它们,然后展示一个工作脚本的示例。

首先,您似乎对函数的工作方式做了一些假设。

调用函数不需要()

addition()

此外,您正在尝试在函数中使用全局位置参数,因为它们不具备自己的功能,因此对函数的调用应该传递您想要的内容

addition $2 $3

考虑到这一点,函数内部也必须改变

echo "The result of adding " + $1 + " and " + $2 + " is:"

正如您所看到的,我们现在使用$ 1和$ 2,因为我们使用函数的第一个和第二个参数,而不是脚本!

在函数内部还有更多问题

d = expr $2 + $3

空格在bash中有用,因此会干扰=符号。该命令读作d(function / file / script / exe / whatever),然后equals是一个参数。因此,=和双方之间不能有空格,所以它应该写成

d=expr $2 + $3

虽然这会因expr之后的空格而导致编译错误。所以我们需要在子shell中运行它以将其分配给d

d=$(expr $2 + $3)

虽然我个人会选择bash算法

d=$(($2 + $3))

因此,如果您在脚本中更改了所有这些内容,它应该可以正常工作,那么就可以将其填充更多但是时间不够用。

工作代码

#!/bin/bash

echo "Welcome to Calculator!"

if [ $# != 3 ];
then
        echo "Usage:"
        echo "Please enter a/s/m/d and two integers"
        exit 1
fi

addition () {
        echo "The result of adding " + $1 + " and " + $2 + " is:"
        d=$(($1 + $2))
        echo $d
}

subtraction () {
        echo "The result of subtracting " + $2 + " and " + $3 + " is:"
        d=$(($1-$2))
        echo $d
}

multiplication () {
        echo "The result of multiply " + $1 + " and " + $2 + " is:"
        d=$(($1*$2))
        echo $d
}

division () {
        echo "The result of dividing " + $1 + " and " + $2 + " is:"
        d=$(($1/$2))
        echo $d
}

if [[ "$1" = "a" ]]
then
        addition $2 $3
        exit 0
elif [[ "$1" = "s" ]]
then
        subtraction $2 $3
        exit 0
elif [[ "$1" = "m" ]]
then
        multiplication $2 $3
        exit 0
elif [[ "$1" = "d" ]]
then
        division $2 $3
        exit 0
else
        echo "Usage:"
        echo "Please enter a/s/m/d and two integers"
        exit 1
fi

答案 1 :(得分:1)

要在bash中调用一个函数,你不需要那些parens,那些就是把你扔掉的东西。而只是做

<img ng-src="/images/{{player.status}}.png" alt="" id="status" popover='{{player.detail}}' title='Player Status: {{player.detailedStatus}}' popover-trigger='mouseenter' placement='bottom'>

另外,对于选项&#34; m&#34;你曾经再次调用减法,我将其改为乘法

这会让你超过这个错误,我想你会在那之后发现更多

答案 2 :(得分:0)

简单计算器 1.0

clear
echo "(exp=**, div=/, mult=*, add=+, sub=-)"
echo "\"a/b\" returns only quotient (use no spaces)"
echo "\"a / b\" returns quotient and remainder (use spaces)"
echo " "
echo "Welcome to my Simple Calculator"
echo " "
read -p 'Enter a simple calculation: ' -a sC
answer=$((${sC[0]} ${sC[1]} ${sC[2]}))
echo " "
echo " "
if [ "${sC[1]}" != "/" ]
then echo "The answer is equal to $answer"
else answerRemainder=$((${sC[0]} % ${sC[2]}))
echo "The answer is equal to $answer remainder $answerRemainder"
fi
echo " "
echo " "
echo "Thank you for using Simple Calculator 1.0"
echo "Have a nice day!"

两个表达式之间没有空格返回没有余数的计算:

(exp=**, div=/, mult=*, add=+, sub=-)
"a/b" returns only quotient (use no spaces)
"a / b" returns quotient and remainder (use spaces)

Welcome to my Simple Calculator

Enter a simple calculation: 10/4


The answer is equal to 2


Thank you for using Simple Calculator 1.0
Have a nice day!

“/”之间的空格将返回余数:

(exp=**, div=/, mult=*, add=+, sub=-)
"a/b" returns only quotient (use no spaces)
"a / b" returns quotient and remainder (use spaces)

Welcome to my Simple Calculator

Enter a simple calculation: 10 / 4


The answer is equal to 2 remainder 2


Thank you for using Simple Calculator 1.0
Have a nice day!

您可以在第一个表达式中创建不带空格的表达式,然后乘以包含加、减、乘或除的第二个表达式:

Enter a simple calculation: 1000/4/2/5 * 4+1
    
The answer is equal to 101


Enter a simple calculation: 1000/4/2/5 *4-2
The answer is equal to 98

Enter a simple calculation: 1000/4/2/5 * 4*2
The answer is equal to 200

Enter a simple calculation: 1000/4/2/5 * 4/2
The answer is equal to 50

但是,简单计算器不支持包含任何未用括号括起来的计算的第二个表达式的余数除法: 如果将第二个表达式与计算相除,则第二个表达式必须用括号括起来才能获得正确的结果

Enter a simple calculation: 1000/4/2/5 / (4/2)


The answer is equal to 12 remainder 1 

第二个表达式的括号是必需的!

1000/4/2/5 / 4/2 <---没有括号会返回不可预测的结果。 答案应该是 12 余数 1,但会返回 3 余数 0。

Enter a simple calculation: 1000/4/2/5 / 4/2


The answer is equal to 3 remainder 0

在这种情况下对于余数除法,第二个表达式的括号是必要的。