Given:
~$ cat .test-profile
export [email protected]
export MONGODB_URI='mongodb+srv://administrator:[email protected]/test?retryWrites=true&w=majority'
I wrote the following shell script to replace the variable values:
BASH_PROFILE="$HOME/.test-profile"
MONGODB_URI="mongodb+srv://administrator:[email protected]/mysite_development?retryWrites=true&w=majority"
INSERT_ENV_VAR () {
if [[ -z "2ドル" ]]
then
echo "Failed to retrieve 1ドル"
else
if grep -q "1ドル" "$BASH_PROFILE"; then
# Cannot use / delimiter, for value can contain /
sed -in -E "s|(export 1ドル=)[a-zA-Z0-9@.:\/=&\-\?\+]+|1円2ドル|g" "$BASH_PROFILE"
else
echo "export 1ドル=2ドル" >> "$BASH_PROFILE"
fi
fi
}
INSERT_ENV_VAR 'ADMIN_EMAIL' '[email protected]'
INSERT_ENV_VAR 'MONGODB_URI' $MONGODB_URI
While the ADMIN_EMAIL variable is replaced fine, the MONGODB_URI variable is not being replaced. And I believe it has to do something with the character class [] inside of the regex. But I am not sure what. I tried it in a programming language like ruby and it found the match. But with sed it isn't finding the match. Why?
This is what I expect .test-profile
to look like afterwards:
export [email protected]
export MONGODB_URI='mongodb+srv://administrator:[email protected]/mysite_development?retryWrites=true&w=majority'
Notice 'mysite_development' replaces 'test'.
2 Answers 2
Escaping the ampersand in the definition of MONGODB_URI in the script and simplifying the regex, getting rid of the -n
and getting rid of the unnecessary g
in the sed, we end up with this (the test profile is named differently in this version, but it should be clear what to do):
BASH_PROFILE="/tmp/test_profile"
MONGODB_URI="mongodb+srv://administrator:[email protected]/mysite_development?retryWrites=true\&w=majority"
INSERT_ENV_VAR () {
if [[ -z "2ドル" ]]
then
echo "Failed to retrieve 1ドル"
else
if grep -q "1ドル" "$BASH_PROFILE"; then
# Cannot use / delimiter, for value can contain /
sed -i -E "s;(export 1ドル=).*;1円2ドル;" "$BASH_PROFILE"
else
echo "export 1ドル=2ドル" >> "$BASH_PROFILE"
fi
fi
}
INSERT_ENV_VAR 'ADMIN_EMAIL' '[email protected]'
INSERT_ENV_VAR 'MONGODB_URI' $MONGODB_URI
and running this with the test profile, I get:
export [email protected]
export MONGODB_URI=mongodb+srv://administrator:[email protected]/mysite_development?retryWrites=true&w=majority
-
Yes, I deleted the comment earlier because it did work.Daniel Viglione– Daniel Viglione2020年02月16日 02:44:30 +00:00Commented Feb 16, 2020 at 2:44
-
This will result in some inconsistency – when
grep
succeeds andsed
is used the replacement made by sed won't contain abackslash
before the&
, but if the append-echo
is used the `\` remainsuser391836– user3918362020年02月17日 12:58:21 +00:00Commented Feb 17, 2020 at 12:58
sed -i "/export 1ドル=/{i export 1ドル='2ドル'
;d}" "$BASH_PROFILE"
This will allow you to use sed
without needing to replace &
with \&
grep -q
could have the options-F
and also-m 1
You must log in to answer this question.
Explore related questions
See similar questions with these tags.
'
character. Try your sed with(export MONGODB_URI=)[a-zA-Z0-9@.:\/=&\-\?\+']+
(adds a'
to the list of valid characters).&
in the replacement string."s|(export 1ドル=).*|1円2ドル|"
? And why useg
? But you still have to escape&
in2ドル
.