1

I have the following bash script code:

GOAL="${1:-help}"
TARGET="${2}"
MODULES_LIST="app|tester"

echo "-> Running $TARGET..."
MODULES_LIST_PATTERN = "^($MODULES_LIST)$"
if [[ "$TARGET" =~ $MODULES_LIST_PATTERN ]]; then
    run_${TARGET}
else
    print_error "You must include an existing module: {$MODULES_LIST}"
    exit 1
fi

As you can see, I have a MODULES_LIST variable where I store the modules supported by the application, then I create a regex pattern MODULES_LIST_PATTERN containing the value of the previous var, and use it to check if the provided parameter matches any of the modules. However, it is not working as expected, since when I run ./myscript.sh run app it prints ERROR] You must include an existing module: {app|tester}.

Could someone tell me the proper way of doing this?

1 Answer 1

2

My fault... the problem is MODULES_LIST_PATTERN = "^($MODULES_LIST)$", you can't have whitespaces surrounding the equals sign. After fixing that, it works as expected.

Sign up to request clarification or add additional context in comments.

1 Comment

Appreciate you coming back to this and answering it immensely!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.