PDA

View Full Version : Bash array only uses first element



wesg
August 14th, 2009, 11:56 PM
I'm writing a bash script that uses an array to operate on files scattered throughout a folder. I've put all the files in the array, but after going through the for loop, only the first element is ever used. I tried multiple configurations with quotation marks, but none work.

Any suggestions?


folders=$(find . -name "*.avi" | sort)
My Array


for x in "${folders[@]}"; do
My loop

lswb
August 15th, 2009, 12:16 AM
wouldn't just

for x in "${folders}"; do

work?

wesg
August 15th, 2009, 12:21 AM
That works in that the loop works, but still doesn't use all elements.

kaibob
August 15th, 2009, 12:27 AM
Try the following, which works for me. Perhaps something is wrong elsewhere in the script.


#!/bin/bash
folders=$(find . -name "*.avi" | sort)
for x in "${folders}" ; do
echo "$x"
done

ghostdog74
August 15th, 2009, 12:45 AM
folders=$(find . -name "*.avi" | sort)

the above puts the results of find and sort into an entire string, not array. Am i missing something here?
@OP, unless you are storing your avi files for further processing in later part of your script, otherwise, no need to use arrays. just process them as you find them


find ... -name "*.avi" ... | while read FILE
do
# do something with $FILE variable
done

geirha
August 15th, 2009, 11:05 AM
And if you still want them in an array


files=()
while IFS= read -d '' -r file; do
files+=("$file")
done < <(find . -name "*.avi" -print0 | sort -z)

echo "All files:" "${files[@]}"
echo "Second file: ${files[1]}"
echo "First file: ${files[0]}"
echo "First file: $files" # Yes, this is equivalent to the previous parameter expansion


It's important that you put ""-quotes around ${files[@]}. If not it will split on spaces in filenames.

ghostdog74
August 15th, 2009, 02:30 PM
its also possible to read into array using -a option, eg


read -a array <<< $(ls -1)