如何比较if中的文件大小百分比

问题描述 投票:1回答:1

我试图在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 arithmetic-expressions
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.