5

Following code works for bash but now i need it for busybox ash , which apparrently does not have "=~"

keyword="^Cookie: (.*)$"
if [[ $line =~ $keyword ]]
then
bla bla
fi

Is there a suitable replacement ?

Sorry if this is SuperUser question, could not decide.

Edit: There is also no grep,sed,awk etc. I need pure ash.

4

3 回答 3

4

对于这个特定的正则表达式,您可能会使用参数扩展技巧:

if [ "$line" = "Cookie: ${line#Cookie: }" ]; then
    echo a
fi

模式匹配符号+大小写破解:

case "$line" in
    "Cookie: "*)
        echo a
    ;;
    *)
    ;;
esac

然而,这些解决方案严格来说不如正则表达式强大,因为它们没有真正的 Kleene 星号*(只有.*),并且您确实应该在该系统上安装一些更强大的工具(真正的编程语言,如 Python?),否则您受到影响。

于 2014-01-09T07:27:29.267 回答
3

Busybox 带有一个expr可以进行正则表达式匹配的小程序(锚定到字符串的开头)。如果正则表达式匹配,则其返回码将为 0。示例:

 # expr "abc" : "[ab]*"
 # echo $?
 0
 # expr "abc" : "[d]*"
 # echo $?
 1
于 2015-03-25T08:15:27.937 回答
3

对我有用的是使用 Busy Box 的grepwc实现:

MATCHES=`echo "$BRANCH" | grep -iE '^(master|release)' | wc -l`
if [ $MATCHES -eq 0 ]; then
 echo 'Not on master or release branch'
fi
于 2019-12-16T15:10:57.647 回答