0

I'm reasonably proficient with regexes, but I'm having trouble with doing something simple in a Bash script.

I'm working with the following script excerpt:

    if [[ "$message" =~ "^The node was low on resource: ephemeral-storage.*$" ]]; then
        message="ephemeral storage"
    elif [[ "$message" =~ "^The node was low on resource: memory.*$" ]]; then
         message="memory"
    elif [[ "$message =~ ^The node was low on resource: \[DiskPressure\].*$" ]]; then
         message="disk"
    else
        echo "No match"
    fi

If the value in "message" begins with "The node was low on resource: ephemeral-storage. Container ...", this matches the LAST expression. I've been staring at this for quite a while, and I'm just not getting it.

I turned on verbose mode in the script, and I see the following in the output:

+ [[ The node was low on resource: ephemeral-storage. Container ...  =~ \^The node was low on resource: ephemeral-storage\.\*\$ ]]
+ [[ The node was low on resource: ephemeral-storage. Container ...  =~ \^The node was low on resource: memory\.\*\$ ]]
+ [[ -n The node was low on resource: ephemeral-storage. Container ...  =~ ^The node was low on resource: \[DiskPressure\].*$ ]]
+ message=disk

Note two things in this output. I'm eliding much of the value that's in the "message" variable with "...". Second, I see the "-n" on the output from the last elif. Clearly, that's not in my code, not explicitly. Checking the docs, I see the "-n" operator returns true if the string operand length is non-zero. So, if that's the code that's being executed, I can see why it's considering the last condition to be true, but I don't understand what it's doing here.

2 Answers 2

1

Regex must not be quoted in BASH. However you can just use glob matching instead of regex:

case $message in
   "The node was low on resource: ephemeral-storage"*)
   message="ephemeral storage";;
   "The node was low on resource: memory"*)
   message="memory";;
   "The node was low on resource: [DiskPressure]"*)
   message="disk";;
   *)
   echo "No match";;
esac
Sign up to request clarification or add additional context in comments.

1 Comment

If so it would be much easier to read to use a case/esac instead of this java-esque horror.
0

It would be much easier if regex patterns are defined in variables :

message="The node was low on resource: ephemeral-storage. Container"

p_ephemeral='^The node was low on resource: ephemeral-storage.*$'
p_memory='^The node was low on resource: memory.*$'
p_disk='^The node was low on resource: \[DiskPressure\].*$'

if  [[ "$message" =~ $p_ephemeral ]]; then
    message="ephemeral storage"
elif [[ "$message" =~ $p_memory ]]; then
    message="memory"
elif [[ "$message" =~ $p_disk ]]; then
    message="disk"
else
   echo "No match"
fi

Comments

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.