admin管理员组文章数量:1400336
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
本文标签:
版权声明:本文标题:bash - Docker container processes ENV from k8s which contains special characters, especially those that appear consecutively in 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744255053a2597438.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