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

这篇具有很好参考价值的文章主要介绍了Linux shell编程学习笔记44:编写一个脚本,将md5sum命令执行结果保存到变量中,进而比较两个文件内容是否相同。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

md5sum -c 值赋给变量,麒麟操作系统,编程资料,Linux世界,linux,学习笔记,shell编程,脚本编程,文件比较,md5sum,cut

0 前言

在 Linux shell编程学习笔记42:md5sumhttps://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命令https://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

 md5sum -c 值赋给变量,麒麟操作系统,编程资料,Linux世界,linux,学习笔记,shell编程,脚本编程,文件比较,md5sum,cut

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

md5sum -c 值赋给变量,麒麟操作系统,编程资料,Linux世界,linux,学习笔记,shell编程,脚本编程,文件比较,md5sum,cut

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

md5sum -c 值赋给变量,麒麟操作系统,编程资料,Linux世界,linux,学习笔记,shell编程,脚本编程,文件比较,md5sum,cut

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

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

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

 md5sum -c 值赋给变量,麒麟操作系统,编程资料,Linux世界,linux,学习笔记,shell编程,脚本编程,文件比较,md5sum,cut

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校验值提取出来了。

md5sum -c 值赋给变量,麒麟操作系统,编程资料,Linux世界,linux,学习笔记,shell编程,脚本编程,文件比较,md5sum,cut

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 

md5sum -c 值赋给变量,麒麟操作系统,编程资料,Linux世界,linux,学习笔记,shell编程,脚本编程,文件比较,md5sum,cut

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

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

md5sum -c 值赋给变量,麒麟操作系统,编程资料,Linux世界,linux,学习笔记,shell编程,脚本编程,文件比较,md5sum,cut

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 ~ $ 

md5sum -c 值赋给变量,麒麟操作系统,编程资料,Linux世界,linux,学习笔记,shell编程,脚本编程,文件比较,md5sum,cut

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

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

Linux shell编程学习笔记14:编写和运行第一个shell脚本hello world!https://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 ~ $ 

md5sum -c 值赋给变量,麒麟操作系统,编程资料,Linux世界,linux,学习笔记,shell编程,脚本编程,文件比较,md5sum,cut

文件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 ~ $ 

md5sum -c 值赋给变量,麒麟操作系统,编程资料,Linux世界,linux,学习笔记,shell编程,脚本编程,文件比较,md5sum,cut

文件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 ~ $ 

md5sum -c 值赋给变量,麒麟操作系统,编程资料,Linux世界,linux,学习笔记,shell编程,脚本编程,文件比较,md5sum,cut

由于第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 ~ $ 

md5sum -c 值赋给变量,麒麟操作系统,编程资料,Linux世界,linux,学习笔记,shell编程,脚本编程,文件比较,md5sum,cut 

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

6 后记

sf.sh算是我写的第1个具有实用价值的脚本,这是开了一个好头,希望后续能够写出更多更好的实用脚本。文章来源地址https://www.toymoban.com/news/detail-846583.html

