如何逃脱从命令中包含特殊符号的命令中返回的值
我正在研究使用.ENV扩展名创建文件的Shell脚本,该文件包含我从Azure键Vault获得的环境变量。 问题是,当我获取密码时,它包含一个特殊字符“%”,该字符被解释为未知命令,其中包含以下消息: %n:无效指令
文件看起来像:
#!/bin/zsh
touch vars.env
printf "PASSWORD=$(az keyvault secret show --name app-PASSWORD --vault-name "my-vault" --query "value";) \n" >>vars.env
文件中的结果(仅在“%”符号之前的零件):
password =“ bkt39f
问题是如何逃脱它,以便整个密码将写在文件中而不是其中的一部分中。
I'm working on the shell script that creates a file with .env extension that contains environment variables that I'm getting from azure key vault.
The problem is that when I'm fetching a password it contains a special character "%" that is being interpreted as an unknown command with the following message:
%N: invalid directive
The file looks like:
#!/bin/zsh
touch vars.env
printf "PASSWORD=$(az keyvault secret show --name app-PASSWORD --vault-name "my-vault" --query "value";) \n" >>vars.env
and the result in the file (only the part before "%" symbol):
PASSWORD="bKt39f
The question is how to escape it so the whole password would be written in the file instead of a part of it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
printf
错误。它的第一个参数是格式字符串,它告诉它如何打印实际数据(在其余参数中)。在格式字符串中,%
指示格式指定符,该格式说明它插入一个数据参数(以及>%
之后的内容,都告诉它如何格式化该数据) 。使用类似的内容:或将“ password =”视为数据而不是格式的一部分:
在这两种情况下,格式字符串中的
>%s
表示“将下一个数据插入一个普通的字符串”。You're using
printf
wrong. Its first argument is a format string, which tells it how to print the actual data (which is in the rest of the arguments). In the format string,%
indicates a format specifier, which tells it to insert one of the data arguments (and the stuff immediately after%
tells it how to format that data). Use something like this:or maybe treat the "PASSWORD=" as data rather than part of the format:
In both of these, the
%s
in the format string means "insert the next piece of data as a plain string".