在 BASH 中构建简单的计算器

Building simple calculator in BASH

我正在学习 Bash Shell 脚本编写作为 Linux Foundation LFS101x.2 的一个部分,并且有一个实验室可以创建一个简单的 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 " +  + " and " +  + " is:"
        d = expr  + 
        echo $d
}

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

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

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

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

要调用 bash 中的函数,您不需要括号,这些会让您失望。而只是做

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

此外,对于选项"m",您又调用了减法,我将其更改为乘法

这会让你克服这个错误,不过我想你会在之后发现更多

在这里排除一些错误,我将 运行 解决它们,然后展示一个工作脚本的示例。

首先,您似乎对函数的工作原理做出了一些假设。

调用函数不需要 ()

addition()

您还尝试在您的函数中使用全局位置参数,但它们无法正常工作,因为它们有自己的参数,因此对函数的调用应该传递您想要的内容

addition  

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

echo "The result of adding " +  + " and " +  + " is:"

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


函数内部还有一些问题

d = expr  + 

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

d=expr  + 

尽管由于 expr.So 之后的空格,这仍然会导致编译错误,但我们需要在子 shell 中 运行 将其分配给 d

d=$(expr  + )

虽然我个人会选择 bash 算术

d=$(( + ))

因此,如果您在脚本中更改所有这些内容,它应该可以正常工作,本来可以再填充一点,但是 运行 没时间了。


工作代码

#!/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 " +  + " and " +  + " is:"
        d=$(( + ))
        echo $d
}

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

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

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

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

简易计算器 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!"

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

(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!
“/”之间的

Space将return余数:

(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 <---没有括号会 return 不可预测的结果。 答案应该是12余1,但是会return3余0.

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


The answer is equal to 3 remainder 0

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