【发布时间】:2022-01-09 01:22:43
【问题描述】:
我需要计算目录层次结构中文件的 md5 哈希值。我正在使用以下案例作为测试。我的 Android 设备有一个 md5 二进制文件,但需要文件的绝对路径 (md5 <filename>)。
我在 Android 设备上有以下目录层次结构:
/data/local/tmp/test1
/data/local/tmp/test1/test2
/data/local/tmp/test1/test2/test3
要获取绝对路径列表,我按照here 中提到的答案进行操作。
$ adb shell 'function rcrls() { ls -d $1/* | while read f; do echo "$f"; if [ -d "$f" ]; then rcrls "$f"; fi; done } ; rcrls /data/local/tmp/' > filelist.txt
现在我有了每个文件的绝对路径列表。
接下来我想在脚本中逐行读取这个文件,并为每一行调用md5。如果输入是目录,md5 将打印一条消息。我按照here的例子。
#! /bin/bash
filename='filelist.txt'
cat $filename | while read LINE; do
adb shell 'md5 $LINE'
done
我得到以下输出:
/data/local/tmp/test1/test2
/data/local/tmp/test1/test2/test3
could not read /data/local/tmp/test1, Is a directory
我希望它为 test1 和 test2 打印一个目录警告,然后为 test3 打印 md5,因为 test3 是一个文件。有人可以建议如何修复此代码吗?我期待的是这样的:
could not read /data/local/tmp/test1, Is a directory
could not read /data/local/tmp/test1/test2, Is a directory
<hash_value> /data/local/tmp/test1/test2/test3
【问题讨论】: