arraysbashdigvariable-substitution

How to nest bash command output into an array inline?


Note - this is not a duplicate! There are other questions covering converting bash output to a bash array - but none of them do it inline. The reason you need inline is because of until.

I'm trying to write an until command that parses the port from a dig command.

Here are the working commands on separate lines.

SERVER_DIG_RESULT=$(dig +noall +answer $SERVER_DNS_NAME SRV )
SERVER_STRING_ARRAY=($SERVER_DIG_RESULT)
SERVER_PORT=${SERVER_STRING_ARRAY[6]}
until nc -z $SERVER_DNS_NAME $SERVER_PORT

The problem with this is that the dns may not come online until after the first dig command is run, so you want the until command to run the dig over and over.

Here is my broken until command (where I try and put it on one line).

until nc -z $SERVER_DNS_NAME ${$(dig +noall +answer $SERVER_DNS_NAME SRV)[6]}

I seem to be having a problem with variable substitution. My question is: How to nest bash command output into an array inline?


Solution

  • The condition for the until statement isn't limited to a single command.

    until SERVER_STRING_ARRAY=( $(dig +noall +answer "$SERVER_DNS_NAME" SRV)
          SERVER_PORT=${SERVER_STRING_ARRAY[6]}
          nc -z "$SERVER_DNS_NAME" "$SERVER_PORT"; do
        ...
    done