到了这里,关于Linux shell编程学习笔记44:编写一个脚本,将md5sum命令执行结果保存到变量中,进而比较两个文件内容是否相同的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【运维工程师学习三】Linux中Shell脚本编写

    Shell程序有很多, 如 Korn shell(ksh)、Bourne Again shell(bash)、C shell(包括csh与tcsh) 等等, 各主要操作系统下缺省的shell: AIX下是 Korn Shell Solaris缺省的是 Bourne shell FreeBSD缺省的是 C shell HP-UX缺省的是 POSIX shell Linux缺省的是 Bourne Again shell 但这种在命令行中的命令是即时输出结果的,不

    2024年02月11日
    浏览(54)
  • 【Shell编程练习】编写 shell 脚本,打印 9*9 乘法表

    输出Hello World 通过位置变量创建 Linux 系统账户及密码 监控内存和磁盘容量,小于给定值时报警 猜大小 输入三个数并进行升序排序 编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机状态 运行结果: seq 是一个用于生成数字序列的命令。它的基本语

    2024年01月17日
    浏览(35)
  • 编写一个阿里云DDNS的shell脚本

    首先,您需要获取阿里云的AccessKey ID和AccessKey Secret,以便于通过API进行操作。 然后,可以使用cURL命令,向阿里云的API发送HTTP请求,来实现动态DNS的功能。 以下是一个简单的阿里云DDNS shell脚本的示例: ``` #!/bin/bash access_key_id=\\\" \\\" access_key_secret=\\\" \\\" domain=\\\" \\\" RR=\\\" \\\" ip= curl -s http:/

    2024年02月05日
    浏览(26)
  • shell 编写一个带有进度条的程序安装脚本

    使用 shell 写一个 软件安装脚本,带有进度条 在这个示例中,使用 ANSI 转义序列来实现覆盖原来的打印信息,并保持进度条在同一行显示。通过使用 r 进行回车,然后使用 \\033[K 清除当前行的内容,可以实现覆盖效果。 在 print_progress 函数中,首先清除当前行的内容,然后构

    2024年02月11日
    浏览(31)
  • Linux shell脚本编写

    一、常用shell脚本指令 echo: 输出指定的文本或变量值到标准输出。 read: 从标准输入读取用户输入,并将其保存到指定的变量中。 if: 执行条件语句,如果满足指定条件则执行特定操作,否则执行其他操作。 for: 循环执行特定操作,每次迭代更新变量值。 while: 循环执行

    2024年02月16日
    浏览(34)
  • 一篇文章教会你如何编写一个简单的Shell脚本

    Shell脚本概念 Shell 脚本是一种用于自动化执行一系列命令和操作的脚本文件。它使用的是 Shell 解释器(如 Bash、Korn Shell、Zsh 等)来解释和执行其中的命令。Shell 脚本通常用于编写简单的任务和工作流程,可以帮助我们进行系统管理、批量处理、自动化部署等任务。 以.sh后缀

    2024年02月10日
    浏览(32)
  • Linux shell编程学习笔记27:tput

    除了stty命令,我们还可以使用tput命令来更改终端的参数和功能。 tput 命令的主要功能有:移动更改光标、更改文本显示属性(如颜色、下划线、粗体),清除屏幕特定区域等。  tput [选项] [参数] 命令格式:   tput setab n : 设置背景色,set text attributes background color   tput

    2024年02月05日
    浏览(37)
  • Linux shell编程学习笔记35:seq

    在使用 for 循环语句时,我们经常使用到序列。比如: for i in 1 2 3 4 5 6 7 8 9 10; do echo \\\"$i * 2 = $(expr $i * 2)\\\";  done 其中的 1 2 3 4 5 6 7 8 9 10; 就是一个整数序列 。 为了方便我们使用数字序列,Linux提供了seq命令,这个命令是取自单词 sequence 的前3个字母。比如: for i in $(seq 1 10) ;

    2024年02月04日
    浏览(33)
  • Linux shell编程学习笔记25:tty

    在 1830 年代和 1840 年代,开发了称为电传打字机(teletypewriters)的机器,这些机器可以将发件人在键盘上输入的消息“沿着线路”发送在接收端并打印在纸上。 电传打字机的名称由teletypewriters, 缩短为teletypes,并最终缩短为 TTY。 电传打字机:teletypewriters →  teletypes → t

    2024年02月05日
    浏览(40)
  • Linux shell编程学习笔记40:stat命令

    “程序员必备的面试技巧,就像是编写一段完美的代码一样重要。在面试战场上,我们需要像忍者一样灵活,像侦探一样聪明,还要像无敌铁金刚一样坚定。只有掌握了这些技巧,我们才能在面试的舞台上闪耀光芒,成为那个令HR们心动的程序猿!” 目录 0 前言 1 DOS、Wind

    2024年01月19日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包