LinuxQuestions.org
Help answer threads with 0 replies.
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 05-28-2008, 04:08 PM   #1
kasthana
LQ Newbie
 
Registered: May 2008
Posts: 7

Rep: Reputation: 0
Help!! Shell script to get output of ls -l command into an array


Hi

I have a requirement to get the output of ls-l command into an array.

Suppose there are three files in directory and the output is as :

-rw-r----- 1 kas kas 53 May 28 20:44 t1.dat
-rw-r----- 1 kas kas 53 May 28 20:44 t2.dat
-rw-r----- 1 kas kas 124 May 28 20:44 t3.dat

I want each line of output to be stored as seperate element of an array; as

arr[0] -rw-r----- 1 kas kas 53 May 28 20:44 t1.dat
arr[1] -rw-r----- 1 kas kas 53 May 28 20:44 t2.dat
arr[2] -rw-r----- 1 kas kas 124 May 28 20:44 t3.dat

Thanks
 
Old 05-28-2008, 04:27 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Code:
let cnt=0
ls -l | \
while read line
do
    echo $cnt
	arr[$cnt]="$line"
	cnt=$(( $cnt + 1 ))
done
echo "$cnt lines read"
 
Old 05-28-2008, 05:02 PM   #3
kasthana
LQ Newbie
 
Registered: May 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Question

Thanks for response.
I tried to execute the following code and the output is as:
0
1
2
3
0 lines read

It seems array is not filled up and also cnt value becomes zero after done statement.

Could you please help me with the reason.

Thanks
 
Old 05-28-2008, 06:01 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
The pipe opens a subprocess, so any info created there is not avail to the parent process
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

This wouldn't be homework/coursework by any chance?
http://catb.org/~esr/faqs/smart-questions.html
 
Old 05-28-2008, 07:03 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Here is what I would do:
Code:
IFS='
' read -d '' -a arr < <(ls -l)
Since we are using bash after all, we can use the <() construct.

Last edited by osor; 06-01-2008 at 10:20 PM. Reason: Donít know what I was thinking before. Definiately not so complicated.
 
Old 05-29-2008, 10:59 AM   #6
kasthana
LQ Newbie
 
Registered: May 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Thumbs up

Thx all, I am able to solve the problem
 
Old 05-29-2008, 01:31 PM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Btw, if you want to use pipes, you can do so as well, as long as you use your array inside the subshell. For example,
Code:
ls -l | (

    IFS='
'   read -d '' -a arr

# Do all accessing and processing of arr here

) # arr goes out of scope here
 
Old 06-01-2008, 10:21 PM   #8
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
There was a much simpler way (and I have edited my post accordingly).
 
Old 06-01-2008, 11:37 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
what do you actually need to do to those ls -l output lines?? why not do your line processing "on the spot"
Code:
ls -l | while read blah
do
 # process lines.
done
 
1 members found this post helpful.
  


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
bash shell script split array robertngo Programming 13 06-19-2011 11:01 PM
shell command without output ricardo85xeii Programming 2 10-17-2007 11:58 AM
help with shell script - fill an array with values from a file delmoras Linux - General 1 07-17-2006 11:19 AM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 09:45 PM
shell script array problem rche3252 Programming 1 10-08-2003 11:43 PM


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