Reputation: 161
I have the following function in a shell script meant to handle some basic grep checks for a server setting in ~/.m2/settings.xml:
SETTINGS_FILE="~/.m2/settings.xml"
SETTINGS_SERVER_ID="<id>ossrh</id>"
check_settings () {
echo grep "$1" "$SETTINGS_FILE"
if grep -q "$1" "$SETTINGS_FILE"; then
echo "Server is set up...\n"; else
echo "DEPLOY FAILED:\n\t$1 not found in ${SETTINGS_FILE}!
fail "Fail :(";
fi
return 0
}
check_settings $SETTINGS_SERVER_ID
Unfortunately, this results in the following message:
grep <id>ossrh</id> ~/.m2/settings.xml
grep: ~/.m2/settings.xml: No such file or directory
DEPLOY FAILED:
<id>ossrh</id> not found in ~/.m2/settings.xml!
Fail :(
Meanwhile, just using grep "<id>ossrh</id>" ~/.m2/settings.xml
in the bash returns <id>ossrh</id>
, which is expected.
I'm wondering if this is an issue of visibility- whether grep can see things in hidden directories (unlikely, considering this question) or whether it is an issue with the argument I'm passing grep (likely, considering how new I am to shell scripting)
Upvotes: 1
Views: 2374
Reputation: 10663
Barmar's answer is right, but you can also write it like this:
SETTINGS_FILE=~/'.m2/settings.xml'
This way you can include special characters and other stuff that should be quoted. Feel free to change single quotes to double quotes if you need to use a variable. (And I always prefer single quotes when there is no variable inside)
However, because you can also use $HOME
instead of ~
, I would write it like this:
SETTINGS_FILE="$HOME/.m2/settings.xml"
Upvotes: 4
Reputation: 782168
The ~/
prefix is not expanded to your home directory when the variable value is in quotes. Change the first line to:
SETTINGS_FILE=~/.m2/settings.xml
The clue to this is that the error message includes the ~
prefix. If the prefix had been expanded, the error message would have contained the absolute pathname to your directory.
Upvotes: 4