LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 04-29-2013, 01:44 PM   #1
awreneau
LQ Newbie
 
Registered: Aug 2003
Location: GA
Distribution: Ubuntu
Posts: 29

Rep: Reputation: 15
print values from array (Is there a better way?)


All, I have created 4 arrays from a server and I'm currently printing the 1st value in each array (code follows). I'd like to itterate thru each array and print the first value thru the last. Is there a loop that I can do this with rather than something like the following? Or if there is a better way to collect the following data with one connection rather than multiple connections.

Code to collect the data for the array:
Code:
owner=$(ssh ${sgnode} "ls -Ald ${MOUNTS[*]}" | awk '{ print $3 ":" $4 }')
        #declare array
        declare -a OWNER
        OWNER=(`echo ${owner}`)
How I'm currently printing the values:

Code:
echo -e "${BLOCKDEV[1]}\t ${MOUNTS[1]}\t ${OWNER[1]}\t ${FSSIZE[1]}"
 
Old 04-29-2013, 02:34 PM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,516

Rep: Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896
Well I see no reason to create the array in 2 steps and a simple for loop can loop through each element:
Code:
OWNER=($(ssh ${sgnode} "ls -Ald ${MOUNTS[*]}" | awk '{ print $3 ":" $4 }'))
See here for details on getting data from arrays
 
Old 04-29-2013, 08:27 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,455

Rep: Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843
Don't parse ls.

Code:
OWNER=($(ssh "${sgnode}" "stat -c '%U:%G' ${MOUNTS[*]}"))
 
  


Reply


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
[SOLVED] reference bash array values, using a variable with a value of the array name gusthecat Programming 5 03-07-2012 03:41 PM
[SOLVED] addition using array values into a variable not possible? olof_ Programming 4 11-21-2011 04:00 PM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM
extracting values from an array bahadur Programming 2 08-14-2006 01:07 AM
How to return values into an array using awk Helene Programming 1 05-01-2004 10:05 PM


All times are GMT -5. The time now is 09:39 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