linux-用BASH构建简单的计算器

我从Linux Foundation LFS101x.2的一部分中学习Bash Shell脚本,并且有一个实验室来创建一个简单的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

解决方法:

在这里退出一些错误,我将遍历它们,然后显示一个工作脚本示例.

首先,您似乎对功能如何工作做出了一些假设.

调用函数不需要()

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
上一篇:linux – 使用awk或sed从ifconfig解析数据?


下一篇:微信小程序实现计算器功能