tags:

views:

35

answers:

2

I currently have something of the form

abc=`find ~/$folder .. etc

I'd like to know how to get the number of items in abc:

abc_length = ?

Thanks

+3  A: 

I'm not sure what you mean by "items", so I will assume "directory entries". Assuming you have no files with newlines in their name, echo "$abc" | wc -l will do the trick.

If you want to count the number of characters, use wc -c instead.

Chris Jester-Young
The echo "$abc" | wc - l is working fine. But how to get that value to a variable?
devoured elysium
just `abc_length=$(find ... | wc -l)` would do what you want.
livibetter
I currently have: results=`find ~/$folder -name "$file@*" -type f`. How can I have a results_count that instead of calling again find, will only use the data that is already in the "results" variable? Thanks
devoured elysium
You will always have to re-run `find` if you want an updated count.
livibetter
`results=$(find ...); num_entries=$(echo "$results" | wc -l)`
Chris Jester-Young
@livibetter: That's not what the OP was asking. :-)
Chris Jester-Young
@Chris, I was kind of assuming OP only wants the count only, so I mis-read OP's comment.
livibetter
+3  A: 

Use array:

abc=( $(find ...) )
abc_length=${#abc[@]}

Retrieve n-th result, say 10th:

echo ${abc[9]}

Or list all:

for dir_name in "${abc[@]}"; do
  echo $dir_name
done

Update: asker doesn't seem to want to process in script, so:

abc_length=$(find ... | wc -l)

You may want to reduce the number by 1 because the first result is ~/$folder:

abc_length=$(( $(find ... | wc -l) - 1 ))
livibetter
When trying to loop through abc, it will only list the first result! abc=( $(find ~/$folder -name "$file@*" -type f) ), and the loop : for j in "$abc". Is there something wrong?
devoured elysium
Use `for j in ${abc[@]}`.
livibetter
Don't forget the quotes: `for j in "${abc[@]}"`.
Chris Jester-Young