我正在从LinuxFoundationLFS101x.2学习Bash脚本,并有一个实验室来创建一个简单的Bash计算器。实验室的详细信息可以在这里找到:实验室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发布于 2015-07-01 14:58:44
在这里退出一些错误,我将运行它们,然后展示一个工作脚本的示例。
首先,您似乎对函数的工作方式做了一些假设。
对函数的调用不需要()
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/随便什么),然后等于是其中的一个参数。因此,您不能在=和双方之间有空格,因此应该将其写为
d=expr $2 + $3虽然由于expr.So后的空格,这仍然会导致编译错误,但我们需要在子still中运行它,以便将它分配给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发布于 2015-07-01 14:26:53
要在bash中调用函数,您不需要父函数,这就是将您抛出的原因。取而代之的是做
if [[ "$1" = "a" ]]
then
addition
exit 0
elif [[ "$1" = "s" ]]
then
subtraction
exit 0
elif [[ "$1" = "m" ]]
then
multiplication
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另外,对于"m“选项,您再次调用减法,我将其改为乘法。
这会让你通过这个错误,我想你会在那之后找到更多
发布于 2021-01-02 04:37:09
简单计算器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在本例中,余数除法为,第二个表达式的括号是必要的。
https://stackoverflow.com/questions/31163999
复制相似问题