我在Bash中有一个字符串:
string="My string"
如何测试它是否包含另一个字符串?
if [ $string ?? 'foo' ]; then
echo "It's there!"
fi
哪里是我不认识的接线员。我使用echo和grep吗?
if echo "$string" | grep 'foo'; then
echo "It's there!"
fi
这看起来有点笨拙。
我在Bash中有一个字符串:
string="My string"
如何测试它是否包含另一个字符串?
if [ $string ?? 'foo' ]; then
echo "It's there!"
fi
哪里是我不认识的接线员。我使用echo和grep吗?
if echo "$string" | grep 'foo'; then
echo "It's there!"
fi
这看起来有点笨拙。
当前回答
使用jq:
string='My long string'
echo $string | jq -Rr 'select(contains("long"))|"It is there"'
jq中最困难的事情是打印单个引用:
echo $string | jq --arg quote "'" -Rr 'select(contains("long"))|"It\($quote)s there"'
仅使用jq检查条件:
if jq -Re 'select(contains("long"))|halt' <<< $string; then
echo "It's there!"
fi
其他回答
通用的大海捞针示例下面是变量
#!/bin/bash
needle="a_needle"
haystack="a_needle another_needle a_third_needle"
if [[ $haystack == *"$needle"* ]]; then
echo "needle found"
else
echo "needle NOT found"
fi
如果使用双括号,也可以在case语句外使用Marcus的答案(*通配符):
string='My long string'
if [[ $string == *"My long"* ]]; then
echo "It's there!"
fi
注意,针字符串中的空格需要放在双引号之间,*通配符应该放在外面。还要注意,使用了简单的比较运算符(即==),而不是正则表达式运算符=~。
正如Paul在绩效比较中提到的:
if echo "abcdefg" | grep -q "bcdef"; then
echo "String contains is true."
else
echo "String contains is not true."
fi
这是符合POSIX的,就像Marcus提供的答案中的“case“$string”一样,但它比case语句答案更容易阅读。还要注意,这将比使用case语句慢得多。正如保罗所指出的,不要在循环中使用它。
Bash 4+示例。注意:当单词包含空格等时,不使用引号会导致问题。请始终在Bash、IMO中引用。
以下是一些Bash 4+示例:
示例1,检查字符串中的“yes”(不区分大小写):
if [[ "${str,,}" == *"yes"* ]] ;then
示例2,检查字符串中的“yes”(不区分大小写):
if [[ "$(echo "$str" | tr '[:upper:]' '[:lower:]')" == *"yes"* ]] ;then
示例3,检查字符串中的“yes”(区分大小写):
if [[ "${str}" == *"yes"* ]] ;then
示例4,检查字符串中的“yes”(区分大小写):
if [[ "${str}" =~ "yes" ]] ;then
示例5,完全匹配(区分大小写):
if [[ "${str}" == "yes" ]] ;then
示例6,完全匹配(不区分大小写):
if [[ "${str,,}" == "yes" ]] ;then
示例7,完全匹配:
if [ "$a" = "$b" ] ;then
示例8,通配符match.ext(不区分大小写):
if echo "$a" | egrep -iq "\.(mp[3-4]|txt|css|jpg|png)" ; then
示例9,对区分大小写的字符串使用grep:
if echo "SomeString" | grep -q "String"; then
示例10,对不区分大小写的字符串使用grep:
if echo "SomeString" | grep -iq "string"; then
示例11,对字符串使用grep,不区分大小写,带通配符:
if echo "SomeString" | grep -iq "Some.*ing"; then
示例12,使用doublehash进行比较(如果变量为空会导致假阳性等)(区分大小写):
if [[ ! ${str##*$substr*} ]] ;then #found
享受
因此,这个问题有很多有用的解决方案——但哪一个最快/使用的资源最少?
使用此框架重复测试:
/usr/bin/time bash -c 'a=two;b=onetwothree; x=100000; while [ $x -gt 0 ]; do TEST ; x=$(($x-1)); done'
每次更换测试:
[[ $b =~ $a ]] 2.92 user 0.06 system 0:02.99 elapsed 99% CPU
[ "${b/$a//}" = "$b" ] 3.16 user 0.07 system 0:03.25 elapsed 99% CPU
[[ $b == *$a* ]] 1.85 user 0.04 system 0:01.90 elapsed 99% CPU
case $b in *$a):;;esac 1.80 user 0.02 system 0:01.83 elapsed 99% CPU
doContain $a $b 4.27 user 0.11 system 0:04.41 elapsed 99%CPU
(doContain在F.Houri的回答中)
对于傻笑:
echo $b|grep -q $a 12.68 user 30.86 system 3:42.40 elapsed 19% CPU !ouch!
因此,无论是在扩展测试还是案例中,简单的替代选项都可以预测地获胜。这个箱子是便携式的。
输出到100000 greps是可想而知的痛苦!关于无需使用外部实用程序的旧规则是正确的。