尝试在if中添加两个条件

时间:2019-04-08 02:28:12

标签: bash

我正在尝试执行此bash脚本,并试图在其中加入域。

domainjoin-cli加入$Domain后,提示输入密码,如果我的密码错误,我希望脚本以msg“ Not Success”退出,如果另一方面是正确的密码,我需要它继续与脚本的其余部分。

#!/bin/bash  
echo -n "Domain: "  
read Domain  
domainjoin-cli join $Domain  
if [[ "$password" = 'SUCCESS' ]]  
then  
    echo "SUCCESS"  
    ls -l  
fi  
echo  
if [[ "$password" = 'Error' ]]  
    then  
        echo "Not SUCCESSFULL"  
        exit  
fi 

如果我执行脚本,则会收到此错误消息:

3.sh: 5: 3.sh: [[: not found  
3.sh: 10: 3.sh: [[: not found 

如果密码错误,这是输出:

"Error: LW_ERROR_PASSWORD_MISMATCH [code 0x00009c56] The password is incorrect for the given account"

如果密码正确,这是输出:

"Warning: System restart required Your system has been configured to authenticate to Active Directory for the first time. It is recommended that you restart your system to ensure that all applications recognize the new settings.

SUCCESS"

1 个答案:

答案 0 :(得分:2)

  • Use More Quotes™。引用变量可确保在脚本运行时将其扩展为单个“单词”或参数。
  • -eq等用于比较数字,因此它将“ SUCCESS”视为数字,这不是您想要的。< / li>
  • 不要害怕Bashisms。 [[[更可取,因为它很难破解。

结果:

[[ "$password" = 'SUCCESS' ]]