0

This bash script when run on Mac terminal, it needs to ask for user input, then it needs to check if a a string "PLACEHOLDER_BACKEND_NAME="user-input" exists in a given file and if not it should exit the script.

echo -e "${YELLOW}enter app name${WHITE}"
    read name

line=grep $name /path/to/file/entrypoint.sh

if [[ line != "PLACEHOLDER_BACKEND_NAME=\"$name\"" ]] ; then
    exit 1
fi

It needs much tuning as I am not very familiar with bash scripts. any suggestions? thx

1 Answer 1

1

Your code needs a little tweaking:

echo -e "${YELLOW}enter app name${WHITE}"
read -r name

if ! grep -q PLACEHOLDER_BACKEND_NAME="\"$name\"" /path/to/file/entrypoint.sh; then
    exit 1
fi
Sign up to request clarification or add additional context in comments.

4 Comments

@gniourf_gniourf: made changes as per your suggestions. Thanks!
@codeforester also: you probably want to use fgrep instead of grep because accidental special regex syntax in $name can do funny things to your grep. Furthermore you also should consider using -x because you want to match the entire line, right? (To avoid false positives from substring matches).
@codeforester this answer failed to exit the script when the line is not found which is what is needed as per the original post.
@FredJ. - changed grep to ! grep. That should do it!

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.