如何逃脱从命令中包含特殊符号的命令中返回的值

发布于 2025-01-22 21:02:42 字数 421 浏览 0 评论 0原文

我正在研究使用.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦初启 2025-01-29 21:02:42

您正在使用printf错误。它的第一个参数是格式字符串,它告诉它如何打印实际数据(在其余参数中)。在格式字符串中,指示格式指定符,该格式说明它插入一个数据参数(以及>%之后的内容,都告诉它如何格式化该数据) 。使用类似的内容:

printf 'PASSWORD=%s\n' "$(az keyvault secret show --name app-PASSWORD --vault-name "my-vault" --query "value";)" >>vars.env

或将“ password =”视为数据而不是格式的一部分:

printf '%s\n' "PASSWORD=$(az keyvault secret show --name app-PASSWORD --vault-name "my-vault" --query "value";)" >>vars.env

在这两种情况下,格式字符串中的>%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:

printf 'PASSWORD=%s\n' "$(az keyvault secret show --name app-PASSWORD --vault-name "my-vault" --query "value";)" >>vars.env

or maybe treat the "PASSWORD=" as data rather than part of the format:

printf '%s\n' "PASSWORD=$(az keyvault secret show --name app-PASSWORD --vault-name "my-vault" --query "value";)" >>vars.env

In both of these, the %s in the format string means "insert the next piece of data as a plain string".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文