Two conditions in a bash if statement

后端 未结 8 2229
失恋的感觉
失恋的感觉 2020-12-17 08:36

I\'m trying to write a script which will read two choices, and if both of them are \"y\" I want it to say \"Test Done!\" and if one or both of them isn\'t \"y\" I want it to

8条回答
  •  一整个雨季
    2020-12-17 09:07

    You are checking for the wrong condition.

    if [ "$choice" != 'y' ] && [ "$choice1" != 'y' ];
    

    The above statement is true when choice!='y' and choice1!='y', and so the program correctly prints "Test Done!".

    The corrected script is

    echo "- Do You want to make a choice ?"
    read choice
    
    echo "- Do You want to make a choice1 ?"
    read choice1
    
    if [ "$choice" == 'y' ] && [ "$choice1" == 'y' ]; then
        echo "Test Done !"
    else
        echo "Test Failed !"
    fi
    

提交回复
热议问题