0

I have a problem with if in bash script. I've written the following if condition but it produces an error:

if [[ "$capacity" != *10 && "$capacity" != *20  ]] || [[ "$capacity" != *80 && "$capacity" != *100 ]]; then

Simply, I want to compare (two values not equals to) || (two values not equals to) condition using or operator

13
  • 1
    What are those * doing there? Commented Jan 30, 2018 at 23:05
  • 2
    Do we have to guess what the error is, or will you tell us? Commented Jan 30, 2018 at 23:09
  • it checks if $capacity ends with .10 , it works if I write only the first section like this -> if [[ "$capacity" != *10 && "$capacity" != *20 ]]; then Commented Jan 30, 2018 at 23:09
  • 2
    /bin/sh is not bash. They're different shells implementing different languages. (Even when /bin/sh is a link to the bash executable, it runs in POSIX compatibility mode when run under that name). Commented Jan 30, 2018 at 23:18
  • 1
    You know that condition is a tautology (always true), right? Commented Jan 30, 2018 at 23:47

1 Answer 1

1

I took your line and added enough additional lines to make it a Minimal, Complete, and Verifiable example:

#!/bin/bash
capacity=55

if [[ "$capacity" != *10 && "$capacity" != *20  ]] || [[ "$capacity" != *80 && "$capacity" != *100 ]]; then
  echo "match"
fi

Then I ran it like this:

$ chmod +x ./myscript
$ ./myscript
match

It wrote "match", exactly as I expected.

Will you please do the same thing?

  1. Add enough lines so that I can run your code on my machine
  2. Show how you run it and what it outputs
  3. Explain why this is different from what you expected
Sign up to request clarification or add additional context in comments.

4 Comments

I saved it as script.sh and then I run it as ./script.sh
@jore The example you posted has no fi so this is indeed an error. You need a body for your if statement and a fi to terminate it
I pasted here only the part of my code without fi, i have it
@jore StackOverflow expects you to write a complete question that contains all the details needed to answer it. You have asked an incomplete question that is missing important information. Please improve your question. Click "edit" and add the missing information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.