Home > database >  how to write if $var == *
how to write if $var == *

Time:02-15

I have a script that I'm writing and I've set some flags using If statements. for example my flags are set as

flag=$(-a,-b,-c,-d)

and for example my If statement is set as

if echo "$flag" | grep -q -E -o "(-)(a)"; then
        1
fi

my question is how do I add another if statement that will say if flag does not exist, then show an error. I've tried something like the following but it does not work.

if "[[ $flag"=="*" ]]; then
        Error.
fi

Any suggestions? Thanks!

CodePudding user response:

"If flag does not exist" -- like, flag not being defined?

You could treat all undefined variables as errors.

Or you could check if the variable is set.

CodePudding user response:

bash suggests a solution with its own $- variable.

Store each flag as a single character in a string:

flags="abcd"

then use pattern-matching to determine if a particular flag is set or not:

if [[ $flags = *a* ]]; then
    echo "a is set"
else
    echo "a is not set"
fi
  • Related