我正在导出所有AWS ElasticBeanstalk环境变量,并使用xargs将输出作为命令行参数。
export $(/opt/elasticbeanstalk/bin/get-config --output YAML environment | sed -r 's/: /=/' | xargs)
get-config
的YAML输出中的大多数字符串都没有用引号引起来,因此当遇到值中有空格的任何环境变量时,上面的命令片段就会中断,因为export
命令使用空格来分隔新键值对的开头。
例如,假设我有一个名为TEST_VARIABLE的环境变量,其值为THIS STRING
,则上面的命令将失败,并显示错误:
-bash: export: `THIS STRING': not a valid identifier
本质上,我的问题是,如何让/opt/elasticbeanstalk/bin/get-config --output YAML environment
给所有字符串加引号?
1条答案
按热度按时间insrf1ej1#
我将使用下面的env.yaml文件作为示例(我不使用AWS BS,所以我不知道是否会有严重的语法差异),下次请提供一个编辑的示例:
环境名称
在任何情况下,管道到
xargs
都很难保留引号(因为它们最终会被shell解释,但随后您需要重新引用它们)。相反,您应该尝试生成等效的(几个)导出行,以便由运行的shell使用,例如
source <( output with several export x="..." lines)
行(bash
、zsh
以及其他行的有效语法)。粘贴以下两种可能性:
仅使用
sed
下面的解决方案(我选择了单引号),假设没有单引号值。
使用
yq
使用https://github.com/kislyuk/yq进行所需的引用,然后使用sed进行
:
替换: