0 前言
在?Linux shell編程學(xué)習(xí)筆記42:md5sumhttps://blog.csdn.net/Purpleendurer/article/details/137125672?spm=1001.2014.3001.5501
中,我們提到編寫(xiě)一個(gè)在Linux系統(tǒng)下比較兩個(gè)文件內(nèi)容是否相同的腳本。
1?基本思路
基本思路是:
從命令行輸入兩個(gè)文件說(shuō)明符
用md5sum和cut命令獲取兩個(gè)文件的md5校驗(yàn)值
比較兩個(gè)文件的md5校驗(yàn)值
如果兩個(gè)文件的md5校驗(yàn)值相同,說(shuō)明兩個(gè)文件內(nèi)容相同。
否則說(shuō)明兩個(gè)文件不同。
其中有兩個(gè)難點(diǎn):
1.文件的md5值的獲取
2.md5值的比較
對(duì)于第1個(gè)難點(diǎn),我們的解決辦法是:用cut命令從md5sum的執(zhí)行結(jié)果中把md5校驗(yàn)值提取出來(lái)。
關(guān)于cut命令的用法可以參考:
Linux shell編程學(xué)習(xí)筆記43:cut命令https://blog.csdn.net/Purpleendurer/article/details/135730679?spm=1001.2014.3001.5501
對(duì)于第2個(gè)難點(diǎn),我們可以把提取出來(lái)的md5校驗(yàn)值保存到變量中。
下面我們來(lái)研究一下實(shí)現(xiàn)的具體辦法。
2 創(chuàng)建測(cè)試用的文件
2.1 測(cè)試文件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?測(cè)試文件b.txt
我們直接用cp命令將a.txt 復(fù)制為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è)試文件 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的執(zhí)行結(jié)果中提取md5校驗(yàn)值
3.1 用md5sum命令計(jì)算文件a.txt的md5校驗(yàn)值
purpleEnduer @ bash \w $ md5sum a.txt
80938ffba186be260c0629fed44ff53a ?a.txt
?
md5sum命令計(jì)算md5校驗(yàn)值后返回信息的格式是:
md5校驗(yàn)值? 文件名
第1部分 md5校驗(yàn)值 和? 第2部分 文件名之間是用空格分隔的。
3.2 用cut命令和管道提取md5校驗(yàn)值
我們使用cut命令,指定分隔符是空格,并選擇第1個(gè)字段
purpleEnduer @ bash \w $ md5sum a.txt | cut -d' ' -f1
80938ffba186be260c0629fed44ff53a
這樣我們就把文件a.txt的md5校驗(yàn)值提取出來(lái)了。
4?將命令執(zhí)行結(jié)果保存到變量中
我們可以使用如下形式的 shell 命令置換特性,將命令的輸出存儲(chǔ)到變量中:
方法1:
變量名=$(命令 [命令選項(xiàng) ...] [參數(shù) ...])
或
方法2:
變量名=`命令 [命令選項(xiàng) ...] [參數(shù) ...]`
4.1 用第1種方法將?文件a.txt 的md5校驗(yàn)值保存到變量a
?
purpleEnduer @ bash \w $ a=$(md5sum a.txt | cut -d' ' -f1)
purpleEnduer @ bash \w $ echo $a
80938ffba186be260c0629fed44ff53a?
4.2 用第2種方法將?文件c.txt 的md5校驗(yàn)值保存到變量b
purpleEnduer @ bash \w $ b=`md5sum c.txt | cut -d' ' -f1`
purpleEnduer @ bash \w $ echo $b
d22c52262de65e6e5dcb47a389eb04c8
5 編寫(xiě)通過(guò)比較md5校驗(yàn)值判斷兩個(gè)文件內(nèi)容是否相同的腳本
5.1 創(chuàng)建腳本sf.sh?
sf.sh 的內(nèi)容如下:
# 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
創(chuàng)建命令如下:?
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
fipurpleEnduer @ bash ~ $?
由于腳本比較長(zhǎng),所以我們用?cp /dev/stdin sf.sh來(lái)創(chuàng)建它。
對(duì)這條命令不熟悉的朋友,可以參閱:
Linux shell編程學(xué)習(xí)筆記14:編寫(xiě)和運(yùn)行第一個(gè)shell腳本hello world!https://blog.csdn.net/Purpleendurer/article/details/133915687
?注意:在輸入所有內(nèi)容后要按Ctrl+D結(jié)束。
5.2 比較a.txt 和 b.txt 內(nèi)容是否相同
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校驗(yàn)值相同,所以兩個(gè)文件內(nèi)容相同。
5.3?比較a.txt 和 c.txt 內(nèi)容是否相同
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校驗(yàn)值相同,所以兩個(gè)文件內(nèi)容相同。
5.4?比較a.txt 和 不存在文件 x.txt 內(nèi)容是否相同
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個(gè)文件x.txt并不存在,所以sf.sh會(huì)顯示出錯(cuò)信息后退出。
5.5 傳遞參數(shù)數(shù)量不正確,會(huì)給出出錯(cuò)信息和命令正確用法
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 ~ $?
?
沒(méi)有傳遞參數(shù),只傳遞了1個(gè)參數(shù),或傳遞了3個(gè)參數(shù),都會(huì)給出出錯(cuò)信息和命令正確用法。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-846583.html
6 后記
sf.sh算是我寫(xiě)的第1個(gè)具有實(shí)用價(jià)值的腳本,這是開(kāi)了一個(gè)好頭,希望后續(xù)能夠?qū)懗龈喔玫膶?shí)用腳本。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-846583.html
到了這里,關(guān)于Linux shell編程學(xué)習(xí)筆記44:編寫(xiě)一個(gè)腳本,將md5sum命令執(zhí)行結(jié)果保存到變量中,進(jìn)而比較兩個(gè)文件內(nèi)容是否相同的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!