【问题标题】:How to compare percent file size in an if如何比较if中的百分比文件大小
【发布时间】:2019-02-15 11:32:46
【问题描述】:

我正在尝试在 bash shell 中执行此操作。基本上想按百分比比较两个文件的大小。如果 file1 有 90% 不同,那么 file2 会做一些事情:

这是我目前所拥有的:

newsize=$(wc -c <"$newfile")
oldsize=$(wc -c <"$oldfile")

if [[ $(($oldsize * 0.9)) -ge $newsize ]]; then
  echo 'This file is 90% or greater'
else
  echo 'This file is not large enough'
fi

我在令牌“0.9”上得到一个无效的算术运算符错误 任何帮助或指针都会被占用

【问题讨论】:

  • bash 不能自己做浮点数学,只能做整数数学。要么按照 Will 的建议转换为整数数学,要么部署 bcawk 或其他一些工具来提供帮助。

标签: bash arithmetic-expressions


【解决方案1】:

尝试使用整数数学(例如9/10)而不是浮点数。

更新脚本
newsize=525
oldsize=584

if [[ $(($oldsize * 9/10)) -ge $newsize ]]; then
  echo 'This file is 90% or greater'
else
  echo 'This file is not large enough'
fi

示例输出

This file is 90% or greater

【讨论】:

    猜你喜欢
    • 2018-01-05
    • 2013-05-27
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多