Results of "find" to an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Results of "find" to an array
# 1  
Old 12-05-2008
Results of "find" to an array

I am looking to search a directory for a folder or file and when it finds any hits I want it to store those hits in an array so I can work with those hits later on.

I have been trying to do something like this but it isn't putting results into an array like I would like, is it syntax or is this not possible?

Code:
TARGET="MyRoot"
START_PATH="/Users/rtipton/Desktop/"

PATH_COUNT=0

find "$START_PATH" -type d \( -iname "$TARGET" \) -exec PATH_RESULTS[PATH_COUNT=$PATH_COUNT+1]=`echo {}` \;

Any ideas?
Rob
# 2  
Old 12-05-2008
Why don't you use a loop?

Code:
TARGET="MyRoot"
START_PATH="/Users/rtipton/Desktop/"

PATH_COUNT=0

find "$START_PATH" -type d \( -iname "$TARGET" \) \; | 
while read result
do
  PATH_RESULTS[$PATH_COUNT]="$result"
  echo "${PATH_RESULTS[$PATH_COUNT]}"
  # more code
  #
  let PATH_COUNT=PATH_COUNT+1
done

Regards
# 3  
Old 12-05-2008
Quote:
Originally Posted by Franklin52
Why don't you use a loop?

Code:
TARGET="MyRoot"
START_PATH="/Users/rtipton/Desktop/"

PATH_COUNT=0

find "$START_PATH" -type d \( -iname "$TARGET" \) \; | 
while read result
do
  PATH_RESULTS[$PATH_COUNT]="$result"
  echo "${PATH_RESULTS[$PATH_COUNT]}"
  # more code
  #
  let PATH_COUNT=PATH_COUNT+1
done

Regards
Ok I tried this out but once the find + loop is finished the array is lost and the values are gone, I need to be able to work with the data in the array later on and outside of the loop. Any way to do that? put the data into a global array?

Thanks for the help,
Rob
# 4  
Old 12-05-2008
If you want to use the array outside of the loop you must avoid to pipe the result. The pipe mechanism starts a subshell and parent shells don't inherit variables of a child shell.

Code:
TARGET="MyRoot"
START_PATH="/Users/rtipton/Desktop/"

PATH_COUNT=0

for result in "`find "$START_PATH" -type d \( -iname "$TARGET" \) \;`"
do
  PATH_RESULTS[$PATH_COUNT]="$result"
  echo "${PATH_RESULTS[$PATH_COUNT]}"
  # more code
  #
  let PATH_COUNT=PATH_COUNT+1
done

Regards
# 5  
Old 12-05-2008
Quote:
Originally Posted by Franklin52
If you want to use the array outside of the loop you must avoid to pipe the result. The pipe mechanism starts a subshell and parent shells don't inherit variables of a child shell.

Code:
TARGET="MyRoot"
START_PATH="/Users/rtipton/Desktop/"

PATH_COUNT=0

for result in "`find "$START_PATH" -type d \( -iname "$TARGET" \) \;`"
do
  PATH_RESULTS[$PATH_COUNT]="$result"
  echo "${PATH_RESULTS[$PATH_COUNT]}"
  # more code
  #
  let PATH_COUNT=PATH_COUNT+1
done

Regards
Hey thanks again, this worked to make the data global but now with this option, find provides all the paths into 1 variable instead of each path being it's own value in the array.

PATH_RESULTS ends up holding the value of all of the results rather than PATH_RESULTS[x] being 1 result each, any way to combine the pipe option which worked this way with the global variable result of this version?

Rob
# 6  
Old 12-05-2008
It looks like I'll be able to adapt to work within the piped loop without need for the data later in the script, thanks for your help!!

-Rob
# 7  
Old 12-05-2008
Try to remove the \; at the end of the find command.

Code:
for result in "`find "$START_PATH" -type d \( -iname "$TARGET" \)`"

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search file containing ps results for a match "my.cnf" and then for a second match . "ok:" and

I need to find two matches in the output from ps. I am searching with ps -ef |grep mysql for: my.cnf /bin/sh /usr/bin/mysqld_safe --defaults-file=/data/mysql/master/agis_core/etc/my.cnf after this match I want to search back and match the hostname which is x number of lines back, above the... (2 Replies)
Discussion started by: bash_in_my_head
2 Replies

2. Shell Programming and Scripting

find . -path "*_nobackup*" -prune -iname "*.PDF" \( ! -name "*_nobackup.*" \)

These three finds worked as expected: $ find . -iname "*.PDF" $ find . -iname "*.PDF" \( ! -name "*_nobackup.*" \) $ find . -path "*_nobackup*" -prune -iname "*.PDF" They all returned the match: ./folder/file.pdf :b: This find returned no matches: $ find . -path "*_nobackup*" -prune... (3 Replies)
Discussion started by: wolfv
3 Replies

3. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

4. Shell Programming and Scripting

Awk find in columns with "if then" statement and print results

I have a file1.txt file1.txt F-120009210","Felix","U-M-F-F-F-","white","yes","no","U-M-F-F-F-","Bristol","RI","true" F-120009213","Fluffy","U-F-","white","yes","no","M-F-","Warwick","RI","true" U-120009217","Lity","U-M-","grey","yes","yes","","Fall River","MA","true"... (4 Replies)
Discussion started by: charles33
4 Replies

5. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Programming

C: Initialize "const" array from the "heap"

Hello, I am working on solving an NP-Complete problem, so it is very important that operations and data with limited integer-argument ranges be computed using immutable look-up-tables contained entirely in CPU cache. Retrieval of the look-up-table data must never leave the CPU once initially... (6 Replies)
Discussion started by: HeavyJ
6 Replies

8. Shell Programming and Scripting

Error to "find" a matching array element in a directory

Hi, I have defined an array which holds a couple of elements which are nothing but files names. I want to find the files in a directory for the matching file name(array elements) with less than 1 day old. When I am trying to execute the code (as below), it gives an error. Your help in this... (1 Reply)
Discussion started by: mkbaral
1 Replies

9. Shell Programming and Scripting

"find command" to find the files in the current directories but not in the "subdir"

Dear friends, please tell me how to find the files which are existing in the current directory, but it sholud not search in the sub directories.. it is like this, current directory contains file1, file2, file3, dir1, dir2 and dir1 conatins file4, file5 and dir2 contains file6,... (9 Replies)
Discussion started by: swamymns
9 Replies

10. Shell Programming and Scripting

grep to find content in between curly braces, "{" and "},"

problem String ~~~~~~~~~~~~~~~~~~ icecream= { smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" } aend = {smart vc4 eatr kalu} output needed ~~~~~~~~~~~~~~~~~~ smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" smart vc4... (4 Replies)
Discussion started by: keshav_rk
4 Replies
Login or Register to Ask a Question