我在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
这看起来有点笨拙。
当前回答
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
享受
其他回答
我的.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
我发现我经常需要这个功能,所以我在.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
精确的单词匹配:
string='My long string'
exactSearch='long'
if grep -E -q "\b${exactSearch}\b" <<<${string} >/dev/null 2>&1
then
echo "It's there"
fi
这里回答的问题的扩展如何判断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*"
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
享受