변수가 에코된 문자열을 완전히 엉망으로 만듭니다.

변수가 에코된 문자열을 완전히 엉망으로 만듭니다.

비밀번호의 sha1 해시가 어딘가에 유출되었는지 확인할 수 있는 pwnedpasswords라는 웹사이트를 찾았습니다. 그래서 프로세스를 자동화하는 스크립트를 만들었습니다. 스크립트는 다음과 같습니다.

#!/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"

테스트 비밀번호로 사용했는데 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

내가 그렇게 하면 echo "Your password has been pwned ${your_pw} times" 올바른 형식이 제공되지만($your_pw는 단지 비밀번호 그 자체입니다), 그렇게 하면 echo "Your password has been pwned ${pwned_count} times"끝에서 시작 times하고 어떻게든 처음에서 겹치는 이상한 형식을 제공합니다... .. .무슨 일인지 모르겠어요...

누구든지 그것을 알아낼 수 있습니까?

답변1

사이트에서 반환된 목록의 행은 CR/LF. A CR( \r)는 캐럿/커서를 줄의 시작 부분으로 이동합니다.

printf 'good \r times'
 times

관련 정보