Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Here's the bash script I am working on:

i/p: ls *.py
o/p: List of output files with .py extension

1.How do I know the number "n" of the .py files?
2.Then pump files one by one into the program to do further processing?

share|improve this question

1 Answer 1

I'd use an array:

# get the files
files=(*.py)

# list the files
printf "%s\n" "${files[@]}"

# count the files
n=${#files[@]}

# iterate over the files
for file in "${files[@]}"; do
    someCommand "$file"
done
# or, if you want the index for some reason
for ((i=0; i < n; i++)); do
    echo "$i: ${files[i]}"
done

bash arrays tutorial here

share|improve this answer
2  
+1, this is the way to go. You may want to be explicit on the use of shopt -s nullglob to cover the case where no files exist –  1_CR Jun 22 '13 at 20:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.