![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| creating a dynamic array | trichyselva | Shell Programming and Scripting | 1 | 07-10-2008 06:13 AM |
| creating array variable | scriptingmani | Shell Programming and Scripting | 2 | 06-28-2007 07:25 AM |
| How can i get directory listing? | haisubbu | UNIX for Dummies Questions & Answers | 2 | 08-25-2006 06:03 AM |
| creating a dynamic array in ksh | gundu | Shell Programming and Scripting | 3 | 03-09-2005 11:26 AM |
| Recursive directory listing without listing files | psingh | UNIX for Dummies Questions & Answers | 4 | 05-10-2002 07:52 AM |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Creating a string array from a directory listing
Hi all,
I'd like to create a string array from a long directory listing, extracting only files last modified on a specific date (e.g. Aug 08). I tried the following: aug8=`ls -ltr |grep 'Aug 08'` The result was an array (I think) but all of the output from the listing went to the first element of the array, so that: echo ${aug8[0]} displayed everything and: echo ${aug8[1]} displayed nothing. Any suggestions as to how I can get each filename in the directory listing to be a single element in the "a8" array? FYI, I'm in Korn Shell. THANKS! |
| Forum Sponsor | ||
|
|
|
|||
|
The reason why your command failed was that the shell was not aware that "aug8" should be an array. That you found the output of "ls" in aug8[0] was just because for every variable this is the case:
Code:
willy="x" print - $willy[0] # will yield "x" set -A aug8 "$(ls -ltr | sed -n '/Aug 08/ s/ */ /gp' | cut -d' ' -f12)" There are two problems with this approach anyways: first, while evaluating the commandline the subshell will be executed and the command be replaced by its output. UNIX-commandlines have a maximum length (4096 characters) which could be exceeded if there are enough files with long enough names. Secondly, ksh-arrays have a maximum number of elements, which is 1024. This might be not enough if the directory contains enough matching files (although presumably the other limit would be hit first). The first limitation could be circumvented by using a loop to assign the array elements: Code:
(( iCnt=0 ))
ls -ltr | sed -n '/Aug 08/ s/ */ /gp' | cut -d' ' -f12 | while read filename ; do
aug8[$iCnt]="$filename"
(( iCnt += 1 ))
done
Code:
ls -ltr | sed -n '/Aug 08/ s/ */ /gp' | cut -d' ' -f12 | while read filename ; do
do_something "$filename"
done
bakunin |
|||
| Google UNIX.COM |