The container gets the ENV from k8s manifest. ARG is used in the container to define local variables after processing the ENV value which contains special characters.
How to escape special characters in BASH shell? Especially those that appear consecutively in a string variable? For example, @@
, $$
, etc? I have tried several but to no avail:
INPUT=hello/P@$$w0rd
arrIN=(${INPUT//\// })
USER=${arrIN[0]}
PASSWORD=${arrIN[1]}
USER=`echo $USER | ( read -rsd '' x; echo ${x@Q} )`
PASSWORD="$(echo "$PASSWORD" | sed -e 's/[()&$]/\\&/g')"
function escape_str () {
echo "$1" | sed 's/\\/\\\\/g' | sed 's/\"/\\"/g' | sed 's/\$/\\$/g'
}
PASSWORD=$(escape_str "$PASSWORD")
All attempts have problem with the double $$
.
Test case: Hello/P@$$w0rd
should result in USER=Hello
and PASSWORD=P@$$w0rd
I need this to work in a Dockerfile:
ARG CREDENTIALS
ENV USER "${CREDENTIALS%/*}"
ENV PASSWORD "${CREDENTIALS#*/}"
CMD echo USER: $USER PASSWORD: $PASSWORD
$ docker run -dt me/myimage:latest --build-arg NEO4J_AUTH='hello/P@$$w0rd'
The container gets the ENV from k8s manifest. ARG is used in the container to define local variables after processing the ENV value which contains special characters.
How to escape special characters in BASH shell? Especially those that appear consecutively in a string variable? For example, @@
, $$
, etc? I have tried several but to no avail:
INPUT=hello/P@$$w0rd
arrIN=(${INPUT//\// })
USER=${arrIN[0]}
PASSWORD=${arrIN[1]}
USER=`echo $USER | ( read -rsd '' x; echo ${x@Q} )`
PASSWORD="$(echo "$PASSWORD" | sed -e 's/[()&$]/\\&/g')"
function escape_str () {
echo "$1" | sed 's/\\/\\\\/g' | sed 's/\"/\\"/g' | sed 's/\$/\\$/g'
}
PASSWORD=$(escape_str "$PASSWORD")
All attempts have problem with the double $$
.
Test case: Hello/P@$$w0rd
should result in USER=Hello
and PASSWORD=P@$$w0rd
I need this to work in a Dockerfile:
ARG CREDENTIALS
ENV USER "${CREDENTIALS%/*}"
ENV PASSWORD "${CREDENTIALS#*/}"
CMD echo USER: $USER PASSWORD: $PASSWORD
$ docker run -dt me/myimage:latest --build-arg NEO4J_AUTH='hello/P@$$w0rd'
Share
Improve this question
edited Mar 25 at 11:24
David Maze
161k46 gold badges249 silver badges289 bronze badges
asked Mar 24 at 8:03
khtehkhteh
4,05210 gold badges59 silver badges104 bronze badges
8
|
Show 3 more comments
1 Answer
Reset to default -4ARG CREDENTIALS
ARG user="${CREDENTIALS%/*}"
ARG password="${CREDENTIALS#*/}"
ENV USER $user
ENV PASSWORD $password
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744255053a4565356.html
input='hello/P@$$w0rd'; user="${input%/*}"; passwd="${input#*/}"; echo "$user"; echo "$passwd"
. Side note: avoid using uppercase variables. They may collide with reserved environment variables. – tshiono Commented Mar 24 at 8:33ARG
. – khteh Commented Mar 24 at 8:40ARG CREDENTIALS
specified? You providedNEO4J_AUTH
on your command line. Same thing? – tjm3772 Commented Mar 24 at 13:17NEO4J_AUTH
before executingdocker run
, and pass the two values separately. – chepner Commented Mar 24 at 15:20