Linux 比较 shell 脚本中的文件大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8108244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 07:10:29  来源:igfitidea点击:

Compare file sizes in shell script

linuxshellsh

提问by Tiago Costa

I'm trying to compare the size of two files in shell script but I'm getting a test: 32: 8: unexpected operatorerror.

我正在尝试比较 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 >> .pares
fi

I test the values in $I and $J using echo and the values are correct but I cant compare them...

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

采纳答案by chown

Try using square braces ([]) and -eqlike so:

尝试使用方括号 ( []) 并-eq像这样:

I=`wc -c $i | cut -d' ' -f1`
J=`wc -c $j | cut -d' ' -f1`
if [ $I -eq $J ]
then
? ? ? echo $i $j >> .pares
fi

回答by Aquarius Power

this works on bash

这适用于 bash

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

回答by bartimar

Try

尝试

I=`wc -c "$i"` # always use quoted var
J=`wc -c "$j"`
[[ "$I" == "$J" ]] && echo "$i" "$j" >> "".pares

Always quote variables, because you can have a file name containing a space.

始终引用变量,因为您可以有一个包含空格的文件名。

Despite BASH is case insensitive with variable names, it's better and safer to use different (and longer than one char) name for variables.

尽管 BASH 对变量名称不区分大小写,但为变量使用不同的(且长度超过一个字符)名称会更好也更安全。

回答by Artur Forte

Something like this could work....

像这样的东西可以工作......

#/bin/bash <br>
I=`wc -c < echo $i`
J=`wc -c < echo $j`

if [ $I -eq $J ]; then

 echo $i $j >> .pares

fi

Hugs!

拥抱!