1

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

4 Answers4

3

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
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • 1
    @Sleekequation56: If you need to check multiple cases, change the `;;` (double semicolons) to `;;&` (double semicolon ampersand). That will cause the test to fall through to the next test instead of ending the `case` at the current test. (Bash 4.0 and later). Another terminator, `;&` (single semicolon ampersand), causes the next set of actions to execute without performing the test associated with them. – Dennis Williamson Nov 17 '22 at 16:57
0

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.

mju
  • 591
  • 1
  • 6
  • 17
0

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  
dawg
  • 98,345
  • 23
  • 131
  • 206
0

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 : do something.
elif [[ "$results" == *"specific character"* ]]
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 ((found++)); : do something.
fi
if [[ "$results" == *"specific character"* ]]
then ((found++)); : do something.
fi
((found)) || echo "characters are not found."
Paul Hodges
  • 13,382
  • 1
  • 17
  • 36