Assign dscl output to variable as an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign dscl output to variable as an array
# 1  
Old 11-25-2009
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
# 2  
Old 11-25-2009
use the shell command expansion with backticks
Code:
output=`dscl -u diradmin -P password /LDAPv3/127.0.0.1 -list /Users UniqueID`

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

NO SPACES around = !!
# 3  
Old 11-25-2009
Frans,

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

Many thanks
# 4  
Old 11-26-2009
If seperated by spaces, maybe you can assign directly to an array variable like
Code:
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

# 5  
Old 11-26-2009
Merci Frans!

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

Happy Thanksgiving Smilie
Nous avon 'French Fries' avec la turkey aujourd hui....
# 6  
Old 11-26-2009
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 ! Smilie
# 7  
Old 11-26-2009
Quote:
Originally Posted by frans
Yeah !
Apologize for my poor english !
Did you know that the turkey is named &quot;dinde&quot; in french ?
It's because they called them &quot;poule d'Inde&quot; (literraly &quot;indian chicken&quotSmilie.

But there's no Thanksgiving in France and the turkey is for Christmas.
Haapy Thanksgiving to You ! Smilie
Did you know the founding fathers dined on Turkey brought with them from farms in Norfolk England, not America? They took them with them........ Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assign output to dynamic variable

Hi Folks, I am trying to assign a value from the command to a dynamic variable. But I am not getting the desired output.. I am sure something is wrong so i need experts advise. There will be multiple files like /var/tmp/server_1, /var/tmp/server_2, /var/tmp/server_3, having different server... (6 Replies)
Discussion started by: ganga.dharan
6 Replies

2. Shell Programming and Scripting

How can we assign value to an array variable from an external file?

is it possible to assign value to an array variable from an external file?? if yes then how?? I am using below code but its not working. #!bin/bash myarray < file_name echo ${mayarray} (6 Replies)
Discussion started by: mukulverma2408
6 Replies

3. Shell Programming and Scripting

awk assign output of array to specific field-number

With this script i want to print the output to a specific field-number . Can anybody help? awk 'NR=FNR{split(FILENAME,fn,"_");nr=$2;f = $1} END{for (i=1;i<=f;i++) print i,$fn=nr}' input_5.csv input_6.csvinput_5.csv 4 135 5 185 6 85 11 30input_6.csv 1 90 3 58 4 135 7 60 8 55 10... (1 Reply)
Discussion started by: sdf
1 Replies

4. Shell Programming and Scripting

how to assign file names to array variable?

I wish to assign file names with particular extention to array variables. For example if there are 5 files with .dat extention in /home/sam then i have to assign these 5 files to an array. plz help me how to accomplish this. Thanks in advance. (4 Replies)
Discussion started by: siteregsam
4 Replies

5. Shell Programming and Scripting

How to assign an array element to a variable

Hi every one.. I'm new to shell scripting... I would like to assign a single array element to a variable... Is it possible to do it.... Could any body help me.... (3 Replies)
Discussion started by: kaushik_87
3 Replies

6. Shell Programming and Scripting

assign value to array variable

Hi, I have a piece of code as follows: i=0 while read LINE do var = "$LINE" i=$((i+1)) echo "${var}" done < file I want to assign value to the array var. However, when i execute the script i get a error. Please can you help me know what i am missing. I ultimately want to... (2 Replies)
Discussion started by: sunrexstar
2 Replies

7. Shell Programming and Scripting

How to assign a variable to an array

I want to ask the user to enter an X amount of file names. I want to put those names into an array and then loop back through them to verify they are in the directory. 1st- How would I assign the value to an array and what is the correct syntax. 2nd- how would i reference that array after I... (3 Replies)
Discussion started by: tvb2727
3 Replies

8. Shell Programming and Scripting

how to assign the output of the interective script to the variable

Hi, I work in ksh88. I have an interective script which prompts the user for the input and returns numeric value depending on the input provided. I need to call this script inside another script and then assign the resulting output the the variable. The call like that A=`my script` obviously... (6 Replies)
Discussion started by: aoussenko
6 Replies

9. Shell Programming and Scripting

hot to assign output to a variable

I want to assign a comment to a veriable for example my program head -1 myfile I want to assıgn output to a variable (1 Reply)
Discussion started by: walnut
1 Replies

10. UNIX for Dummies Questions & Answers

how to assign an output to a variable

Hi, I am giving a grep command, and i am getting the output. i want to store it in a variable for eg a = grep '12345' /dir/1/2/log.txt ( the output is number) b= grep 'basic' /dir/1/2/log1.txt (in this case the output is character) so how to assign the output of grep to a variable ... (1 Reply)
Discussion started by: vasikaran
1 Replies
Login or Register to Ask a Question