LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-17-2009, 06:03 AM   #1
Simon256
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:

Code:
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, 06:15 AM   #2
Simon256
LQ Newbie
 
Registered: Aug 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Sorted, thanks.

Code:
#!/bin/bash
OIFS=${IFS}
IFS=$'\n'
running=($(su - zimbra -c "zmcontrol status"| grep Running | awk '{ print $1 }'))
arrayLen=${#running[@]}
echo "Number of elements : ${arrayLen}"
for (( i=0; i<${arrayLen}; i++ ));
do
  echo "${running[$i]}"
done
 
Old 02-17-2009, 01:39 PM   #3
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

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:
Code:
running=($(su - zimbra -c "zmcontrol status"| awk ' /Running/ { printf "%s ",$1; }'))
Jan
 
  


Reply



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 06:20 PM
[bash] How do I nest for arrays? blckleprd Programming 3 06-05-2008 10:49 PM
Bash - How can you list 2 arrays in columns zak111 Programming 12 07-23-2007 11:27 AM
bash arrays question introuble Programming 1 05-20-2006 03:07 AM
bash for statement with 2 arrays? Noerr Linux - General 10 05-27-2002 12:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:34 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration