1

I am attempting to get a list of running VirtualBox VMs (the UUIDs) and put them into an array. The command below produces the output below:

$ VBoxManage list runningvms | awk -F '[{}]' '{print $(NF-1)}' 

f93c17ca-ab1b-4ba2-95e5-a1b0c8d70d2a
46b285c3-cabd-4fbb-92fe-c7940e0c6a3f
83f4789a-b55b-4a50-a52f-dbd929bdfe12
4d1589ba-9153-489a-947a-df3cf4f81c69

I would like to take those UUIDs and put them into an array (possibly even an associative array for later use, but a simple array for now is sufficient)

If I do the following:

array1="( $(VBoxManage list runningvms | awk -F '[{}]' '{print $(NF-1)}') )"

The commands

array1_len=${#array1[@]}
echo $array1_len

Outputs "1" as in there's only 1 element. If I print out the elements:

    echo ${array1[*]}

I get a single line of all the UUIDs

( f93c17ca-ab1b-4ba2-95e5-a1b0c8d70d2a 46b285c3-cabd-4fbb-92fe-c7940e0c6a3f 83f4789a-b55b-4a50-a52f-dbd929bdfe12 4d1589ba-9153-489a-947a-df3cf4f81c69 )

I did some research (Bash Guide/Arrays on how to tackle this and found this with command substitution and redirection, but it produces an empty array

while  read -r -d '\0'; do
        array2+=("$REPLY")
    done < <(VBoxManage list runningvms | awk -F '[{}]' '{print $(NF-1)}')

I'm obviously missing something. I've looked at several simiar questions on this site such as:

Unfortunately, none have helped. I would apprecaite any assistance in figuring out how to take the output and assign it to an array.

I am running this on macOS 10.11.6 (El Captain) and BASH version 3.2.57

4
  • 2
    Remove the double-quotes: array1=( $(VBoxManage list runningvms | awk -F '[{}]' '{print $(NF-1)}') ), (The entries will be subject to word-splitting and pathname expansion but that doesn't appear to be a problem here.) Commented Dec 4, 2017 at 21:44
  • 1
    You were also very close with your while. If you change it to just: while read -r; do, it will work too. Commented Dec 4, 2017 at 22:58
  • @John1024 - the double quotes did it. Out of curiosity, (even though it's moot), how would I approach this if there were spaces that led to word splitting since the quotes made the whole output a single long string? Commented Dec 5, 2017 at 11:35
  • @PesaThe - Thanks! I thought I had to account for a null char based on what I read/understood). Commented Dec 5, 2017 at 11:37

1 Answer 1

1

Since you're on a Mac:

brew install bash

Then with this bash as your shell, pipe the output to:

readarray -t array1

Of the -t option, the man page says:

-t     Remove a trailing delim (default newline) from each line read.

If the bash4 solution is admissible, then the advice given e.g. by gniourf_gniourf at reading-output-of-command-into-array-in-bash is still sound.

Sign up to request clarification or add additional context in comments.

1 Comment

It's a good solutions, however, I can't use it. I don't like writing scripts that are dependent on specific environments. Many times, I share the script or I cannot install a port/brew making my solution unworkable. I try to keep everything as native as possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.