这是经过 shell 检查和更新的代码。我将非常感谢任何帮助!更具体地说,代码要么不运行,要么运行但会返回错误消息,并且我将无法输入任何作为一顿饭价格的数字。当我尝试运行代码时弹出的错误消息是:stinput:找不到命令,然后是:找不到命令,重复两次。更新:我按照下面用户建议的方式尝试了代码,所以这是更新的代码和错误消息:
#!/bin/bash
NumberofInputs="$1"
total=$(( $1 * $2))
total=$(("$total" / 100))
total=$(("$total" + $3 + "$NumberofInputs"))
echo "The price of the meal is: $1"
echo "The tax for the meal is: $2"
echo "The tip for the meal is: $3"
echo "The total amount paid is: $total"
exit 1
更新:当我删除 user000001 代码的第一部分时,出现此错误: bash: * : 语法错误: 预期操作数 (错误标记为 "* ") bash: "" / 100: 语法错误: 预期操作数 (错误标记为""" / 100") bash: "" + + : 语法错误: 预期操作数 (错误标记为 """ + + ") 但是当我保留 user000001 代码的第一部分时,我收到此错误消息: echo I抱歉,这是不正确的,请仅输入 3 个参数。: 找不到命令
更新:我找到了答案,它删除了引号并将所有变量间隔在一起。感谢大家的帮助!
答案1
要在 bash 中进行算术运算,您需要双括号,位置参数也是$1
$2
如此;
我想你想要
total=$(($1 * $2))
完整的脚本是
$ cat script.sh
#!/bin/bash
if [[ $# != 3 ]]
then
echo "Usage: $0 price tax tip" 1>&2
exit 1
fi
tax=$(( $1 * $2 / 100 ))
total=$(( tax + $3 + $1 ))
echo "The price of the meal is: $1"
echo "The tax for the meal is: $tax"
echo "The tip for the meal is: $3"
echo "The total amount paid is: $total"
$ ./script.sh 10 20 4
The price of the meal is: 10
The tax for the meal is: 2
The tip for the meal is: 4
The total amount paid is: 16