我在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
这看起来有点笨拙。
当前回答
我发现我经常需要这个功能,所以我在.bashrc中使用了一个自制的shell函数,这样我就可以随时重用它,并使用一个容易记住的名字:
function stringinstring()
{
case "$2" in
*"$1"*)
return 0
;;
esac
return 1
}
要测试$string1(例如,abc)是否包含在$string2(例如,123abcABC)中,我只需要运行striginstring“$string1”“$string2”并检查返回值,例如
stringinstring "$str1" "$str2" && echo YES || echo NO
其他回答
您应该记住,shell脚本不是一种语言,而是一组命令。你本能地认为这种“语言”要求你在if后面加一个[或一个[[。这两个命令都只是返回一个表示成功或失败的退出状态的命令(就像其他所有命令一样)。出于这个原因,我会使用grep,而不是[命令。
只要做到:
if grep -q foo <<<"$string"; then
echo "It's there"
fi
既然您正在考虑如果测试它后面的命令的退出状态(用分号完成),为什么不重新考虑您正在测试的字符串的源代码?
## 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文档的一行版本(我不确定这是标准还是巴什主义)。
grep-q对于这个目的很有用。
同样使用awk:
string="unix-bash 2389"
character="@"
printf '%s' "$string" | awk -vc="$character" '{ if (gsub(c, "")) { print "Found" } else { print "Not Found" } }'
输出:
未找到
string="unix-bash 2389"
character="-"
printf '%s' "$string" | awk -vc="$character" '{ if (gsub(c, "")) { print "Found" } else { print "Not Found" } }'
输出:
建立
原始来源:http://unstableme.blogspot.com/2008/06/bash-search-letter-in-string-awk.html
因此,这个问题有很多有用的解决方案——但哪一个最快/使用的资源最少?
使用此框架重复测试:
/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是可想而知的痛苦!关于无需使用外部实用程序的旧规则是正确的。
公认的答案是最好的,但由于有不止一种方法可以做到这一点,这里有另一种解决方案:
if [ "$string" != "${string/foo/}" ]; then
echo "It's there!"
fi
${var/search/replace}是$var,如果找到了第一个搜索实例,则将其替换为replace(它不会更改$var)。如果您试图将foo替换为空,并且字符串发生了更改,那么很明显找到了foo。
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
享受