简体   繁体   English

Bash,变量的行包含substring?

[英]Bash, lines of a variable contain substring?

While trying to write a robust bash method that detects whether a variable that consists of multiple lines of text, contains a substring (that may contain spaces) I am experiencing some difficulties.在尝试编写一个可靠的 bash 方法来检测由多行文本组成的变量是否包含 substring(可能包含空格)时,我遇到了一些困难。 Three functions were written and I wrote some tests for them using the BATS framework.编写了三个函数,我使用BATS框架为它们编写了一些测试。 The functions and the accompanying tests on which they fail, are included below.它们失败的功能和伴随的测试包括在下面。

Method I方法一

lines_contain_string() {
    local substring="$1"
    shift
    local lines=("$@")
    
    local found_substring="NOTFOUND"

    for i in "${lines[@]}"; do
        if [[ $i == *"$substring"* ]]; then
            echo "FOUND"
            local found_substring="FOUND"
        fi
    done

    if [ "$found_substring" == "NOTFOUND" ]; then
        echo "NOTFOUND"
    fi
}

This fails on test:这在测试中失败:

EXAMPLE_LINES=$(cat <<-END
First line
second line 
third line 
sometoken
END
)

@test "Substring in first line is found in lines by lines_contain_string." {
    contained_substring="First line"
    
    actual_result=$(lines_contain_string "$contained_substring" "\${EXAMPLE_LINES}")
    #actual_result=$(lines_contain_string_with_space "$contained_substring" "\${EXAMPLE_LINES}")
    EXPECTED_OUTPUT="FOUND"
        
    assert_equal "$actual_result" "$EXPECTED_OUTPUT"
}

So the first method does not catch all substrings.所以第一种方法不会捕获所有子字符串。

Method II方法二

And for the second method:对于第二种方法:

lines_contain_string_with_space() {
    local substring="$1"
    local lines="$@"
    if [ "$lines" == "" ]; then
        echo "NOTFOUND"
    # shellcheck disable=SC2154
    elif [[ "$lines" =~ "$substring" ]]; then
        echo "FOUND"; 
    else
        echo "NOTFOUND";
    fi
}

This seems to return false positive/seems to always return "FOUND", for example, on test:这似乎返回误报/似乎总是返回“FOUND”,例如,在测试中:

EXAMPLE_LINES=$(cat <<-END
First line
second line 
third line 
sometoken
END
)

@test "lines_contain_string returns NOTFOUND on non-existing substring." {
    contained_substring="Non-existing-substring"
    
    #actual_result=$(lines_contain_string "$contained_substring" "\${EXAMPLE_LINES}")
    actual_result=$(lines_contain_string_with_space "$contained_substring" "\${EXAMPLE_LINES}")
    EXPECTED_OUTPUT="NOTFOUND"
        
    assert_equal "$actual_result" "$EXPECTED_OUTPUT"
}

Method III方法三

This method is provided by choroba:这个方法是choroba提供的:

string_in_lines() {
    local substring=$1
    shift
    local lines=$1
    if [[ $lines = *"$substring"* ]] ; then
        echo FOUND
    else
        echo NOTFOUND
    fi
}

And calling it with test:并通过测试调用它:

@test "Substring in first line is found in lines by lines_contain_string." {
    contained_substring="First line"
    
    read -p "EXAMPLE_LINES=$EXAMPLE_LINES"

    #actual_result=$(lines_contain_string "$contained_substring" "\${EXAMPLE_LINES}")
    #actual_result=$(lines_contain_string_with_space "$contained_substring" "\${EXAMPLE_LINES}")
    actual_result=$(string_in_lines "$contained_substring" "\${EXAMPLE_LINES}")
    EXPECTED_OUTPUT="FOUND"
        
    assert_equal "$actual_result" "$EXPECTED_OUTPUT"
}

Produces output:产生 output:

 ✗ Substring in first line is found in lines by lines_contain_string.
   (from function `assert_equal' in file test/no_server_required/preserves_server/../../libs/bats-assert/src/assert_equal.bash, line 40,
    in test file test/no_server_required/preserves_server/test_parsing.bats, line 35)
     `assert_equal "$actual_result" "$EXPECTED_OUTPUT"' failed
   EXAMPLE_LINES=First line
   second line 
   third line 
   sometoken
   -- values do not equal --
   expected : FOUND
   actual   : NOTFOUND
   --

Question问题

How can one echo "FOUND"/"NOTFOUND" in a bash function, based on whether an incoming variable, consisting of multiple lines, contains a substring that may contain spaces?如何根据由多行组成的传入变量是否包含可能包含空格的 substring,在 bash function 中回显“FOUND”/“NOTFOUND”? (And test it accordingly.) (并相应地进行测试。)

Note笔记

Since my tests also fail on the method provided by choroba, which I assume has a large likelihood of being successful, I expect my test functions are not correct.由于我的测试在 choroba 提供的方法上也失败了,我认为成功的可能性很大,所以我预计我的测试函数不正确。 I updated the question accordingly.我相应地更新了问题。

The comments by choroba suggested the backslash in: `"${EXAMPLE_LINES}" was incorrect. choroba 的评论建议反斜杠:`"${EXAMPLE_LINES}" 不正确。 I tested whether that was incorrect, and it turned out it was incorrect.我测试了一下是不是不对,结果是不对。 So the solution was to write the test as:所以解决方案是将测试编写为:

@test "Substring in first line is found in lines by lines_contain_string." {
    contained_substring="First line"

    #actual_result=$(lines_contain_string "$contained_substring" "${EXAMPLE_LINES}")
    #actual_result=$(lines_contain_string_with_space "$contained_substring" "${EXAMPLE_LINES}")
    actual_result=$(string_in_lines "$contained_substring" "${EXAMPLE_LINES}")
    EXPECTED_OUTPUT="FOUND"
        
    assert_equal "$actual_result" "$EXPECTED_OUTPUT"
}

@test "lines_contain_string returns NOTFOUND on non-existing substring." {
    contained_substring="Non-existing-substring"
    
    #actual_result=$(lines_contain_string "$contained_substring" "${EXAMPLE_LINES}")
    #actual_result=$(lines_contain_string_with_space "$contained_substring" "${EXAMPLE_LINES}")
    actual_result=$(string_in_lines "$contained_substring" "${EXAMPLE_LINES}")
    EXPECTED_OUTPUT="NOTFOUND"
        
    assert_equal "$actual_result" "$EXPECTED_OUTPUT"
}

Use = with a wildcard pattern.=与通配符模式一起使用。

string_in_lines() {
    local substring=$1
    shift
    local lines=$1
    if [[ $lines = *"$substring"* ]] ; then
        echo FOUND
    else
        echo NOTFOUND
    fi
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM