LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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

View Poll Results: How clear was this question?
Clear 0 0%
Unclear 1 100.00%
Voters: 1. You may not vote on this poll

Reply
 
LinkBack Search this Thread
Old 02-23-2009, 11:32 AM   #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


In a Linux bash script, how would I store each line of output from a command into individual elements in an array? Thanks in advance...
 
Old 02-23-2009, 12:59 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
My answer to your (fairly pointless, IMO ) poll is "could be better". I believe I understand basically what you want, but it needs more detail to make it really clear. What kind of "command" output are you talking about? Is it something simple like "find", or is it the output of a complex scripting function? Explaining exactly what you're trying to do is the most direct way to get help.

In any case, whether the input is space-separated or line-separated shouldn't matter generally, because the IFS defaults to both. I'm not an expert on arrays, but if, for example, you want to input the files in the current directory, you could do something like this:

Code:
find .
./afile.txt
./bfile.txt
./cfile.txt

testarray=( $(find .) )
echo ${testarry[@]}
./afile.txt ./bfile.txt ./cfile.txt
Another way could be to use a loop. Assuming $(command) gives your output:
Code:
N=0
for i in $(command) ; do
   
      testarray[$N]="$i"
      echo "$N = $i"     #to confirm the entry
       
  let "N= $N + 1"
done
 
Old 02-23-2009, 01:35 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,512
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
Hello Steven
Code:
#! /bin/bash

i=1
ls | while read line
do
    array[ $i ]="$line"
    (( i++ ))
done
Best

Charles
 
Old 02-23-2009, 04:23 PM   #4
steven.c.banks
Member
 
Registered: Dec 2007
Location: Virginia
Distribution: RHEL
Posts: 44
Blog Entries: 1

Original Poster
Rep: Reputation: 15
Sorry - I was not sure what the poll was - please disregard. And thanks for the replies on my post - I incorporated in my script with no problems.
 
Old 12-05-2010, 11:29 AM   #5
vbekker
Member
 
Registered: Aug 2010
Location: Brooklyn, NY
Distribution: CentOS
Posts: 38

Rep: Reputation: 0
How about storing user input into an array?
Code:
echo "Please enter some info (separated by spaces for example)
read var
how do i tell bash to store the values in var into an array? is there a way to set an IFS variable and automatically store into another array varriable or do i need to parse the variable and place the values into an array manually?
Thank YOu
 
Old 12-05-2010, 02:08 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,512
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
vbekker, best start a new thread for your question
 
  


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 On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are 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
Bash store last line from displayed text output in a variable carl0ski Programming 1 01-16-2007 03:38 AM
Multi-line return from grep into an array? rose_bud4201 Programming 2 06-14-2005 04:11 PM
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 10:38 PM.

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