Reputation: 554
I have been trying to execute a bash script which works perfect in MacOS but showing weird behavior in windows git bash. Bash script is trying to read from yaml file and print the string for this example.
Git bash version:
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 File (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 Script:
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
It shows below result
api1
.image.repository - < ./values.yaml
---- null
api2
.image.repository - < ./values.yaml
---- null
api3
yq .api3.image.repository - < ./values.yaml
---- xxxx.azurecr.io/api3
Expected result:
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
I have tried with static array and it works. However, it doesn't work when it reads keys from file.
Could some expert please shade some light on missing part?
Upvotes: 1
Views: 412
Reputation: 554
Thanks @Gordon to point me out to check execution trace. After putting set -x
, it shows me the wrong thing happening.
++ pull_apis
++ local apps
+++ yq '.apis | keys' - -o json
+++ jq -n 'inputs[]' --raw-output
++ apps='api1
api2
api3'
++ for i in ${apps[@]}
++ echo $'api1\r'
api1
++ echo $'api1\r'
this value was coming with \r. I am able to fix my script after removing it as below,
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
Upvotes: 1