问题描述
我编写了一个小脚本,它将接受用户输入,然后为其生成 md5sum 值
count = 0
echo "Enter number of records"
read number
while [ $count -le $number ]
do
echo "Enter path"
read path
echo "file name"
read file_name
md5sum $path"/"$filename #it shows the md5sum value and path+filename
((count++))
done
如何将这些值(路径、文件名和 md5sums)传递给 CSV 文件。 (假设用户选择输入超过 1 条记录)
输出应该像
/c/training,sample.txt,34234435345346549862123454651324 #placeholder values
/c/file,text.sh,4534534534534534345345435342342
解决方法
您可以提示用户输入文件的完整路径,而不是提示用户输入目录路径和该目录中的文件名。然后,您可以使用 bash string manipulations 从该路径中提取您需要的内容。
#!/bin/bash
set -euo pipefail
function calc_md5() {
local path="${1}"
if [[ -f "${path}" ]] ; then
echo "${path%/*},${path##*/},$(md5sum ${path} | awk '{ print $1 }')"
else
echo "
x - Script requires path to file.
Usage: $0 /path/to/file.txt
"
exit 1
fi
}
calc_md5 "$@"
用法示例:
$ ./script.sh /tmp/test/foo.txt
/tmp/test,foo.txt,b05403212c66bdc8ccc597fedf6cd5fe