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?
    – kvantour
    Commented Jan 30, 2018 at 23:05
  • 2
    Do we have to guess what the error is, or will you tell us?
    – cdarke
    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
    – jore
    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?
    – Kevin
    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
4
  • I saved it as script.sh and then I run it as ./script.sh
    – jore
    Commented Jan 30, 2018 at 23:16
  • @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 Commented Jan 30, 2018 at 23:16
  • I pasted here only the part of my code without fi, i have it
    – jore
    Commented Jan 30, 2018 at 23:17
  • 3
    @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. Commented Jan 30, 2018 at 23:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.