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个具有实用价值的脚本,这是开了一个好头,希望后续能够写出更多更好的实用脚本。

1. 什么是shell脚本Shell脚本是一种编程语言,它是在Unix和Linux操作系统使用的一种脚本语言。它可以帮助用户自动化任务,并且可以运行一系列命令Shell脚本通常以.sh扩展名结尾。 2. 为什么要学习shell脚本学习shell脚本可以帮助你自动化任务,提高工作效率。Shell脚本也可以帮助你编写小工具,方便你自己或其他人使用。Shell脚本还可以帮助你更好地理解Linux和Unix操作系统。 3. 如何编写一个简单的shell脚本? 首先,在命令输入命令nano test.sh,创建一个名为test.sh的文件。然后,在文件输入以下内容: #!/bin/bash echo "Hello World" 接着,按下Ctrl + X,然后按下Y,最后按下Enter,保存并退出文件。接下来,您需要在命令输入以下命令: chmod +x test.sh ./test.sh 这将使test.sh文件执行,并运行脚本。在命令,您应该看到输出“Hello World”。 4. shell脚本的注释是什么? 注释是用于向脚本添加说明和文档的文本。在Shell脚本,注释以“#”开头。注释不会被脚本解释器执行,但可以帮助其他人更好地理解脚本。 5. 如何在shell脚本使用变量变量一个用于存储值的占位符。在Shell脚本,您可以使用以下语法来定义变量: my_variable="Hello World" 您可以使用echo命令来输出变量的值: echo $my_variable 6. 如何在shell脚本使用条件语句? 在Shell脚本,您可以使用条件语句来执行基于条件的操作。以下是一个示例条件语句: if [ $my_variable = "Hello World" ] then echo "The variable contains Hello World" else echo "The variable does not contain Hello World" fi 7. 如何在shell脚本使用循环? 在Shell脚本,您可以使用for循环或while循环来执行重复的操作。以下是一个示例for循环: for i in 1 2 3 4 5 do echo $i done 以上代码将输出数字1到5。 8. 如何在shell脚本使用函数? 在Shell脚本,您可以使用函数来组织和重复使用代码。以下是一个示例函数: function say_hello { echo "Hello World" } 您可以通过以下方式调用函数: say_hello 9. 如何从shell脚本读取用户输入? 在Shell脚本,您可以使用read命令来从用户那里读取输入。以下是一个示例: echo "What is your name?" read name echo "Hello $name" 以上代码将提示用户输入他们的名字,并输出“Hello”后跟用户的名字。 10. 如何在shell脚本使用命令行参数? 在Shell脚本,您可以使用$1、$2、$3等变量来访问命令行参数。例如,以下是一个示例脚本,它接受两个命令行参数并将它们相加: #!/bin/bash sum=$(($1 + $2)) echo "The sum of $1 and $2 is $sum" 您可以使用以下命令来运行脚本并传递两个参数: ./test.sh 2 3 以上代码将输出“The sum of 2 and 3 is 5”。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

紫郢剑侠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值