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

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

问题描述

== 运算符用于比较 shell 脚本中的两个字符串.但是,我想比较两个字符串忽略大小写,怎么办?有什么标准的命令吗?

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?

推荐答案

如果你有 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.

替代方案,使用 awk

alternative, using awk

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

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

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