0

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
egrok
  • 55
  • 4
  • Is this the complete code? `ip` and `rg` are not defined. Please provide a [mre] including the full code. Then try running your code through [ShellCheck](https://www.shellcheck.net/) and fix any problems it identifies, like quoting variables. [Variables should almost always be quoted in Bash.](/a/10067297/4518341) – wjandrea Jan 06 '22 at 01:30
  • I've added the whole error and included the ip and rg variables. – egrok Jan 06 '22 at 01:36
  • Is that really the whole error message? There's nothing in front of it like `/bin/bash: ` or `az: `? – wjandrea Jan 06 '22 at 01:38
  • Yeah that's the entire error. – egrok Jan 06 '22 at 01:39
  • 1
    I ran it through ShellCheck, had me fix quotes. I edited the code above to be the checked version. Getting same error. – egrok Jan 06 '22 at 01:45
  • 1
    Add the command `set -x` to the top of the script, just after the shebang (or run it with `bash -x yourscript`) to enable trace logging -- there may be DOS newlines or other hidden characters present. Editing the generated log into your question would be appreciated. – Charles Duffy Jan 06 '22 at 01:46
  • 1
    (insofar as there isn't a shebang included in the question, btw, do add one if it's not present in the real file; a first line of something like `#!/usr/bin/env bash` tells the operating system which interpreter to invoke when running a script) – Charles Duffy Jan 06 '22 at 01:49
  • (btw, you don't need the `declare -a` here -- `readarray -t` makes the destination a numerically-indexed array on its own) – Charles Duffy Jan 06 '22 at 01:51
  • Because you're requesting TSV output, I could also see there being something like a stray tab after your content -- that won't show up in `echo` output, _especially_ when you aren't quoting the arguments to `echo`. So we _really_ need that xtrace log I asked for earlier. (Relevant: [I just assigned a variable, but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else)) – Charles Duffy Jan 06 '22 at 01:56
  • 1
    I added the trace log. The value for the workspaces is incorrect - $'workspace-1\r'. Thank you. How can I fix that variable value? – egrok Jan 06 '22 at 02:15
  • That's a DOS newline. We have a canonical duplicate describing how to deal with them. – Charles Duffy Jan 06 '22 at 02:32
  • One easy approach is to pipe the output of `az` into `tr -d '\r'`. Another is to use `${w%$'\r'}` to strip it at usage time. – Charles Duffy Jan 06 '22 at 02:33
  • I am right there with you. Did some googling and found the solution. Code is updated above. Thanks so much for your help. – egrok Jan 06 '22 at 02:37

0 Answers0