Bash - Loading a command's output line by line into an array


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash - Loading a command's output line by line into an array
# 1  
Bash - Loading a command's output line by line into an array

I have been trying this a lot of different ways and haven't found too much online. Here's what I've got so far:

Code:
j=0 
declare -a first 
zero=(`cat $tmpfile`)          
      
     for i in "${zero[@]}"         
     do                 
           command $i >> "${first[$j]}"                 
           (( j = j + 1 ))                 
           sleep 2                  

     done < $tmpfile

Basically $i would just be a parameter for the command taken from a temp file. The command would output many single column lines of text that I would like to store line by line in the array elements of first[*] (or $j). While I've found tons on storing to a file and then to an array, I want to start skipping the temp files and just putting output in the arrays. Is this possible in Bash?
# 2  
Code:
first+=( "$(command "$i")" )

For bash4, also read the man page for the mapfile builtin.
This User Gave Thanks to cfajohnson For This Post:
# 3  
Thank you Cfajohnson! That does work, however it no longer outputs line by line from my program. Is there a trick to creating a new line after each element?

Quote:
For bash4, also read the man page for the mapfile builtin.
I have Bash version 4.1.5 and there seems to be no man page for that on my system. I will try to research this though.
# 4  
Hi.

You can find a bash4 man page here
This User Gave Thanks to Scott For This Post:
# 5  
Oh! I thought mapfile had its own man page. I see now.

Thank you Scottn

I got the problem with newlines too. Didn't think it would be that simple:

Code:
a=0
length=${#first[@]}
for (( b = 0; b < $length; b++ ));
do
        echo -e "${first[$a]}\n" 
done

Thanks again!

Last edited by Azrael; 09-20-2011 at 06:17 AM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #569
Difficulty: Medium
foreach loops are considered syntactic sugar.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

How to read the output of a command line by line and pass it as a variable?

Hi, I have some 2000 names in a table like below. Java Oracle/SQL ANSI SQL SQL,DWH,DB DB&Java And by using for loop in my code i am able to get a single word but if there is any special character or space then it is considering as a next line. I have to execute the below queries in... (10 Replies)
Discussion started by: Samah
10 Replies

3. UNIX for Beginners Questions & Answers

Limiting Bash array single line output

#!/bin/bash PH=(AD QD QC 5H 6C 8C 7D JH 3H 3S) echo ${PH} In the above array, how can I print to screen just the first 8 elements of ${PH} and have the last 2 elements print just below the first line starting underneath AD? I need to do this in order to save terminal window spacing... (5 Replies)
Discussion started by: cogiz
5 Replies

4. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

5. Shell Programming and Scripting

Need to have output of AWK array in one line

I have this code echo $logfile | awk ' {arr++; next} END { for (i in arr) {print i} }' that gives me this output result1 result2 result3 I try to figure out how to get it like this result1 result2 result3 (4 Replies)
Discussion started by: Jotne
4 Replies

6. Shell Programming and Scripting

help with reading command line into array

set -A title set -A author count=1 index=0 while do read -A ${title}?"Booktitle: " read -A ${author}?"Author(s): " (( count = count + 1 )) (( index = index + 1 )) done Hi... (1 Reply)
Discussion started by: bjhum33
1 Replies

7. Shell Programming and Scripting

parse output line using bash

hi, i have the followiing scenario where by i am parsing teh following output using cut -d like so #!/bin/bash output="ABCTable| ------------------| | ------------------| code | name | amount |" col1= $output | cut -d'|' -f5 col2= $output | cut -d'|'... (1 Reply)
Discussion started by: nano2
1 Replies

8. Shell Programming and Scripting

reading ps command's output line by line

hi; as a pseudo; while read psLine do myFunc $psLine done < ps i don't want to redirect ps command's to a file. in fact, my problem is "how can i read stdout line by line in bash, sed, awk or any?" thanks, (5 Replies)
Discussion started by: s. murat
5 Replies

9. UNIX for Dummies Questions & Answers

array as command line argument !!!!

hello, can any help me how to can pass array as command line argument in korn shell. also how to read a array from command line. thanks spandu (2 Replies)
Discussion started by: spandu
2 Replies

Featured Tech Videos