Home > Software design >  Check if multiple 'If Statements failed
Check if multiple 'If Statements failed

Time:11-18

I'm working on a script that checks (with If statements) if a variable contains a specific character. If a character is found the script executes some code. If a certain character cannot be found, the script will go into another If Statement to check if the variable contains another specific character, and so on.

this all works perfectly fine.

But I need the script to tell me if none of the characters are found, but i'm having some trouble accomplishing this goal. the script looks like this.

if [[ "$results" == *"specific character"* ]]; then
    do something.
fi

if [[ "$results" == *"specific character"* ]]; then
    do something.
fi

if [[ "$results" == *"specific character"* ]]; then
    do something.
fi

If all these If Statements cannot find their character I need the script to echo that these characters are not found.

I tried to put a If/else statement around all the other If statements, but this did not work for me.

if [[ ]]; then

   if [[ "$results" == *"specific character"* ]]; then
    do something.
   fi

   if [[ "$results" == *"specific character"* ]]; then
    do something.
   fi

   if [[ "$results" == *"specific character"* ]]; then
    do something.
   fi

else
   echo "characters are not found."

I can't seem to get it to work. Can somebody give me a push in the right direction?

Kind Regards,

Sleek

CodePudding user response:

Use a case statement:

case "$results" in
    *c*) do something;;
    *d*) do something;;
    *e*|*f*) do something
             do another thing;;
    *) echo "characters are not found.";;
esac

CodePudding user response:

You could just set a variable in the if bodies and check whether it's set or not lateron, like so:

...

if [[ "$results" == *"specific character"* ]]; then
    foundsmth=1
fi

if [[ -z ${foundsmth} ]] ; then
  # nothing found
fi

Another solution would be to use regular expressions in combination with a switch-case construction as explained here.

CodePudding user response:

For your if surrounding the individual if statements, you could use a regex:

if [[ $str =~ [abc] ]]; then
    if [[ "$str" == *"a"* ]]; then
        echo "a"
    fi  
    
    if [[ "$str" == *"b"* ]]; then
        echo "b"
    fi  
    
    if [[ "$str" == *"c"* ]]; then
        echo "c"
    fi  
else
    echo "None of those characters are in str"
fi  

CodePudding user response:

I usually prefer a case like Dennis. Since you already have it written as if's, you could use a compound if/elif/else, so long as it's ok to short-circuit out before checking them all.

if [[ "$results" == *"specific character"* ]]
then : do something.
elif [[ "$results" == *"specific character"* ]]; then
then : do something.
elif [[ "$results" == *"specific character"* ]]; then
then : do something.
else echo "characters are not found."
fi

On the other hand, if you need to check them ALL, whether or not any are found along the way, you're going to need mju's flag variable.

found=0;
if [[ "$results" == *"specific character"* ]]
then ((found  )); : do something.
fi
if [[ "$results" == *"specific character"* ]]; then
then ((found  )); : do something.
fi
if [[ "$results" == *"specific character"* ]]; then
then ((found  )); : do something.
fi
((found)) || echo "characters are not found."
  • Related