Im trying to build dynamically a json using a bash script with a loop inside:
touch credScanSuppressionsFile.json
echo '{"tool": "Credential Scanner", "suppressions":[]}' | jq > credScanSuppressionsFile.json
webapp_name=customer-brasil-forms
for env in develop #staging demo preprod prod
do
file='azure_portal_variables\'$env-$webapp_name.json
echo $file
jq --arg file $file '.suppressions = [{"file": $file, "_justification": "app setting file"}]' <<< "$(cat credScanSuppressionsFile.json)" > credScanSuppressionsFile.json
done
Everything looks like its working... but when you see the output:
{
"tool": "Credential Scanner",
"suppressions": [
{
"file": "azure_portal_variables\\develop-customer-brasil-forms.json",
"_justification": "app setting file"
}
]
}
The desire output should be (be aware of the filename with double slash):
{
"tool": "Credential Scanner",
"suppressions": [
{
"file": "azure_portal_variables\develop-customer-brasil-forms.json",
"_justification": "app setting file"
}
]
}
CodePudding user response:
Please do the shellcheck as I recommended you to here
$ shellcheck myscript
Line 8:
for env in develop #staging demo preprod prod
^-- SC2043 (warning): This loop will only ever run once. Bad quoting or missing glob/expansion?
Line 10:
file='azure_portal_variables\'$env-$webapp_name.json
^-- SC1003 (info): Want to escape a single quote? echo 'This is how it'\''s done'.
Line 12:
jq --arg file $file '.suppressions = [{"file": $file, "_justification": "app setting file"}]' <<< "$(cat credScanSuppressionsFile.json)" > credScanSuppressionsFile.json
>> ^-- SC2094 (info): Make sure not to read and write the same file in the same pipeline.
>> ^-- SC2094 (info): Make sure not to read and write the same file in the same pipeline.
$
Once you have fixed all the shell issues, then we can start helping you about the jq
issue.
CodePudding user response:
Finally, the chosen code, at least a valid one for my case, is:
touch credScanSuppressionsFile.json
echo '{"tool": "Credential Scanner", "suppressions":[]}' | jq > credScanSuppressionsFile.json
webapp_name=customer-brasil-forms
for env in develop #staging demo preprod prod
do
file=$env-$webapp_name.json
echo $file
jq --arg file $file '.suppressions = [{"file": $file, "_justification": "app setting file"}]' <<< "$(cat credScanSuppressionsFile.json)" > credScanSuppressionsFile.json
done
I have realised that just using the name of the file is enough.
Things to highlight is that @Armali is right, the json as it was (my supposed wrong result) is correct and the backslash should be scaped with double backslash and @LeaGris thanks for your help, but the code was correct after the edition, the errors you see its just info in case you want to use it.
touch credScanSuppressionsFile.json
echo '{"tool": "Credential Scanner", "suppressions":[]}' | jq > credScanSuppressionsFile.json
webapp_name=customer-brasil-forms
for env in develop staging demo preprod prod
do
file='azure_portal_variables\'$env-$webapp_name.json
echo $file
jq --arg file $file '.suppressions = [{"file": $file, "_justification": "app setting file"}]' <<< "$(cat credScanSuppressionsFile.json)" > credScanSuppressionsFile.json
done
This code is also correct