bash - Docker container processes ENV from k8s which contains special characters, especially those that appear consecutively in

The container gets the ENV from k8s manifest. ARG is used in the container to define local variables af

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
  • 3 Quote the string and variables properly: 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:33
  • This seems to work on local environment but doesn't work well in Dockerfile. The input is ARG. – khteh Commented Mar 24 at 8:40
  • Where is ARG CREDENTIALS specified? You provided NEO4J_AUTH on your command line. Same thing? – tjm3772 Commented Mar 24 at 13:17
  • Please define what exactly you meann by special character. The @ does not have any special meaning to bash in general, only inside a parameter substitution. – user1934428 Commented Mar 24 at 13:28
  • 1 If the container just needs the user name and password, I'd split the value of NEO4J_AUTH before executing docker run, and pass the two values separately. – chepner Commented Mar 24 at 15:20
 |  Show 3 more comments

1 Answer 1

Reset to default -4
ARG CREDENTIALS
ARG user="${CREDENTIALS%/*}"
ARG password="${CREDENTIALS#*/}"
ENV USER $user
ENV PASSWORD $password

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744255053a4565356.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信