读书人

shell 比较两个资料

发布时间: 2012-08-08 14:32:45 作者: rapoo

shell 比较两个文件
我在shell 中比较两个文件是否一样:
diff file1 file2 > f1_f2

如果f1_f2 是空的话,怎么检查啊?

在shell 里怎么check 啊?

我写的:
if [ $f1_f2 == 0 ]; then
echo " test ok"
else
echo " please check.."
fi

我发现有问题啊,就算file1 和file2 内容一样还是echo " please check.."

有啥改进的地方啊?

[解决办法]
如果文件名叫f1_f2 那么这样判断是否为空

if [ -z f1_f2 ]
then
echo "空"
else
echo "非空"
fi

或者不用diff file1 file2 > f1_f2 直接比较file1 file2即可
if diff file1 file2 >/dev/null
then
echo "file1 and file2 are the same"
else
echo "file1 and file2 are not the same"
fi

读书人网 >UNIXLINUX

热点推荐