2015-05-01 121 views
2

我想寫一個bash腳本來將用戶輸入的答案轉換爲MD5並將其與MD5哈希值進行比較我已經必須查看它是否相同,但是我「M得到一個語法錯誤語法錯誤附近的意外令牌'fi',bash腳本

Syntax error near unexpected token 'fi' 

的代碼:

#!/bin/bash 
# Check if you're right! 
declare -x answer=6f22bf74918703932091343ba9c64402 
echo "Enter your answer, follow by [ENTER]:" 
read userAnswer 
md5sum userAnswer > hashedAnswer 
if [ answer == hashedAnswer ] 
then 
    echo "Correct! You're right! It was him! Maybe I shou----" 
    echo "-You get run over, silenced before you can present your proof-" 
else 
    echo "Wrong, try again!" 
fi 

任何幫助,將不勝感激。

+1

即語法上有效。那是你的*精確*腳本嗎?你已經錯過了使用變量的'$',而'hashedAnswer'是一個文件名而不是散列值本身。 –

+0

是的,這是我的確切腳本,它在第15行拋出錯誤,並且還標記「':不是有效的標識符」「 – Defa1t

+5

啊。該文件中有DOS行結束符。運行'dos2unix'就可以了。 –

回答

2

更改這兩行:

md5sum userAnswer > hashedAnswer 
if [ answer == hashedAnswer ] 

到:

hashedAnswer=$(printf "%s" "$userAnswer" |md5sum) 
if [ "$answer" == "$hashedAnswer" ] 

例子:

#!/bin/bash 
# Check if you're right! 
answer="b326b5062b2f0e69046810717534cb09 -" 
echo "Enter your answer, follow by [ENTER]:" 
read userAnswer 
hashedAnswer=$(printf "%s" "$userAnswer" |md5sum) 
if [ "$answer" == "$hashedAnswer" ] 
then 
    echo "Correct! You're right! It was him! Maybe I shou----" 
    echo "-You get run over, silenced before you can present your proof-" 
else 
    echo "Wrong, try again!" 
fi 

N.B:這裏的答案是true

相關問題