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

时间:2015-04-08 18:37:10

标签: bash math

我正在尝试比较交换机型号(例如: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

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

提前谢谢你,

1 个答案:

答案 0 :(得分:5)

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

Reference

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

相关问题