Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 将变量从.sh文件发送到.yml_Bash_Shell_Github_Yaml - Fatal编程技术网

Bash 将变量从.sh文件发送到.yml

Bash 将变量从.sh文件发送到.yml,bash,shell,github,yaml,Bash,Shell,Github,Yaml,我正在尝试从yml文件运行shell脚本,并将从shell脚本生成的变量插入yml文件..但我无法这样做。 这是yml文件 版本:0.2 phases: install: - bash build.sh - cd $DIR 这就是构建脚本的功能 #!/bin/bash directory=$(git log -1 --pretty=%B | awk '{print $1;}') echo " directory: $directory" build up -d -e

我正在尝试从yml文件运行shell脚本,并将从shell脚本生成的变量插入yml文件..但我无法这样做。 这是yml文件 版本:0.2

phases:
  install:
    - bash build.sh
    - cd $DIR
这就是构建脚本的功能

#!/bin/bash
  directory=$(git log -1 --pretty=%B | awk '{print $1;}')
echo "   directory: $directory"
build up -d -e DIR:$directory
export DIR=$directory
shell脚本所做的一切就是获取git commit消息。我希望将它传输到调用shell脚本的yml文件中,这是一个既有效又有效的YAML和JSON表示形式 考虑任何有效的YAML解析器都会接受的以下内容:

{"phases": {"install": ["bash build.sh", "cd $DIR"]}}

使用
jq
dir=/foo/bar/baz##这可以是任何东西
jq--参数目录“$dir”
.phases.install=[(.phases.install | map(sub(“[$]DIR”;($DIR |@sh)))]
'out.json
…将用正确引用的目录名版本正确替换任何可能的目录名。(这确实要求
$DIR
不能在引用的上下文中使用;例如,如果使用
“$DIR”
,则
@sh
助手在
jq
中添加的引用字符将被视为文字)


是的,因为所有JSON都是有效的YAML,所以这个结果是YAML。

您是否关心保留当前格式的文件?由于YAML是JSON的超集,您可以用JSON来表达这一点,并使用
jq
来进行转换。感谢您的回复@CharlesDuffy..yes file应该是YAML,因为我正在尝试使用Travis CI实现一些功能,它只在YAML文件上工作,在YAML文件上工作的所有东西也在JSON文件上工作(除非依赖于JSON不能表示的数据结构),因为每个JSON文件也是一个YAML文件。一个是另一个的超集。因此,我所说的“每个表单”,并不是要问你是否需要它来保持有效的YAML——我的意思是问你是否想保持有效的块形式YAML(格式化YAML的一种特定方法,它也不是有效的JSON)。(回答这个问题的简单方法只是执行字符串替换,但允许格式错误的值注入任意内容;语法感知解析和生成更安全)。假设shell脚本在子shell中执行,则不需要设置任何变量(或导出)将对父shell可见。您需要
source build.sh
。可能…I取决于yaml如何转化为实际执行。
dir=/foo/bar/baz  ## this can be anything
jq --arg dir "$dir" '
.phases.install = [ (.phases.install | map(sub("[$]DIR"; ($dir | @sh))))]
' <in.json >out.json