Linux shell编程学习笔记44:编写一个脚本,将md5sum命令执行结果保存到变量中,进而比较两个文件内容是否相同

0 前言

在 Linux shell编程学习笔记42:md5sumicon-default.png?t=N7T8https://blog.csdn.net/Purpleendurer/article/details/137125672?spm=1001.2014.3001.5501

中,我们提到编写一个在Linux系统下比较两个文件内容是否相同的脚本。

1 基本思路

基本思路是:

从命令行输入两个文件说明符

用md5sum和cut命令获取两个文件的md5校验值

比较两个文件的md5校验值

如果两个文件的md5校验值相同,说明两个文件内容相同。

否则说明两个文件不同。

其中有两个难点:

1.文件的md5值的获取

2.md5值的比较

对于第1个难点,我们的解决办法是:用cut命令从md5sum的执行结果中把md5校验值提取出来。

关于cut命令的用法可以参考:

Linux shell编程学习笔记43:cut命令icon-default.png?t=N7T8https://blog.csdn.net/Purpleendurer/article/details/135730679?spm=1001.2014.3001.5501

对于第2个难点,我们可以把提取出来的md5校验值保存到变量中。

下面我们来研究一下实现的具体办法。

2 创建测试用的文件

2.1 测试文件a.txt

purpleEnduer @ bash \w $ echo -e "no\tname\tmusic\tsport" > a.txt
purpleEnduer @ bash \w $ echo -e "1\taa\t100\t100" >> a.txt
purpleEnduer @ bash \w $ echo -e "2\tbb\t99\t99" >> a.txt
purpleEnduer @ bash \w $ cat a.txt
no      name    music   sport
1       aa      100     100
2       bb      99      99

 

2.2 测试文件b.txt

我们直接用cp命令将a.txt 复制为b.txt

purpleEnduer @ bash \w $ cp a.txt b.txt
purpleEnduer @ bash \w $ cat b.txt
no      name    music   sport
1       aa      100     100
2       bb      99      99

2.3 测试文件 c.txt

purpleEnduer @ bash \w $ echo -e "no\tname\tmusic\tsport" > c.txt
purpleEnduer @ bash \w $ echo -e "3\tcc\t88\t88" >> c.txt
purpleEnduer @ bash \w $ echo -e "4\tdd\t77\t77" >> c.txt
purpleEnduer @ bash \w $ cat c.txt
no      name    music   sport
3       cc      88      88
4       dd      77      77

3 用cut命令从md5sum的执行结果中提取md5校验值

3.1 用md5sum命令计算文件a.txt的md5校验值

purpleEnduer @ bash \w $ md5sum a.txt
80938ffba186be260c0629fed44ff53a  a.txt

 

md5sum命令计算md5校验值后返回信息的格式是:

md5校验值  文件名

第1部分 md5校验值 和  第2部分 文件名之间是用空格分隔的。

3.2 用cut命令和管道提取md5校验值

我们使用cut命令,指定分隔符是空格,并选择第1个字段

purpleEnduer @ bash \w $ md5sum a.txt | cut -d' ' -f1
80938ffba186be260c0629fed44ff53a

这样我们就把文件a.txt的md5校验值提取出来了。

4 将命令执行结果保存到变量中

我们可以使用如下形式的 shell 命令置换特性,将命令的输出存储到变量中:

方法1:

变量名=$(命令 [命令选项 ...] [参数 ...])

方法2:

变量名=`命令 [命令选项 ...] [参数 ...]`

4.1 用第1种方法将 文件a.txt 的md5校验值保存到变量a

 

purpleEnduer @ bash \w $ a=$(md5sum a.txt | cut -d' ' -f1)
purpleEnduer @ bash \w $ echo $a
80938ffba186be260c0629fed44ff53a 

4.2 用第2种方法将 文件c.txt 的md5校验值保存到变量b

purpleEnduer @ bash \w $ b=`md5sum c.txt | cut -d' ' -f1`
purpleEnduer @ bash \w $ echo $b
d22c52262de65e6e5dcb47a389eb04c8

5 编写通过比较md5校验值判断两个文件内容是否相同的脚本

5.1 创建脚本sf.sh 

sf.sh 的内容如下:

# Show program information
echo -e "\n========\nsamefile v 1.0 by PurpleEndurer\n========\n"

