在Shell腳本中為zip壓縮包添加注釋(MD5)


在shell中使用zip工具打包文件,並希望將文件的MD5值添加到zip壓縮包的注釋中。

1. 終端中為zip壓縮包添加注釋

1.1. 查看需要被壓縮的文件

終端使用 fonts-powerline 字體,在此處無法正常顯示(詳情見 linux 安裝並配置zsh)。

 pi@ubuntu  ~/project/zip_md5  ls -al
total 16
drwxrwxr-x 2 pi pi 4096 Jun 10 23:24 .
drwxrwxr-x 5 pi pi 4096 Jun 10 23:23 ..
-rwxrwxr-x 1 pi pi 5949 Jun  2 20:54 hello-rpi

1.2. 計算MD5值,並保存到文件中

 pi@ubuntu  ~/project/zip_md5  md5sum hello-rpi   
8417cd4ae26ea57d1c9bf6145ccc4202  hello-rpi
 pi@ubuntu  ~/project/zip_md5  md5sum hello-rpi > hello-rpi.md5
 pi@ubuntu  ~/project/zip_md5  cat hello-rpi.md5            
8417cd4ae26ea57d1c9bf6145ccc4202  hello-rpi

 1.3. 使用zip工具制作壓縮包

在命令中加上 -z 選項(”-z   add zipfile comment “);壓縮過程中,會提示輸入注釋,就將生成的MD5值輸入(注釋結束的方式為:換行,單獨輸入“.”)。

 pi@ubuntu  ~/project/zip_md5  zip hello-rpi.zip hello-rpi hello-rpi.md5 -z
  adding: hello-rpi (deflated 59%)
  adding: hello-rpi.md5 (stored 0%)
enter new zip file comment (end with .):
8417cd4ae26ea57d1c9bf6145ccc4202  hello-rpi
.
 pi@ubuntu  ~/project/zip_md5  ls -al hello-rpi.zip 
-rw-rw-r-- 1 pi pi 2877 Jun 10 23:34 hello-rpi.zip

1.4. 查看生成的壓縮包及注釋

 pi@ubuntu  ~/project/zip_md5  zipnote hello-rpi.zip 
@ hello-rpi
@ (comment above this line)
@ hello-rpi.md5
@ (comment above this line)
@ (zip file comment below this line)
8417cd4ae26ea57d1c9bf6145ccc4202  hello-rpi

 

2. shell腳本中為zip壓縮包添加注釋

2.1. 創建腳本do_zipm

 pi@ubuntu  ~/project/zip_md5  vi do_zipm 

將如下代碼輸入,並:wq保存。

#!/bin/bash
file_name
=$1 md5=$(md5sum ${file_name}) echo "${md5}" > ${file_name}.md5 zip -q ${file_name}.zip ${file_name} ${file_name}.md5 -z << EOF ${md5} . EOF

2.2. 修改權限,執行腳本

 pi@ubuntu  ~/project/zip_md5  chmod a+x do_zipm 
 pi@ubuntu  ~/project/zip_md5  ls -al do_zipm      
-rwxrwxr-x 1 pi pi 163 Jun 11 00:24 do_zipm
 pi@ubuntu  ~/project/zip_md5  ./do_zipm hello-rpi
 pi@ubuntu  ~/project/zip_md5  ls -al        
total 28
drwxrwxr-x 2 pi pi 4096 Jun 11 00:38 .
drwxrwxr-x 5 pi pi 4096 Jun 10 23:23 ..
-rwxrwxr-x 1 pi pi  163 Jun 11 00:24 do_zipm
-rwxrwxr-x 1 pi pi 5949 Jun  2 20:54 hello-rpi
-rw-rw-r-- 1 pi pi   44 Jun 11 00:38 hello-rpi.md5
-rw-rw-r-- 1 pi pi 2877 Jun 11 00:38 hello-rpi.zip

2.3. 查看注釋

 pi@ubuntu  ~/project/zip_md5  zipnote hello-rpi.zip
@ hello-rpi
@ (comment above this line)
@ hello-rpi.md5
@ (comment above this line)
@ (zip file comment below this line)
8417cd4ae26ea57d1c9bf6145ccc4202  hello-rpi

3. 驗證MD5

3.1. Raspbian

pi@raspberrypi:~/project/zip_md5 $ unzip hello-rpi.zip
Archive:  hello-rpi.zip
8417cd4ae26ea57d1c9bf6145ccc4202  hello-rpi
  inflating: hello-rpi
 extracting: hello-rpi.md5
pi@raspberrypi:~/project/zip_md5 $ ls -al
總用量 24
drwxr-xr-x 2 pi pi 4096 6月  11 00:30 .
drwxr-xr-x 5 pi pi 4096 6月  11 00:27 ..
-rwxrwxr-x 1 pi pi 5949 6月   2 20:54 hello-rpi
-rw-rw-r-- 1 pi pi   44 6月  11 00:24 hello-rpi.md5
-rw-r--r-- 1 pi pi 2877 6月  11 00:30 hello-rpi.zip
pi@raspberrypi:~/project/zip_md5 $ md5sum -c hello-rpi.md5
hello-rpi: 成功

3.2. Windows

PS D:\Share\hello-rpi> certutil.exe -hashfile .\hello-rpi md5
MD5 的 .\hello-rpi 哈希:
8417cd4ae26ea57d1c9bf6145ccc4202
CertUtil: -hashfile 命令成功完成。
PS D:\Share\hello-rpi> & type .\hello-rpi.md5
8417cd4ae26ea57d1c9bf6145ccc4202  hello-rpi

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM