【发布时间】:2017-05-11 20:10:30
【问题描述】:
我想用 md5 在一个新的单独文本文件中散列文本文件的内容。这适用于每一行。我为此编写了以下 bash 脚本
#!/bin/bash
while read line; do echo -n $line|md5sum; done < $1 > $1.hash
这很好用。但是随着
echo -n 'somewords' | md5sum
我的输出是这样的8bf1072ac725ca3bc7f532079dd973ba -
我希望我的输出没有 - 最后。所以我的脚本应该给我这样的东西:
hash 1
hash 2
hash 3
而不是
hash 1 -
hash 2 -
hash 3 -
【问题讨论】:
-
只需通过
cut:while IFS= read -r line; do printf '%s' "$line" | md5sum; done < "$1" | cut -d' ' -f1 > "$1.hash"。或awk。为提高效率,将cut/awk/whatever 放在done关键字之后,就像我向您展示的那样。并使用更多的引号。并使用printf而不是echo -n。 -
很好,工作正常。还要感谢 printf 的提示。
标签: linux bash hash command-line md5