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
Try:
if [[ "$choice" != 'y' && "$choice1" != 'y' ]]; then
echo "Test Done!"
else
echo "Test Failed!"
fi
Another thought,
$ c1='y' ; c2='y' ; [[ ${c1} = 'y' ]] && [[ ${c2} = 'y' ]] && echo true || echo false
true
$ c1='n' ; c2='y' ; [[ ${c1} = 'y' ]] && [[ ${c2} = 'y' ]] && echo true || echo false
false
$ c1='n' ; c2='y' ; [[ ${c1} = 'y' ]] || [[ ${c2} = 'y' ]] && echo true || echo false
true
$ c1='n' ; c2='n' ; [[ ${c1} = 'y' ]] || [[ ${c2} = 'y' ]] && echo true || echo false
false
$
Overflow of gibberish. (;
if [ "$choice" != 'y' -a "$choice1" != 'y' ]; then
echo "Test Done !"
else
echo "Test Failed !"
fi
You have your logic reversed; you're checking for != when you should be checking for ==. Try this:
if [ "$choice" == 'y' ] && [ "$choice1" == 'y' ]; then
echo "Test Done !"
else
echo "Test Failed !"
fi
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
The line
if [ "$choice" != 'y' ] && [ "$choice1" != 'y' ]; then
tests if both choices aren't 'y', so when both choices are 'y', the statement is false and your program correctly prints "Test Failed".