-4

cat test.sh

read value
if [ $value != "y" ] || [ $value != "n" ]
then
echo "invalid"
exit 1
fi 

I tried passing value at runtime, like "n", "m" and "y". And I am expecting script to exit when I pass "m". But its not working as expected.

NOTE: The above code works with Bash shell.

I tried several syntax but no luck.

if [ $value != "y" || $value != "n" ]

if [[ $value != "y" ] || [ $value != "n" ]]

if [[ $value != "y" ]] || [[ $value != "n" ]]

if [[ $value != "y" || $value != "n" ]]

sometimes condition does not satisfy

Please help in fixing this issue

  • With what shell are you running this code? – Chris Davies Oct 13 '20 at 09:24
  • What is the value of $value when you test your code? Do you get the same error message every time? What shell are you using? Is the script file a Unix text file or a Windows text file (with CRLF line endings)? – Kusalananda Oct 13 '20 at 09:32
  • its executing in bash shell, when the value is been passed other than y or n then it should exit the script – Yuva Raj Oct 13 '20 at 09:51
  • 1
    Your logic is wrong and you need to quote the variable expansion, but it's unclear what is your actual issue with this question. Is your issue that you're always getting an error message (which would indicate that something is wrong with the formatting of the text), or is the issue that you sometimes get an error and sometimes not? It's further confusing that you say that it works in bash. This leaves us wondering in what shell it does not work. In short, you will need to closer specify what you're doing and under what circumstances the script fails, and how it fails. – Kusalananda Oct 13 '20 at 10:01
  • the condition is not working as expected. Basically I want to get the value at runtime and check whether its "y" (or) "n", if anything other than that should exit . How to configure this – Yuva Raj Oct 13 '20 at 10:34
  • 2
    Where does the fail come from in your output? It's not there in the code shown. What does it mean that you had "no luck" with the lower set of conditionals? What did you expect to happen, and what exactly happened? Please show how the full script you have, and how you run it. You're asking for help from people who have never seen your code before, and who don't know what you are actually trying to do, so don't require them to guess -- it doesn't help in getting your problem solved. – ilkkachu Oct 13 '20 at 11:21
  • @ilkkachu I have re-edit my question, can you please check now – Yuva Raj Oct 13 '20 at 12:49
  • Unfortunately, it is still not clear what you mean by "sometimes condition does not satisfy". When does it, and when does it not (although you expect it to do)? – AdminBee Oct 14 '20 at 09:49

1 Answers1

3

And I am expecting script to exit when I pass "m". But its not working as expected.

Ok, so, you have the conditional:

if [ $value != "y" ] || [ $value != "n" ]; then
    echo "invalid"
    ...

If $value is m, it's not equal to y, and it's not equal to n, so that evaluates to (true or true), which is true, and the commands inside the if are executed.

If $value if y, it is equal to y, but is not equal to n, so that evaluates to (false or true), which is true, so the commands inside the if are executed again.

The only case where the commands inside the if would not be executed would be the case where both tests would be false -- $value would need to be equal to y and equal to n at the same time.

Basically, what you want here is this

if [ $value = "y" ] || [ $value = "n" ]; then
    echo ok
else
    echo invalid
fi

But without the useless "ok" branch, so the condition inverted:

if ! ( [ $value = "y" ] || [ $value = "n" ] ); then
    echo invalid
fi

Now, the inverse of (A or B) is not (not A or not B), but (not A and not B), so that's what your condition needs to say.

Also, you want to quote the variable expansion, because otherwise whitespace in the contents of $value will mess up how the command is interpreted, so:

if [ "$value" != "y" ] && [ "$value" != "n" ]; then
    echo invalid
fi

(or use [[ $value != "y" ]] etc., since [[ is special and one of the few cases where whitespace doesn't cause splitting.)

See also:

ilkkachu
  • 138,973