1

Disclaimer: I am brand new to bash I'm building a dialog box and trying to get some Docker commands to populate results.

I am using this to return a result, which works for the most part.

result=($(sudo docker exec -ti $appid timeout 3 /bin/bash -c "echo > /dev/tcp/fake.url.com/80 >& /dev/null" && echo "Test is successful" || echo "Not OK"))

The problem is that $result only contains the first word before a space. So in the above example it returns Test If I put echo "fake.url.com is successful" it returns "fake.url.com" Which means the dots don't break it just spaces

I have tried double and triple quotes.

New contributor
James Durand is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Answer 1

2

The first parentheses pair, i.e. the ( immediately before the $(sudo...) and its matching ) at the end, are causing $result to be populated as an array.

To confirm this, try typing declare -p result

I expect you'll see $result as an array with either 3 elements ("Test", "is", "successful") or 2 elements ("Not", "OK"), i.e.:

declare -a result=([0]="Test" [1]="is" [2]="successful")

or

declare -a result=([0]="Not" [1]="OK")

In that case, the solution is easy - just remove the first pair of parentheses.

e.g.

result=$(sudo docker exec -ti $appid timeout 3 \
           /bin/bash -c "echo > /dev/tcp/fake.url.com/80 >& /dev/null" &&
             echo "Test is successful" ||
             echo "Not OK")

Worth noting: when you refer to an array variable without an index (e.g. just $result instead of ${result[n]} or ${result[@]} etc), bash returns just the first (zeroth) element of the array, i.e. ${result[0]}.

That's why it seems that $result only contains the first word of the captured output.


BTW, for a possibly clearer example of what is happening here, consider the different results from these two commands:

$ result=$(echo Test is successful)
$ declare -p result
declare -- result="Test is successful"

and

$ result=($(echo Test is successful))
$ declare -p result
declare -a result=([0]="Test" [1]="is" [2]="successful")

In the first example, $result is being created as a scalar variable containing a single string value. In the second, it's being created as an array variable containing three whitespace-separated string values.

4
  • Thanks so much. I can't believe the penny didn't drop. I spent a day trying to get my array of Docker image Names into a select statement. Same issue but reverse, the whole array was going into the first select statement. # Example 1: Varying array size my_items=($(docker ps -a --filter name=IVS1- --filter name=EVS1- --format {{.Names}})) create_dialog_menu "${my_items[@]}" # Format the input array for dialog (tag, item name, tag, item name...) for item in "${list_items[@]}"; do dialog_options+=("$index" "$item") ((index++)) done Commented 2 days ago
  • sorry, that wasn't a question. Hard to say in 30 characters, but you resolved my issue Commented 2 days ago
  • 1
    @JamesDurand, If this answer solved your issue, please take a moment and accept it by clicking on the checkmark on the left. That is the best way to express your thanks on the Stack Exchange sites. Commented 2 days ago
  • Thanks terdon. (and cas) I was going to ask how to vote, missed that green tick. Commented yesterday

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.