问:
我在 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
这看起来有点笨拙。
答1:
保持自己快人一步,享受全网独家提供的一站式外包任务、远程工作、创意产品订阅服务–huntsbot.com
如果使用双括号,也可以在 case 语句之外使用 Marcus’s answer (* wildcards):
string='My long string'
if [[ $string == *"My long"* ]]; then
echo "It's there!"
fi
请注意,needle 字符串中的空格需要放在双引号之间,并且 * 通配符应该放在外面。另请注意,使用了一个简单的比较运算符(即 ==),而不是正则表达式运算符 =~。
另请注意,您可以通过在测试中切换到 != 来反转比较。感谢你的回答!
嗯,有了这个确切的代码,我得到了 [[: not found。知道有什么问题吗?我在 Ubuntu 上使用 GNU bash 版本 4.1.5(1)。
@Jonik:您可能缺少 shebang 或将其设置为 #!/bin/sh。请改用 #!/bin/bash。
答2:
与HuntsBot一起,探索全球自由职业机会–huntsbot.com
如果您更喜欢正则表达式方法:
string='My string';
if [[ $string =~ "My" ]]; then
echo "It's there!"
fi
必须在 bash 脚本中替换 egrep 正则表达式,这非常有效!
=~ 运算符已在整个字符串中搜索匹配项;这里的 .* 是多余的。此外,引号通常比反斜杠更可取:[[ $string =~ "My s" ]]
@bukzor Quotes 从 Bash 3.2+ 开始在这里停止工作:tiswww.case.edu/php/chet/bash/FAQ E14)。最好分配给一个变量(使用引号),然后进行比较。像这样:re="My s"; if [[ $string =~ $re ]]
测试它是否 NOT 包含字符串:if [[ ! "abc" =~ "d" ]] 为真。
答3:
一个优秀的自由职业者,应该有对需求敏感和精准需求捕获的能力,而huntsbot.com提供了这个机会
我不确定是否使用 if 语句,但您可以使用 case 语句获得类似的效果:
case "$string" in
*foo*)
# Do stuff
;;
esac
这可能是最好的解决方案,因为它可以移植到 posix shell。 (又名没有 bashisms)
@technosaurus 我觉得在一个只有 bash 标签的问题中批评“bashism”很奇怪:)
@PP与其说是批评,不如说是更普遍的解决方案优于更有限的解决方案。请考虑,几年后,人们(像我一样)会停下来寻找这个答案,并且可能很高兴找到一个比原始问题更有用的答案。正如他们在开源世界中所说:“选择是好的!”
@technosaurus,FWIW [[ $string == *foo* ]] 也适用于某些 POSIX 兼容的 sh 版本(例如 Solaris 10 上的 /usr/xpg4/bin/sh)和 ksh (>= 88)
答4:
huntsbot.com全球7大洲远程工作机会,探索不一样的工作方式
stringContain 变体(兼容或大小写无关)
由于这些 Stack Overflow 答案主要讲述的是 Bash,因此我在这篇文章的最底部发布了一个 case Independent Bash 函数…
反正有我的
兼容的答案
由于已经有很多使用 Bash 特定功能的答案,因此有一种方法可以在功能较差的 shell 下工作,例如 BusyBox:
[ -z "${string##*$reqsubstr*}" ]
在实践中,这可以给出:
string='echo "My string"'
for reqsubstr in 'o "M' 'alt' 'str';do
if [ -z "${string##*$reqsubstr*}" ] ;then
echo "String '$string' contain substring: '$reqsubstr'."
else
echo "String '$string' don't contain substring: '$reqsubstr'."
fi
done
这是在 Bash、Dash、KornShell (ksh) 和 ash (BusyBox) 下测试的,结果始终是:
String 'echo "My string"' contain substring: 'o "M'.
String 'echo "My string"' don't contain substring: 'alt'.
String 'echo "My string"' contain substring: 'str'.
合一功能
正如@EeroAaltonen 所问,这里是同一演示的一个版本,在相同的外壳下进行了测试:
myfunc() {
reqsubstr="$1"
shift
string="$@"
if [ -z "${string##*$reqsubstr*}" ] ;then
echo "String '$string' contain substring: '$reqsubstr'.";
else
echo "String '$string' don't contain substring: '$reqsubstr'."
fi
}
然后:
$ myfunc 'o "M' 'echo "My String"'
String 'echo "My String"' contain substring 'o "M'.
$ myfunc 'alt' 'echo "My String"'
String 'echo "My String"' don't contain substring 'alt'.
注意:您必须转义或双引号和/或双引号:
$ myfunc 'o "M' echo "My String"
String 'echo My String' don't contain substring: 'o "M'.
$ myfunc 'o "M' echo \"My String\"
String 'echo "My String"' contain substring: 'o "M'.
简单的功能
这是在 BusyBox、Dash,当然还有 Bash 下测试的:
stringContain() { [ -z "${2##*$1*}" ]; }
那么现在:
$ if stringContain 'o "M3' 'echo "My String"';then echo yes;else echo no;fi
no
$ if stringContain 'o "M' 'echo "My String"';then echo yes;else echo no;fi
yes
…或者如果提交的字符串可能是空的,正如@Sjlver 所指出的那样,该函数将变为:
stringContain() { [ -z "${2##*$1*}" ] && [ -z "$1" -o -n "$2" ]; }
或按照 Adrian Günter’s comment 的建议,避免 -o 开关:
stringContain() { [ -z “${2##$1}” ] && { [ -z “$1” ] || [ -n “$2” ];};}
最终(简单)功能:
并反转测试以使它们可能更快:
stringContain() { [ -z "$1" ] || { [ -z "${2##*$1*}" ] && [ -n "$2" ];};}
使用空字符串:
$ if stringContain '' ''; then echo yes; else echo no; fi
yes
$ if stringContain 'o "M' ''; then echo yes; else echo no; fi
no
大小写无关(仅限 Bash!)
要在不区分大小写的情况下测试字符串,只需将每个字符串转换为小写即可:
stringContain() {
local _lc=${2,,}
[ -z "$1" ] || { [ -z "${_lc##*${1,,}*}" ] && [ -n "$2" ] ;} ;}
查看:
stringContain 'o "M3' 'echo "my string"' && echo yes || echo no
no
stringContain 'o "My' 'echo "my string"' && echo yes || echo no
yes
if stringContain '' ''; then echo yes; else echo no; fi
yes
if stringContain 'o "M' ''; then echo yes; else echo no; fi
no
如果你能想办法把它放到一个函数中,这会更好。
@EeroAaltonen 您如何找到我的(新添加的)功能?
我知道!寻找 。 -名称“*”| xargs grep "myfunc" 2> /dev/null
@F.Hauri 抱歉,您对 EuroAaltonen 的评论是个玩笑 find 命令与此线程上发布的问题完全无关。
这很棒,因为它非常兼容。但是有一个错误:如果 haystack 字符串为空,则它不起作用。正确的版本是 string_contains() { [ -z "${2##*$1*}" ] && [ -n "$2" -o -z "$1" ]; } 最后一个想法:空字符串是否包含空字符串?上面的版本是肯定的(因为 -o -z "$1" 部分)。
答5:
huntsbot.com洞察每一个产品背后的需求与收益,从而捕获灵感
您应该记住,shell 脚本与其说是一种语言,不如说是一组命令。您本能地认为这种“语言”要求您在 if 后面加上 [ 或 [[。这两个只是返回指示成功或失败的退出状态的命令(就像所有其他命令一样)。出于这个原因,我会使用 grep,而不是 [ 命令。
做就是了:
if grep -q foo <<<"$string"; then
echo "It's there"
fi
既然您将 if 视为测试它后面的命令的退出状态(用分号完成),为什么不重新考虑您正在测试的字符串的来源呢?
## Instead of this
filetype="$(file -b "$1")"
if grep -q "tar archive" <<<"$filetype"; then
#...
## Simply do this
if file -b "$1" | grep -q "tar archive"; then
#...
-q 选项使 grep 不输出任何内容,因为我们只需要返回码。 <<< 使 shell 扩展下一个单词并将其用作命令的输入,这是 << here 文档的单行版本(我不确定这是标准还是 Bashism)。
他们被称为 here strings (3.6.7) 我相信这是 bashism
也可以使用 Process Substitution if grep -q foo <(echo somefoothing); then
请注意,echo 是不可移植的,如果您要传递变量,请改用 printf '%s' "$string。
这样做的代价非常昂贵:执行 grep -q foo <<<"$mystring" 意味着 1 个 fork 并且是 bashism 并且 echo $mystring | grep -q foo 意味着 2 个 fork(一个用于管道,第二个用于运行 /path/to/grep)
如果参数字符串包含反斜杠序列,则没有标志的 @BrunoBronosky echo 可能仍然存在意外的可移植性问题。 echo "nope\c" 预计在某些平台上会像 echo -e "nope" 在其他平台上一样工作。 printf '%s' "nope" 与 printf '%s\n' 'nope\c'
答6:
与HuntsBot一起,探索全球自由职业机会–huntsbot.com
公认的答案是最好的,但由于有不止一种方法可以做到这一点,这里有另一种解决方案:
if [ "$string" != "${string/foo/}" ]; then
echo "It's there!"
fi
${var/search/replace} 是 $var,如果找到 search 的第一个实例,则替换为 replace(它不会更改 $var)。如果您尝试用任何内容替换 foo,并且字符串已更改,那么显然找到了 foo。
上面 ephemient 的解决方案: >
if [ “
s
t
r
i
n
g
"
!
=
"
string" != "
string"!="{string/foo/}” ];然后回显“它就在那里!” fi 在使用 BusyBox 的 shell ash 时很有用。公认的解决方案不适用于 BusyBox,因为某些 bash 的正则表达式没有实现。
差异的不平等。很奇怪的想法!我喜欢它
除非你的字符串是'foo'
@hanshenrik 您将 $XDG_CURRENT_DESKTOP 与 $string 进行比较。您想要的表达式是 if [ "$XDG_CURRENT_DESKTOP" != "${XDG_CURRENT_DESKTOP/GNOME/}" ]; then echo MATCHES GNOME; fi
@venimus 是的,"x$string" != "x${string/foo/}" 更好。
答7:
huntsbot.com全球7大洲远程工作机会,探索不一样的工作方式
所以这个问题有很多有用的解决方案——但哪个最快/使用最少的资源?
使用此框架重复测试:
/usr/bin/time bash -c 'a=two;b=onetwothree; x=100000; while [ $x -gt 0 ]; do TEST ; x=$(($x-1)); done'
每次更换 TEST:
[[ $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 是可以预见的痛苦!关于无需使用外部实用程序的旧规则仍然适用。
整洁的基准。说服我使用 [[ $b == *$a* ]]。
如果我没看错的话,case 会以最少的总时间消耗获胜。但是,您在 $b in *$a 之后缺少一个星号。在纠正错误后,[[ $b == *$a* ]] 的结果比 case 的结果稍快一些,但它当然也可能取决于其他因素。
ideone.com/5roEVt 我对修复了一些额外的错误进行了实验,并针对不同的场景进行了测试(其中字符串实际上不存在于较长的字符串中)。结果大体相似; [[ $b == *$a* ]] 很快,case 几乎一样快(并且与 POSIX 兼容)。
条件表达式 [[ $b == *$a* ]] 和 case 语句 case $b in *$a):;;esac 在不匹配条件下是不等价的。交换 $a 和 $b 会导致条件表达式 [[ 的退出代码 1 和 case 语句的退出代码 0。根据 help case:退出状态:返回最后执行的命令的状态。如果 no pattern 匹配,则返回状态为零,这可能不是预期的行为。要在不匹配条件下返回 1,它应该是:case $b in *$a*):;; *) false ;; esac
答8:
huntsbot.com – 高效赚钱,自由工作
这也有效:
if printf -- '%s' "$haystack" | egrep -q -- "$needle"
then
printf "Found needle in haystack"
fi
阴性测试是:
if ! printf -- '%s' "$haystack" | egrep -q -- "$needle"
then
echo "Did not find needle in haystack"
fi
我想这种风格更经典一些——更少依赖 Bash shell 的特性。
– 参数是纯粹的 POSIX 偏执狂,用于保护类似于选项的输入字符串,例如 --abc 或 -a。
注意:在紧密循环中,此代码将比使用内部 Bash shell 功能慢得多,因为将创建一个(或两个)单独的进程并通过管道连接。
...但是OP没有说哪个版本的bash;例如,较旧的 bash(如 solaris 经常使用的)可能不包含这些较新的 bash 功能。 (我在带有 bash 2.0 的 solaris 上遇到了这个确切的问题(未实现 bash 模式匹配))
echo 不可移植,您应该改用 printf '%s' "$haystack。
不,除了不以 - 开头的没有转义的文字文本外,只需完全避免 echo。它可能对你有用,但它不是便携式的。根据是否设置了 xpg_echo 选项,甚至 bash 的 echo 的行为也会有所不同。 PS:我忘了关闭我之前评论中的双引号。
@kevinarpe 我不确定,-- 未列在 POSIX spec for printf 中,但无论如何您都应该使用 printf '%s' "$anything",以避免在 $anything 包含 % 字符时出现问题。
@kevinarpe基于此,可能是。
答9:
与HuntsBot一起,探索全球自由职业机会–huntsbot.com
Bash 4+ 示例。注意:当单词包含空格等时,不使用引号会导致问题。始终在 Bash、IMO 中引用。
以下是 Bash 4+ 的一些示例:
示例 1,检查字符串中的“是”(不区分大小写):
if [[ "${str,,}" == *"yes"* ]] ;then
示例 2,检查字符串中的“是”(不区分大小写):
if [[ "$(echo "$str" | tr '[:upper:]' '[:lower:]')" == *"yes"* ]] ;then
示例 3,检查字符串中的“是”(区分大小写):
if [[ "${str}" == *"yes"* ]] ;then
示例 4,检查字符串中的“是”(区分大小写):
if [[ "${str}" =~ "yes" ]] ;then
示例 5,完全匹配(区分大小写):
if [[ "${str}" == "yes" ]] ;then
示例 6,完全匹配(不区分大小写):
if [[ "${str,,}" == "yes" ]] ;then
示例 7,完全匹配:
if [ "$a" = "$b" ] ;then
示例 8,通配符匹配 .ext(不区分大小写):
if echo "$a" | egrep -iq "\.(mp[3-4]|txt|css|jpg|png)" ; then
享受。
Aaaah - 在我发现 ${str,,} 中的两个逗号将 $str 转换为小写之后,我才理解它。很棒的解决方案/很棒的清单!
答10:
HuntsBot周刊–不定时分享成功产品案例,学习他们如何成功建立自己的副业–huntsbot.com
正如他的性能比较中的 Paul mentioned:
if echo "abcdefg" | grep -q "bcdef"; then
echo "String contains is true."
else
echo "String contains is not true."
fi
这与 'the answer provided by Marcus 中的 'case “$string” 一样符合 POSIX,但它比 case 语句的答案更容易阅读。另请注意,这将比使用 case 语句慢得多。正如保罗指出的那样,不要在循环中使用它。
答11:
huntsbot.com精选全球7大洲远程工作机会,涵盖各领域,帮助想要远程工作的数字游民们能更精准、更高效的找到对方。
这个怎么样:
text=" bmnmn "
if [[ "$text" =~ "" ]]; then
echo "matched"
else
echo "not matched"
fi
=~ 用于正则表达式匹配,因此对于 OP 的目的来说太强大了。
huntsbot.com聚合了超过10+全球外包任务平台的外包需求,寻找外包任务与机会变的简单与高效。