문제

I'm moving a bash script to dash for compatibility reasons. Is there a POSIX/Dash alternative to the following comparison?

COMPARE_TO="^(lp:~?|https?://|svn://|svn\+ssh://|bzr://|bzr\+ssh://|git://|ssh://)"

if [[ $COMPARE =~ $COMPARE_TO ]]; then
    echo "WE ARE COMPARED!"
fi
도움이 되었습니까?

해결책

You can use a case. It doesn't use regex, but it's not that much longer with globs

case $compare in
    lp:*|http://*|https://*|svn://*|svn+ssh://*|bzr://*|bzr+ssh://*|git:/*|ssh://*)
        echo "We are compared"
    ;;
esac

On a side note, you should avoid using all uppercase variable names as you risk overwriting special shell variables or environment variables.

다른 팁

dash doesn't have regex comparing built in, but you can always use grep:

if echo "$compare" | egrep -q "$compare_to"; then
    ...

(Note that I second @geirha's note about uppercase variables in the shell.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top