Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-17-2009, 07:03 AM   #1
LQ Newbie
Registered: Aug 2008
Posts: 7

Rep: Reputation: 0
Bash Arrays

Hi All.

Im currently attempting to write a script that takes the output of a command and put it into an array for me to 'play with' each item.

The problem I have is that it's currently treating the output as one item in the array as opposed to the nine items there actually is.

I run the command:

running=`su - zimbra -c "zmcontrol status"|grep Running| awk '{print $1}'|tr "\n" " "`
echo ${running[0]}
The command (starting from su -) returns a list of services. I attempt to format this into a space delmited list and pass this into a variable as an array.

However the entire list is becoming just one item in the array.

ie. the list is:
antispam antivirus ldap logger mailbox mta snmp spell stats

and echo ${running[0]} returns:
antispam antivirus ldap logger mailbox mta snmp spell stats

Any ideas what I'm doing wrong here?
Old 02-17-2009, 07:15 AM   #2
LQ Newbie
Registered: Aug 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Sorted, thanks.

running=($(su - zimbra -c "zmcontrol status"| grep Running | awk '{ print $1 }'))
echo "Number of elements : ${arrayLen}"
for (( i=0; i<${arrayLen}; i++ ));
  echo "${running[$i]}"
Old 02-17-2009, 02:39 PM   #3
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46

a little hint: You can do the grep work within the awk and save time and resources - and you don't need to redefine IFS:
running=($(su - zimbra -c "zmcontrol status"| awk ' /Running/ { printf "%s ",$1; }'))


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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Bash Arrays LXer Syndicated Linux News 0 06-20-2008 07:20 PM
[bash] How do I nest for arrays? blckleprd Programming 3 06-05-2008 11:49 PM
Bash - How can you list 2 arrays in columns zak111 Programming 12 07-23-2007 12:27 PM
bash arrays question introuble Programming 1 05-20-2006 04:07 AM
bash for statement with 2 arrays? Noerr Linux - General 10 05-27-2002 01:58 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:19 AM.

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