首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

shell 比较两个资料

2012-08-08 
shell 比较两个文件我在shell 中比较两个文件是否一样:diff file1 file2 f1_f2如果f1_f2 是空的话,怎么

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

热点排行