0

This should be a relatively simple question. I am trying to pass multiple files through an executable program in a loop. I have tried multiple strategies (storing in an array, etc.) but the file name does not seem to be recognized as a variable. The error I am getting is: No such file or directory.

The outline of my latest attempt:

FILES=path/*/*

for f in $FILES
do
./function -input $f 
done

I am working in a bash shell.

  • 2
    So, what part of the script gives that error? Bash should show you a line number. Also, you speak of a function, but ./function looks more like an executable program. (function by itself would be shell syntax for defining a function) – ilkkachu Nov 30 '17 at 17:22
  • 1
    You should double quote the file path as it can contain whitespace ./function -input "$f". Try running the script with set -xv to see how the variables are really populated. – choroba Nov 30 '17 at 17:29
  • You should edit the question to include the necessary information that ikkachu asked about. It's not enough to say that "nothing seems to be working". – Anthony Geoghegan Nov 30 '17 at 17:57
3

Instead of using a plain variable to store a glob expansion sequence, recommend using a proper array and enable shell globbing option globstar to populate files in all sub-directories

shopt -s globstar nullglob
file_list=(path/**/*)

and use a loop over the array to process on each file

for file in "${file_list[@]}"; do
    printf '%s\n' "$file"
    # Your other actions on file goes here
done

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.