shell脚本中字符串的不区分大小写比较 [英] Case insensitive comparison of strings in shell script

查看:759
本文介绍了shell脚本中字符串的不区分大小写比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

== 运算符用于在shell脚本中比较两个字符串。但是,我想比较两个字符串忽略的情况,怎么办呢?是否有任何标准命令?

解决方案

如果你有bash

  str1 =MATCH
str2 =match
shopt -s nocasematch
case$ str1in
$ str2) echomatch;;
*)echono match;;
esac

否则,你应该告诉我们你使用的shell。

使用awk

  str1 =MATCH
str2 = match
awk -vs1 =$ str1-vs2 =$ str2'BEGIN {
if(tolower(s1)== tolower(s2)){
print
}
}'


The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

解决方案

if you have bash

str1="MATCH"
str2="match"
shopt -s nocasematch
case "$str1" in
 $str2 ) echo "match";;
 *) echo "no match";;
esac

otherwise, you should tell us what shell you are using.

alternative, using awk

str1="MATCH"
str2="match"
awk -vs1="$str1" -vs2="$str2" 'BEGIN {
  if ( tolower(s1) == tolower(s2) ){
    print "match"
  }
}'

这篇关于shell脚本中字符串的不区分大小写比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