Windows Git Bash脚本字符串连接不工作



我一直在尝试执行一个bash脚本,它在MacOS中工作完美,但在windows git bash中显示奇怪的行为。Bash脚本正在尝试从yaml文件中读取并打印此示例中的字符串。

Git bash版本:

GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

Yaml文件(values.yaml):

apis:
api1:
image:
repository: xxxx.azurecr.io/api1
api2:
image:
repository: xxxx.azurecr.io/api2
api3:
image:
repository: xxxx.azurecr.io/api3

Bash脚本:


function pull_apis () {
local apps=$(yq ".apis | keys" - < ./values.yaml -o json | jq -n 'inputs[]' --raw-output)
for i in ${apps[@]}
do
echo $i
echo "yq .$i.image.repository - < ./values.yaml"        
repository=$(yq ".apis.$i.image.repository" - < ./values.yaml)
echo "----  $repository"
done
}
pull_apis

结果如下

api1
.image.repository - < ./values.yaml
----  null
api2
.image.repository - < ./values.yaml
----  null
api3
yq .api3.image.repository - < ./values.yaml
----  xxxx.azurecr.io/api3

预期结果:

api1
yq .api1.image.repository - < ./values.yaml
----  xxxx.azurecr.io/api1
api2
yq .api2.image.repository - < ./values.yaml
----  xxxx.azurecr.io/api2
api3
yq .api3.image.repository - < ./values.yaml
----  xxxx.azurecr.io/api3

我已经尝试过静态数组,它的工作原理。但是,当它从文件中读取键时,它不起作用。

请专家在缺失的部分上遮光好吗?

感谢@Gordon指出我检查执行跟踪。在输入set -x之后,它显示错误的事情发生了。

++ pull_apis
++ local apps
+++ yq '.apis | keys' - -o json
+++ jq -n 'inputs[]' --raw-output
++ apps='api1
api2
api3'
++ for i in ${apps[@]}
++ echo $'api1r'
api1

++ echo $'api1r'此值与r一起出现。我能够修复我的脚本后删除它如下,

function pull_apis () {
local apps=$(yq ".apis | keys" - < ./values.yaml -o json | jq -n 'inputs[]' --raw-output)
for i in ${apps[@]}
do
local param="${i/$'r'/}"
echo "$param"        
echo "yq .$param.image.repository - < ./values.yaml"        
repository=$(yq ".apis.$param.image.repository" - < ./values.yaml)
echo "----  $repository"
done
}
pull_apis

最新更新