我在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
看起来有点笨拙。
text=" <tag>bmnmn</tag> "
if [[ "$text" =~ "<tag>" ]]; then
echo "matched"
else
echo "not matched"
fi
被采纳的答案是正确的,但很难阅读和理解。
对于与搜索相关的问题,您应始终使用$needle in a $haystack习语。
由于其建议的编辑队列已满,我发布了这个内容:
haystack='There are needles here.'
if [[ "$haystack" == *"needle"* ]]; then
echo "It's there!"
fi
bash
下工作,当用于脚本文件时,必须以 #!/bin/bash
开头。 - Sam Sirry[[ $string == *foo* ]] && echo "It's there" || echo "Couldn't find"
echo "Couldn't find
语句是一个很好的技巧,可以为这些匹配命令返回0的退出状态。 - nicodjimenez|| echo "Couldn't find"
,那么如果没有匹配,你将返回一个错误退出状态,如果你正在运行持续集成管道等情况,你可能不想要这个错误退出状态。 - nicodjimenez第一个问题是:
[ $(expr $mystring : ".*${search}.*") -ne 0 ] && echo 'yes' || echo 'no'
expr
,当可移植性阻止使用bash(例如,在旧版本中行为不一致),tr(无处不在的不一致)或sed(有时太慢)时。但从个人经验来看,每当重新阅读这些expr
-isms时,我都必须回到man页面。因此,我建议对expr
的每个用法进行注释... - michaelexpr
和test
这样的工具来执行它们。在今天,通常有更好的工具,其中许多内置于任何现代shell中。我猜test
仍然存在,但似乎没有人想念expr
。 - tripleeeexpr: syntax error: unexpected argument ‘.*.*’
bash: [: -ne: unary operator expected
- Nairum这篇 Stack Overflow 的答案 是唯一一个捕获空格和破折号的答案:
# For null cmd arguments checking
to_check=' -t'
space_n_dash_chars=' -'
[[ $to_check == *"$space_n_dash_chars"* ]] && echo found
由于POSIX/BusyBox问题没有提供正确答案(在我看来),我将在此发布一个答案。
最简短的答案是:
[ ${_string_##*$_substring_*} ] || echo Substring found!
或者
[ "${_string_##*$_substring_*}" ] || echo 'Substring found!'
ash
)中,双井号是强制性的。上面的代码将在未找到子字符串时评估[ stringvalue ]
。它不会返回错误。当找到子字符串时,结果为空,并且它会评估[ ]
。由于*
,这将抛出错误代码1,因为字符串被完全替换。
最短常见语法:
[ -z "${_string_##*$_substring_*}" ] && echo 'Substring found!'
或者
[ -n "${_string_##*$_substring_*}" ] || echo 'Substring found!'
另一个:
[ "${_string_##$_substring_}" != "$_string_" ] && echo 'Substring found!'
或者
[ "${_string_##$_substring_}" = "$_string_" ] || echo 'Substring found!'
#!/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
我的.bash_profile文件以及我如何使用grep:
如果PATH环境变量包括我的两个bin
目录,则不要追加它们。
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
U=~/.local.bin:~/bin
if ! echo "$PATH" | grep -q "home"; then
export PATH=$PATH:${U}
fi
grep -q -E 'pattern1|...|patternN'
。 - Mohamed Bana这是对此问题的扩展回答:如何在POSIX sh中判断一个字符串是否包含另一个字符串?:
这个解决方案适用于特殊字符:
# contains(string, substring)
#
# Returns 0 if the specified string contains the specified substring,
# otherwise returns 1.
contains() {
string="$1"
substring="$2"
if echo "$string" | $(type -p ggrep grep | head -1) -F -- "$substring" >/dev/null; then
return 0 # $substring is in $string
else
return 1 # $substring is not in $string
fi
}
contains "abcd" "e" || echo "abcd does not contain e"
contains "abcd" "ab" && echo "abcd contains ab"
contains "abcd" "bc" && echo "abcd contains bc"
contains "abcd" "cd" && echo "abcd contains cd"
contains "abcd" "abcd" && echo "abcd contains abcd"
contains "" "" && echo "empty string contains empty string"
contains "a" "" && echo "a contains empty string"
contains "" "a" || echo "empty string does not contain a"
contains "abcd efgh" "cd ef" && echo "abcd efgh contains cd ef"
contains "abcd efgh" " " && echo "abcd efgh contains a space"
contains "abcd [efg] hij" "[efg]" && echo "abcd [efg] hij contains [efg]"
contains "abcd [efg] hij" "[effg]" || echo "abcd [efg] hij does not contain [effg]"
contains "abcd *efg* hij" "*efg*" && echo "abcd *efg* hij contains *efg*"
contains "abcd *efg* hij" "d *efg* h" && echo "abcd *efg* hij contains d *efg* h"
contains "abcd *efg* hij" "*effg*" || echo "abcd *efg* hij does not contain *effg*"
contains "-n" "n"
,因为 echo -n
会将 -n
当作选项而忽略它!一个常见的解决方法是使用 printf "%s\n" "$string"
。 - joeytwiddle我喜欢使用sed。
substr="foo"
nonsub="$(echo "$string" | sed "s/$substr//")"
hassub=0 ; [ "$string" != "$nonsub" ] && hassub=1
编辑,逻辑:
使用sed从字符串中删除子字符串的实例
如果新字符串与旧字符串不同,则子字符串存在
expr
命令。 - cifer