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?
echo "yq .$i.image.repository - < ./values.yaml"should printyq ...., yet the "yq" is gone. This usually indicates that there are some weird characters (i.e. control characters, non-printable, ...) in$i. You could inspect the values withecho "$i" | od -c. Gut feeling,$iis not clean.set -xat the beginning of the script to get an execution trace as it runs -- that often clarifies what's going wrong. Also, I second the recommendation of shellcheck.net even if the script seems to be running fine, since it has several quoting problems that could cause trouble even if they aren't right now.