I have the following bash/az cli code:
ip=$(dig @resolver4.opendns.com myip.opendns.com +short)
rg=synapse-test
declare -a workspaces
readarray -t workspaces < <(az synapse workspace list --query "[].name" -o tsv)
for w in "${workspaces[@]}";
do az synapse workspace firewall-rule create --name allowinbound --start-ip-address "$ip" --end-ip-address "$ip" --workspace-name "$w" --resource-group "$rg";
done
If I run it one line at a time, the following error occurs after the for loop:
Operation returned an invalid status code 'Bad Request'
The problem has something to do with the array, because if I provide a workspace name instead of $w, it works. When I run
echo $w
...for the do command, it shows the following (which looks right):
workspace-2
workspace-1
This is literally the first bash script I've ever written, so any help would be appreciated. Thanks.
xtrace log output below:
+ ./addfwrule.sh
++ dig @resolver4.opendns.com myip.opendns.com +short
+ ip=redacted
+ rg=synapse-test
+ readarray -t workspaces
++ az synapse workspace list --query '[].name' -o tsv
WARNING: Command group 'synapse' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
+ for w in "${workspaces[@]}"
+ az synapse workspace firewall-rule create --name allowinbound --start-ip-address redacted --end-ip-address redacted --workspace-name $'workspace-2\r' --resource-group synapse-test
Command group 'synapse' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
Operation returned an invalid status code 'Bad Request'
+ for w in "${workspaces[@]}"
+ az synapse workspace firewall-rule create --name allowinbound --start-ip-address redacted --end-ip-address redacted --workspace-name $'workspace-1\r' --resource-group synapse-test
Command group 'synapse' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
Operation returned an invalid status code 'Bad Request'
working code is:
#!/usr/bin/env bash
ip=$(dig @resolver4.opendns.com myip.opendns.com +short)
rg=synapse-test
readarray -t workspaces < <(az synapse workspace list --query "[].name" -o tsv | tr -d '\r')
for w in "${workspaces[@]}";
do az synapse workspace firewall-rule create --name allowinbound --start-ip-address "$ip" --end-ip-address "$ip" --workspace-name "$w" --resource-group "$rg";
done