如何在shell脚本中使用if / else语句?

时间:2014-02-04 18:31:12

标签: shell unix if-statement terminal

我想知道如何使用if / else语句来检查我的unix shell中的密码是否正确。

2 个答案:

答案 0 :(得分:1)

以下是语法:

#!/bin/sh
# This is some secure program that uses security.

VALID_PASSWORD="secret" #this is our password.

echo "Please enter the password:"
read PASSWORD

if [ "$PASSWORD" == "$VALID_PASSWORD" ]; then
    echo "You have access!"
else
    echo "ACCESS DENIED!"
fi

bash-3.2$ Please enter the password:
secr
ACCESS DENIED!
bash-3.2$ 

bash-3.2$ Please enter the password:
secret
You have access!"
bash-3.2$ 

Click here for more on if/else syntax in scripts.

答案 1 :(得分:0)

 read -s -e -p "Enter your password: " PASSWORD
 if [ "$PASSWORD" == "password"}; then
      echo "You have access!"
 fi