0

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?

4

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

  • 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 – iruvar Jun 22 '13 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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