Robert
Robert

Reputation: 169

How to do string(mixed numbers and letters) comparisons

I'm trying to compare switch models(example: 2620-48-PWR, 2848, 5308xl, 5412zl) so I can run specific tests for specific models. When I run the script I am getting the error:

tstVOIPConfigTest.bsh: line 207: 5412zl: arithmetic syntax error

However this page: (http://www.softpanorama.org/Scripting/Shellorama/arithmetic_expressions.shtml) indicates that "Bash simply ignores any strings that don't contain numeric values and generates an error for anything that contains both numbers and other characters:"

Example Code:

if [[ $switchModel -eq "2810-48G" ]]; then
   echo "2810-48g Series Switch detected - verifying if qos-passthrough-mode is enabled" >> $errFile
   test "qos-passthrough-mode typical" "qos-passthrough-mode typical set" "qos-passthrough-mode typical not set"             
fi

Since my script is written in bash, my question is how can I accomplish what I need to do when bash inherently (as I understand it) does not do what I need it to?

Thank you in advance,

Upvotes: 1

Views: 242

Answers (1)

glenn jackman
glenn jackman

Reputation: 247042

You need to use the = operator for string comparison.
-eq is strictly for numeric comparison

Reference

Be aware that in [[, the = operator is not string equality: it is a pattern matching operator, so be cautious to quote the right-hand value appropriately.

Upvotes: 5

Related Questions