# Check the number of parameters
if [ $# != 2 ]; then
  echo -e "The number of parameters is incorrect.\nUsage: samefile file1 file2"
else

  # Get the md5 checksum of file1
  a=$(md5sum $1 | cut -d" " -f1)
  if [ ${#a} != 32 ]; then
     echo "$1 MD5 checksum error"
     return 1
  fi
  echo "The MD5 checksum of file $1 is:" $a

  # Get the md5 checksum of file2

  b=`md5sum $2  | cut -d" " -f1`
  if [ ${#b} != 32 ]; then
     echo "$2 MD5 checksum error"
     return 2
  fi
  echo "The MD5 checksum of file $2 is:" $b

  # Check whether two strings are equal
  if [ $a = $b ]; then
     echo 'These files is same.'
  else
     echo 'These files is not same.'
  fi
fi

创建命令如下: 

purpleEnduer @ bash ~ $ cp /dev/stdin sf.sh
echo -e "\n========\nsamefile v 1.0 by PurpleEndurer\n========\n"
if [ $# != 2 ]; then
  echo -e "The number of parameters is incorrect.\nUsage: samefile file1 file2"
else
  a=$(md5sum $1 | cut -d" " -f1)
  if [ ${#a} != 32 ]; then
     echo "md5 error"
     exit 1
  fi
  echo "The MD5 of file $1 is:" $a

  b=`md5sum $2  | cut -d" " -f1`
  if [ ${#b} != 32 ]; then
     echo "md5 error"
     exit 2
  fi

  echo "The MD5 of file $2 is:" $b
  if [ $a = $b ]; then
     echo 'These files is same.'
  else
     echo 'These files is not same.'
  fi
fi

purpleEnduer @ bash ~ $ 

由于脚本比较长,所以我们用 cp /dev/stdin sf.sh来创建它。

对这条命令不熟悉的朋友,可以参阅:

Linux shell编程学习笔记14:编写和运行第一个shell脚本hello world!icon-default.png?t=N7T8https://blog.csdn.net/Purpleendurer/article/details/133915687

 注意:在输入所有内容后要按Ctrl+D结束。

5.2 比较a.txt 和 b.txt 内容是否相同

purpleEnduer @ bash ~ $ . sf.sh a.txt b.txt

========
samefile v 1.0 by PurpleEndurer
========

The MD5 checksum of file a.txt is: 040da8e416ea8877d9b91959cd986593
The MD5 checksum of file b.txt is: 040da8e416ea8877d9b91959cd986593
These files is same.

purpleEnduer @ bash ~ $ 

文件a.txt 和 b.txt 的 md5校验值相同,所以两个文件内容相同。

5.3 比较a.txt 和 c.txt 内容是否相同

purpleEnduer @ bash ~ $ . sf.sh a.txt c.txt

========
samefile v 1.0 by PurpleEndurer
========

The MD5 of file a.txt is: 040da8e416ea8877d9b91959cd986593
The MD5 of file c.txt is: 30a0ce6033957f499c6f0ac904781fa0
These files is not same.

purpleEnduer @ bash ~ $ 

文件a.txt 和 c.txt 的 md5校验值相同,所以两个文件内容相同。

5.4 比较a.txt 和 不存在文件 x.txt 内容是否相同

purpleEnduer @ bash ~ $ . sf.sh a.txt x.txt

========
samefile v 1.0 by PurpleEndurer
========

The MD5 checksum of file a.txt is: 8b039d79547840fef49dadb7f3853eae
md5sum: x.txt: No such file or directory
x.txt MD5 checksum error
purpleEnduer @ bash ~ $ 

由于第2个文件x.txt并不存在,所以sf.sh会显示出错信息后退出。

5.5 传递参数数量不正确,会给出出错信息和命令正确用法

purpleEnduer @ bash ~ $ . sf.sh

========
samefile v 1.0 by PurpleEndurer
========

The number of parameters is incorrect.
Usage: samefile file1 file2
purpleEnduer @ bash ~ $ . sf.sh a.txt

========
samefile v 1.0 by PurpleEndurer
========

The number of parameters is incorrect.
Usage: samefile file1 file2
purpleEnduer @ bash ~ $ . sf.sh a.txt b.txt c.txt

========
samefile v 1.0 by PurpleEndurer
========

The number of parameters is incorrect.
Usage: samefile file1 file2
purpleEnduer @ bash ~ $ 

 

没有传递参数,只传递了1个参数,或传递了3个参数,都会给出出错信息和命令正确用法。

6 后记

sf.sh算是我写的第1个具有实用价值的脚本,这是开了一个好头,希望后续能够写出更多更好的实用脚本。

  • 20
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫郢剑侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值