我正在尝试在 bash 中创建两个函数。一个函数循环遍历目录的内容,并为每个文件生成 md5 哈希值,并将它们存储在同一目录中的 .md5 校验和文件中。另一个函数用于根据目录内容检查校验和文件。我正在努力保持.md5 文件标准我认为这是 md5 哈希值,后跟每个文件的一行文件名。
要求:我只能使用digest命令来创建哈希;目录上有哪些错误...
理想情况下,我可以假设要归档的入站文件是 n 个非目录项。尽管如此,我希望能够首先检查摘要是否有错误。即使由于某种未知原因失败,我也可以在退出整个脚本并出现错误之前再次尝试,或者至少记录无法为该特定文件进行摘要并继续的情况。
主要问题:如果条件错误,如何检查命令并将输出存储在变量中?
更新:解决方案在这里
if ! digest -a md5 "$file" ; then ; fi
我尝试了其他变体,例如通过管道传输到临时文件获取变量,然后删除临时文件,但这对于这项任务来说感觉太过分了。我还研究了使用 $? 访问退出代码但根据我过去读到的内容,我不确定这个社区对其使用的团结程度,请告诉我您的想法。
第二个问题:有没有更好的方法来实现这一目标?
以下是我目前不完整的功能。谢谢!
function make_directory_md5 () {
local md5_file=$"$1/$(basename $1).md5"
local attempts=$(($2 - 1))
if [ ! -f "$md5_file" ] ; then
if ! touch "$md5_file" >> "$file_log" ; then
warning_handle "Could not make checksum file $md5_file, continuing anyway..."
if [ "$verbose_mode" = true ]; then verbose_handle "Checksum file $md5_file process failed." ; fi
else
if [ "$verbose_mode" = true ]; then verbose_handle "Checksum file $md5_file created..." ; fi
#Loop through file contents and get md5 and filename and output to checksum file
for file in "$1/*" ; do
if [ ! -d "$file" ] ; then
if [ ! "$file" = "$md5_file" ] ; then #Skip md5 file.
if ! hash="$(digest -a md5 $file)" >> "$file_log" ; then
warning_handle "Problem occured when attempting to digest $file"
if [ "$verbose_mode" = true ]; then verbose_handle "Attempting to try again, attempts left: $attempts" ; fi
if [ $attempts -ge 1 ]; then
make_directory_md5 $1 $attempts
else
warning_handle "Attempt to create checksum file $md5_file FAILED out of attempts."
local sucess=false
if ! rm "$md5_file" >> "$file_log" ; then
if [ -f "$md5_file" ] ; then
warning_handle "Could not remove $md5_file and it still exists!"
fi
else
if [ "$verbose_mode" = true ]; then verbose_handle "Removed $md5_file" ; fi
fi
fi
else
echo "$hash $file" >> "$md5_file"
fi
fi
else
warning_handle "Cannot hash directory! Skipping $file"
#Recursive decent into new directory. Or assume no directories ever...
fi
done
fi
else
warning_handle "Attempted to create checksum file $md5_file but it already exists..."
if [ "$verbose_mode" = true ]; then verbose_handle "Attempting to remove $md5_file..." ; fi
if ! rm "$md5_file" >> "$file_log" ; then
if [ -f "$md5_file" ] ; then
warning_handle "Could not remove $md5_file and it still exists. Continuing with backup..."
if [ "$verbose_mode" = true ]; then verbose_handle "Checksum file $md5_file process failed." ; fi
else
if [ "$verbose_mode" = true ]; then verbose_handle "Old checksum file $md5_file removed..." ; fi
if [ "$verbose_mode" = true ]; then verbose_handle "Attempting to try again, attempts left: $attempts" ; fi
if [ $attempts -ge 1 ] ; then
make_directory_md5 $1 $attempts
else
local sucess=false
warning_handle "Attempt to create checksum file $md5_file FAILED out of attempts."
if [ "$verbose_mode" = true ]; then verbose_handle "Checksum file $md5_file process failed." ; fi
fi
fi
else
if [ "$verbose_mode" = true ]; then verbose_handle "Old checksum file $md5_file removed..." ; fi
if [ "$verbose_mode" = true ]; then verbose_handle "Attempting to try again, attempts left: $attempts" ; fi
if [ $attempts -ge 1 ] ; then
make_directory_md5 $1 $attempts
else
local sucess=false
warning_handle "Attempt to create checksum file $md5_file FAILED out of attempts."
if [ "$verbose_mode" = true ]; then verbose_handle "Checksum file $md5_file process failed." ; fi
fi
fi
fi
}
function check_directory_md5 () {
local md5_file=$"$1/$1.md5"
for file in "$1"/. ; do
echo "$file"
done
}
make_directory_md5 "/home/jsharpe/out" 3