比较Shell脚本中的文件大小

Tia*_*sta 5 linux shell sh

我正在尝试比较Shell脚本中两个文件的大小,但是正在测试:32:8:意外的操作员错误。

I=`wc -c $i | cut -d' ' -f1`
J=`wc -c $j | cut -d' ' -f1`
if test $I == $J
then
      echo $i $j >> $1.pares
fi
Run Code Online (Sandbox Code Playgroud)

我使用echo测试$ I和$ J中的值,并且值正确,但是我无法比较它们...

Aqu*_*wer 11

这适用于 bash

if((`stat -c%s "$file1"`==`stat -c%s "$file2"`));then
  echo "do something"
fi
Run Code Online (Sandbox Code Playgroud)

  • 在 Mac 上,这会返回 `stat:非法选项 -- c 用法:stat [-FlLnqrsx] [-f format] [-t timefmt] [file ...]` (2认同)
  • @AquariusPower `stat -f file.ext` 只是在 macOS 上打印文件名 `fiel.ext`。要在 macOS 上获取文件大小(以字节为单位),请使用“stat -f%z file.ext”。 (2认同)

cho*_*own 4

尝试使用方括号 ( []) ,-eq如下所示:

\n\n
I=`wc -c $i | cut -d' ' -f1`\nJ=`wc -c $j | cut -d' ' -f1`\nif [ $I -eq $J ]\nthen\n\xc2\xa0 \xc2\xa0 \xc2\xa0 echo $i $j >> $1.pares\nfi\n
Run Code Online (Sandbox Code Playgroud)\n