LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 12-16-2009, 03:54 PM   #1
steven.c.banks
Member
 
Registered: Dec 2007
Location: Virginia
Distribution: RHEL
Posts: 44
Blog Entries: 1

Rep: Reputation: 15
Store multi-line output into an array in a Linux bash script


I am using this command to load the output of a command into an array and then examine the results:

ldapoutputarray=( $(su - dcmbox -c "$dcdldapstr") )
echo "0=${ldapoutputarray[0]}"
echo "1=${ldapoutputarray[1]}"
echo "2=${ldapoutputarray[2]}"
echo "3=${ldapoutputarray[3]}"


What I get is:
0=#
1=extended
2=LDIF
3=#


What I want is:
0=# extended LDIF
1=#
2=# LDAPv3
3=#

i.e. it is using the space between words as a delimiter when loading the array. I want it to load the array by line, not word. Would appreciate any suggestions.
 
Old 12-16-2009, 04:55 PM   #2
sarum1990
Member
 
Registered: Dec 2008
Distribution: Gentoo, Debian
Posts: 31

Rep: Reputation: 21
This has to do with the variable IFS in bash. by default:
Code:
IFS=$' \t\n'
This means essentially that all tabs and newlines are translated to spaces when variables are assigned that value because space is the first character in IFS. Also it means that spaces, tabs, and newlines represent separators. (IFS I believe stands for internal field separator)

so the following script should work:

Code:
IFS=$'\n'
dapoutputarray=( $(su - dcmbox -c "$dcdldapstr") )
echo "0=${ldapoutputarray[0]}"
echo "1=${ldapoutputarray[1]}"
echo "2=${ldapoutputarray[2]}"
echo "3=${ldapoutputarray[3]}"
unset IFS
So only newlines are considered to be array field separators.

hope this helps
 
1 members found this post helpful.
Old 12-17-2009, 09:10 AM   #3
steven.c.banks
Member
 
Registered: Dec 2007
Location: Virginia
Distribution: RHEL
Posts: 44
Blog Entries: 1

Original Poster
Rep: Reputation: 15
Smile

That worked perfectly - thanks!!
 
  


Reply

Tags
array


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
Store multi-line output into an array in a Linux bash script steven.c.banks Linux - General 5 12-05-2010 02:08 PM
Bash store last line from displayed text output in a variable carl0ski Programming 1 01-16-2007 03:38 AM
Bash script text line into an array toolshed Programming 1 06-13-2005 05:49 PM
store output of cmd in array? h/w Programming 6 10-09-2003 08:46 PM


All times are GMT -5. The time now is 12:07 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration