shell - shell中如何乘以小数点

标签 shell

price=22.22
qty=33
let "total_sales=$price*$qty"

它给了我一个错误

invalid arithmetic operator

我可以将这两个值相乘吗?

最佳答案

使用 awk 进行浮点运算,因为某些 shell,包括 bash 不支持浮点运算。

awk -v price=22.22 -v qty=33 'BEGIN{total_sales=(price*qty); print total_sales;}'
733.26

或者)如果您的变量是在 shell 中定义的,您可以将它们导入到 Awk 中,如下所示。注意,shellawk 下的变量是不同的。

price="22.22"
qty="33"

awk -v price="${price}" -v qty="${qty}" 'BEGIN{total_sales=(price*qty); print total_sales}'
733.26

将其存储在变量中

price="22.22"
qty="33"
unset total_sales
total_sales=$(awk -v price="${price}" -v qty="${qty}" 'BEGIN{print (price*qty)}')

关于shell - shell中如何乘以小数点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41695634/

相关文章:

PHP shell_exec()、exec() 和 system() 仅返回部分输出

Linux:如何将每个文件移动到相应命名的文件夹中

xcode - 如何在 applescript 中使用 do shell 脚本?

linux - 如何测试远程服务器是否在 Linux 中启动?

git - 如何将 Bitbucket post-commit Hook shell 脚本正确转换为 Powershell 脚本?

Bash 从列名中检索列号

linux - 如何打印特定行和列中的数字

linux - 如何在运行时检查进程然后退出否则删除并安装包

regex - pgrep 多个进程名称

bash - 将 stderr/stdout 消息发送到函数并捕获退出信号