变量完全弄乱了回显字符串

use*_*587 6 bash

我发现了这个名为 pwnedpasswords 的网站,您显然可以在其中检查您的密码的 sha1 哈希值是否已在某处泄露。所以我制作了一个脚本来自动化这个过程,这是我的脚本:

#!/bin/bash

read -s -p "Input your password: " your_pw
echo
your_hash=$(printf "$your_pw"|sha1sum|tr '[:lower:]' '[:upper:]'|head -c40)
hash_head=$(printf "$your_hash"|head -c5)
hash_tail=$(printf "$your_hash"|tail -c35)

pwned_count=$(curl https://api.pwnedpasswords.com/range/${hash_head} 2> /dev/null|grep "${hash_tail}"|awk -F ':' '{print $2}')
echo "Your password has been pwned ${your_pw} times"
echo "Your password has been pwned ${pwned_count} times"
Run Code Online (Sandbox Code Playgroud)

我用作测试密码1,这是输出:

[me@my_compuuter aaa8]$ ./was_your_password_pwned.sh
Input your password:
Your password has been pwned 1 times
 timesassword has been pwned 197972
Run Code Online (Sandbox Code Playgroud)

请注意当我echo "Your password has been pwned ${your_pw} times" 给我正确的格式时($your_pw 只是密码本身),但是当我echo "Your password has been pwned ${pwned_count} times"给我这种奇怪的格式时,它times从末尾开始并以某种方式在开头重叠......我不知道这是怎么回事...

有人能弄清楚吗?

Unc*_*lly 7

该站点返回的列表包含以 结尾的行CR/LF。A CR( \r) 将插入符号/光标移动到行的开头:

printf 'good \r times'
 times
Run Code Online (Sandbox Code Playgroud)

  • @user323587,`tr -d '\r'` 会更常见,它实际上删除了回车。将其更改为换行符当然也适用于您的情况,因为命令替换会删除所有尾随换行符。 (6认同)