2017-04-25 1 views
2

그래서 암호 스크립트를 만들려고 노력 중이며 대부분 작동합니다. 그러나 루프는 각 "잘못된 암호"이후에 멈추는 대신 영원히 계속됩니다. 따라서 사용자는 암호를 입력 할 수 있습니다. 그러나 그것은 스팸 잘못된 비밀 번호를 유지하고 아무 것도 전혀 입력의 기회를 제공하지 않습니다. 나는 또한 줄을 아래에 read password 퍼팅 시도했지만이 작동하지 않았다.터미널 비밀번호

#!/bin/bash 
# Author: Dom (GlitchyShadowZ) 
# No name lol 
# Password Protected Terminal 

echo "Please enter password to continue" 
str="owo" 
read password 

until [ $password = $str ] 
do 
    # Password Verification 
    if [ $password = $str ]; 
    then 
     echo "Password correct. Logging in" 
    else 
     echo "Password Incorrect. Try again." 
    fi 
done 
+4

루프 내부에서 '암호 읽기'를해야합니다. 그렇지 않으면 변경되지 않습니다. – msitt

답변

3

비교하기 전에 항상 암호를 읽어야합니다. 또한 $password = $str 상태를 두 번 확인하고 있습니다. 필요하지 않습니다. 당신은 완전히 루프에서 if을 제거하고 같은 스크립트의 끝에서 Password correct. Logging in를 이동하여 한 번 확인할 수 있습니다

#!/bin/bash 

# Author: Dom (GlitchyShadowZ) 

# No name lol 

# Password Protected Terminal 

echo "Please enter password to continue" 
str="owo" 
read password 

until [ "$password" = "$str" ] 
do 
    echo "Password Incorrect. Try again." 
    read password 
done 

echo "Password correct. Logging in" 

또는 루프 내부 break과 함께 while true를 사용하여 올바른 암호를 입력 할 때 :

#!/bin/bash 

# Author: Dom (GlitchyShadowZ) 

# No name lol 

# Password Protected Terminal 


str="owo" 
password="" 

echo "Please enter password to continue" 

while true 
do 
    read password 
    # Password Verification 
    if [ "$password" = "$str" ]; 
    then 
    echo "Password correct. Logging in" 
    break 
    else 
    echo "Password Incorrect. Try again." 
    fi 
done 

두 경우 모두 현재 스크립트가 실행되고 아무 것도 입력되지 않고 을 입력하면 즉시을 입력하므로 다음과 같이 입력하면 ""을 입력해야합니다.

line 13: [: =: unary operator expected 
관련 문제