- 浏览: 4735036 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
bzhao:
你也应该可以这样:(not tested)./rbtunnel ...
在Bash脚本中怎么关闭文件描述符? -
bzhao:
如果有A进程原代码情况下,通过如下调用,把他的子进程继承关闭则 ...
在Bash脚本中怎么关闭文件描述符? -
Master-Gao:
楼主咋没分析下源码呢?
我使用过的Linux命令之dirname - 截取给定路径的目录部分 -
jiedushi:
tail -F 就可以吧
Linux下实时跟踪log4j日志文件的bash脚本 - 增强了tail -f的功能 -
java_is_new:
新手学习了,就是不明白为一个网卡配多个ip有什么用
我使用过的Linux命令之ifconfig - 网络配置命令
Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)
In Java
String.contains & String.indexOf
String.contains方法只能判断是否包含某个子串,不能判断是否包含单个字符(当然能判断是否包含单个字符的子串)
boolean contains(CharSequence s)
当且仅当此字符串包含 char 值的指定序列时,才返回 true。
使用String.indexOf方法判断是否包含某个字符
int indexOf(int ch)
Returns the index within this string of the first occurrence of the specified character.
if (s.contains(c)) {
}
使用String.indexOf方法判断是否包含某个子串
int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
if (str.indexOf(sub) >= 0) {
// do something
}
使用String.matches方法判断是否包含子串,注意正则表达式元字符的转义
boolean matches(String regex)
Tells whether or not this string matches the given regular expression.
if (str.matches(".*"+sub+".*")) {
// do something
}
StringUtils.contains
判断是否包含某个字符
Checks if String contains a search character, handling null. This method uses String.indexOf(int).
A null or empty ("") String will return false.
StringUtils.contains(null, *) = false
StringUtils.contains("", *) = false
StringUtils.contains("abc", 'a') = true
StringUtils.contains("abc", 'z') = false
Parameters:
str - the String to check, may be null
searchChar - the character to find
Returns:
true if the String contains the search character, false if not or null string input
判断是否包含另外的子串
Checks if String contains a search String, handling null. This method uses String.indexOf(String).
A null String will return false.
StringUtils.contains(null, *) = false
StringUtils.contains(*, null) = false
StringUtils.contains("", "") = true
StringUtils.contains("abc", "") = true
StringUtils.contains("abc", "a") = true
StringUtils.contains("abc", "z") = false
Parameters:
str - the String to check, may be null
searchStr - the String to find, may be null
Returns:
true if the String contains the search String, false if not or null string input
In Bash
是否包含子串(推荐方式)
[[ $STR == *$SUB* ]]
[[ $STR == *$SUB* ]]
注意:*不能引起来,否则不灵。
[root@jfht ~]# STR=123456789
[root@jfht ~]# SUB=456
[root@jfht ~]# [[ "$STR" == *"$SUB"* ]] && echo contains;
contains
[root@jfht ~]# SUB=4568
[root@jfht ~]# [[ "$STR" == *"$SUB"* ]] && echo contains;
[root@jfht ~]# SUB="1 2"
[root@jfht ~]# [[ "$STR" == *"$SUB"* ]] && echo contains;
[root@jfht ~]# [[ "$STR" == *$SUB* ]] && echo contains;
[root@jfht ~]# STR="1 2 3"
[root@jfht ~]# [[ "$STR" == *$SUB* ]] && echo contains;
contains
[root@jfht ~]#
特殊情况:以某子串开头。
[[ $STR == $SUB* ]]
特殊情况:以某子串结尾。
[[ $STR == *$SUB ]]
[root@jfht ~]# STR=123456789
[root@jfht ~]# SUB=123
[root@jfht ~]# [[ "$STR" == $SUB* ]] && echo "starts";
starts
[root@jfht ~]# [[ "$STR" == *$SUB ]] && echo "ends";
[root@jfht ~]# SUB=789
[root@jfht ~]# [[ "$STR" == $SUB* ]] && echo "starts";
[root@jfht ~]# [[ "$STR" == *$SUB ]] && echo "ends";
ends
使用正则表达式匹配方式确定是否包含子串
[[ $STR =~ .*$SUB.* ]]
注:.*是不必要的,可写成
[[ $STR =~ $SUB ]]
[root@jfht ctmw]# STR=123456789
[root@jfht ctmw]# SUB=456
[root@jfht ctmw]# [[ "$STR" =~ .*$SUB.* ]] && echo contains
contains
[root@jfht ctmw]# [[ "$STR" =~ $SUB ]] && echo contains
contains
使用case语句来确定是否包含子串
case "$STR" in *$SUB*) echo contains; ;; esac
[root@jfht ctmw]# STR=123456789
[root@jfht ctmw]# SUB=456
[root@jfht ctmw]# case "$STR" in *$SUB*) echo contains; ;; esac
contains
[root@jfht ctmw]#
使用字符串替换来实现是否包含子串
if [ "$STR" != "${STR/$SUB/}" ]; then echo contains; fi
解读:如果将字符串STR中的SUB子串删除掉之后,不与STR相等,就表明STR中包含SUB串。
[root@jfht ctmw]# STR=123456789
[root@jfht ctmw]# SUB=456
[root@jfht ctmw]# if [ "$STR" != "${STR/$SUB/}" ]; then echo contains; fi
contains
[root@jfht ctmw]#
使用grep来实现是否包含子串
if echo "$STR" | grep -q "$SUB"; then echo contains; fi
if grep -q "$SUB" <<<"$STR"; then echo contains; fi
[root@jfht ctmw]# STR=123456789
[root@jfht ctmw]# SUB=456
[root@jfht ctmw]# if echo "$STR" | grep -q "$SUB"; then echo contains; fi
contains
[root@jfht ctmw]# if grep -q "$SUB" <<<"$STR"; then echo contains; fi
contains
[root@jfht ctmw]#
使用expr match来实现是否包含子串
if [ "$(expr match "$STR" ".*$SUB.*")" != "0" ]; then echo contains; fi
[root@jfht ctmw]# STR=123456789
[root@jfht ctmw]# SUB=456
[root@jfht ctmw]# if [ "$(expr match "$STR" ".*$SUB.*")" != "0" ]; then echo contains; fi
contains
[root@jfht ctmw]#
本文链接:http://codingstandards.iteye.com/blog/1181490 (转载请注明出处)
返回目录:Java程序员的Bash实用指南系列之字符串处理(目录)
上节内容:Bash字符串处理(与Java对照) - 13.字符串数组连接(以指定分隔符合并)
下节内容:Bash字符串处理(与Java对照) - 15.计算子串出现的次数
发表评论
-
Bash字符串处理(与Java对照) - 22.判断字符串是否数字串
2011-10-25 09:25 5437Bash字符串处理(与Java对照) - 22.判断字符串是否 ... -
Bash字符串处理(与Java对照) - 21.字符串正则匹配
2011-10-24 09:07 11013Bash字符串处理(与Java对照) - 21.字符串正则匹配 ... -
Bash字符串处理(与Java对照) - 20.查找子串的位置
2011-10-19 09:14 6738Bash字符串处理(与Java对照) - 20.查找子串的位置 ... -
Bash字符串处理(与Java对照) - 19.查找字符的位置
2011-10-18 09:06 5908Bash字符串处理(与Java对照) - 19.查找字符的位置 ... -
Bash字符串处理(与Java对照) - 18.格式化字符串
2011-10-17 09:18 4932Bash字符串处理(与Java对照) - 18.格式化字符串 ... -
Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾
2011-10-09 08:58 6940Bash字符串处理(与Java对照) - 17.判断是否以另外 ... -
Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头
2011-10-08 09:17 8309Bash字符串处理(与Java对照) - 16.判断是否以另外 ... -
Bash字符串处理(与Java对照) - 15.计算子串出现的次数
2011-09-28 09:37 3370Bash字符串处理(与Java对照) - 15.计算子串出现的 ... -
Bash字符串处理(与Java对照) - 13.字符串数组连接(以指定分隔符合并)
2011-09-26 09:19 5123Bash字符串处理(与Java对照) - 13.字符串数组连接 ... -
Bash字符串处理(与Java对照) - 12.字符串连接
2011-09-23 09:08 6315Bash字符串处理(与Java对照) - 12.字符串连接 ... -
Bash字符串处理(与Java对照) - 11.比较两个字符串大小(字典顺序、数值比较)
2011-09-21 09:31 5656Bash字符串处理(与Java对照) - 11.比较两个字符串 ... -
Bash字符串处理(与Java对照) - 10.判断两个字符串是否相等(不等)
2011-09-20 09:16 6852Bash字符串处理(与Java对照) - 10.判断两个字符串 ... -
Bash字符串处理(与Java对照) - 9.获取字符串指定位置的字符、遍历字符串中的字符
2011-09-19 09:13 3698Bash字符串处理(与Java对照) - 9.获取字符串指定位 ... -
Bash字符串处理(与Java对照) - 8.计算字符串长度
2011-09-16 09:20 5605Bash字符串处理(与Java对照) - 8.计算字符串长度 ... -
Bash字符串处理(与Java对照) - 7.字符串与默认值
2011-09-15 09:20 3901Bash字符串处理(与Java对照) - 7.字符串与默认值 ... -
Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空)
2011-09-14 09:20 7168Bash字符串处理(与Java对照) - 6.判断字符串是否为 ... -
Bash字符串处理(与Java对照) - 5.字符串输入(读取字符串)
2011-09-13 09:28 3890Bash字符串处理(与Java对照) - 5.字符串输入(读取 ... -
Bash字符串处理(与Java对照) - 4.字符串输出
2011-09-08 09:30 3706Bash字符串处理(与Java对照) - 4.字符串输出 I ... -
Bash字符串处理(与Java对照) - 3.给(字符串)变量赋值
2011-09-07 09:29 6841Bash字符串处理(与Java ... -
Bash字符串处理(与Java对照) - 2.字符串的表示方式(字符串常量)
2011-09-06 09:18 6089Bash字符串处理(与Java ...
相关推荐
在centos6或rhel6 操作系统安装bash-completion-2.1-6.el7.noarch.rpm后,重启即生效
bash-3.1-MSYS-1.0.11-snapshot.tar.bz2 ffmpeg 安装用包!
离线安装包,测试可用
bash-4.1.2-9.el6_2.i686.rpm是centos工具类。
libguestfs-bash-completion-1.40.2-28.module_el8.5.0+821+97472045.noarch(1).rpm官方离线安装包,亲测可用
linux 服务器GNU Bash小于版本4.3有操作系统命令注入漏洞,需要对bash升级,下载解压 #tar zxvf bash-4.4.tar.gz #cd bash-4.4 #./configure (如果centos7编译失败,请先安装#yum install gcc) #make #make ...
git-bash.exe
openjdk-18.0.1.1(openjdk-18.0.1.1_linux-x64_bin.tar.gz)适用于Linux x64 系统:
-bash: /usr/local/jdk/jdk1.8.0_181/bin/java: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory 安装完后 java -version 查看版本出现: 原因是:没有那个文件或目录,找了很久发现需要...
2. 检查包是否安装上 rpm -qa rlwrap 3. 修改配置文件 vi .bash_profile 增加:alias sqlplus='rlwrap sqlplus' alias rman='rlwrap rman' alias lsnrctl='rlwrap lsnrctl' 4. 生效: source .bash_profile
redhat bash漏洞补丁包含文件:bash-4.1.2-15.el6_5.2.i686.rpmbash-4.1.2-15.el6_5.2.x86_64.rpmbash-3.0-27.el4.i386.rpmbash-3.0-27.el4.x86_64.rpmbash-3.2-33.el5_11.4.i386.r... redhat bash漏洞补丁 包含文件...
修复redhat6 bash远程执行任意代码漏洞CVE-2014-6271
修改redhat 7 bash远程执行任意代码漏洞
命令补全 Centos6/7或Rhel6/7 安装 bash-completion-2.1-6.el7.noarch.rpm 后重启即生效
网上有很多bash补丁下载,但都是x86_64架构的,针对i686的下载很少,bash-4.1.2-15.el6_5.2.src.rpm 也很难下载,希望上传的这个bash-4.1.2-15.el6_5.2.i686.rpm对你有用。
修复bash远程执行漏洞,支持redhat linux 64位操作系统 CVE-2014-6271
aix 操作系统bash工具bash-3.2-2.aix6.1.ppc.rpm,aix 5及6、7均支持
检测方法 执行如下命令查看设备是否存在bash安全漏洞。 #env -i X='() { (a)=>\\' bash -c '/dev/stdout echo vulnerable' 2>/dev/null 判定依据 如果以上命令的输出为以下内容则不合规: vulnerable
AIX-6系统的bash-5安装包