如何进行字符串(混合数字和字母)比较

Rob*_*ert 1 math bash

我正在尝试比较交换机型号(例如:2620-48-PWR,2848,5308xl,5412zl),因此我可以针对特定型号运行特定测试.当我运行脚本时,我收到错误:

tstVOIPConfigTest.bsh:第207行:5412zl:算术语法错误

但是这个页面:(http://www.softpanorama.org/Scripting/Shellorama/arithmetic_expressions.shtml)表示"Bash只是忽略任何不包含数值的字符串,并为包含数字和其他数字的任何内容生成错误人物:"

示例代码:

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
Run Code Online (Sandbox Code Playgroud)

由于我的脚本是用bash编写的,我的问题是如何在bash本身(据我所知)不能满足我需要的时候完成我需要做的事情?

先感谢您,

gle*_*man 5

您需要使用=运算符进行字符串比较.
-eq严格用于数字比较

参考

请注意,在运算符中[[,=运算符不是字符串相等:它是模式匹配运算符,因此请谨慎引用右侧值.