Assign dscl output to variable as an array

Greetings folks,

I am trying to assign the output of a dscl command (contains name<spaces>id) to a variable as an array. Currently I am piping the output into a tmp file, then reading the tmp file into an array, then parsing the array. I would like to bypass creating the tmp file portion of the code.

currently I am doing this:

----pseudo code follows----

dscl -u diradmin -P password /LDAPv3/127.0.0.1 -list /Users UniqueID > nameid.tmp

read file into array

process array values

----end pseudo code----

Is there a way to simply assign the dscl output directly to a variable as an array, e.g.

output = dscl -u diradmin -P password /LDAPv3/127.0.0.1 -list /Users UniqueID

Many thanks for your time

use the shell command expansion with backticks

output=`dscl -u diradmin -P password /LDAPv3/127.0.0.1 -list /Users UniqueID`

or $()

output=$(dscl -u diradmin -P password /LDAPv3/127.0.0.1 -list /Users UniqueID)

NO SPACES around = !!

Frans,

It works. I got the result as a single string that I can now parse through and insert into an array.

Many thanks

If seperated by spaces, maybe you can assign directly to an array variable like

output=( $(dscl -u diradmin -P password /LDAPv3/127.0.0.1 -list /Users UniqueID) )
# ${#output[@]} Gives the number of elements in the array
# ${output[$i]} Will return the element i of the array
# Attention : the first element has the index 0

Merci Frans!

That is much easier than what I was trying and works beautifully.

Happy Thanksgiving :slight_smile:
Nous avon 'French Fries' avec la turkey aujourd hui....

Yeah !
Apologize for my poor english !
Did you know that the turkey is named "dinde" in french ?
It's because they called them "poule d'Inde" (literraly "indian chicken").

But there's no Thanksgiving in France and the turkey is for Christmas.
Haapy Thanksgiving to You ! :slight_smile:

Did you know the founding fathers dined on Turkey brought with them from farms in Norfolk England, not America? They took them with them........ :slight_smile: