0

I have the following Bash script. It looks for pictures in 3 (or more) directories.

#!/bin/sh
declare -a ext=(".tga" ".jpg" ".png")
declare -a directory=(
  "../Untitled.ed.tga/" "../Untitled.ed.jpg/" "../Untitled.ed.png/")        
for ((ctDir=0; ctDir<${#directory[@]}; ctDir++));
do 
  #get number of Pix in each Dir
  cd ${directory[${ctDir}]}
  let numPixInDir[${ctDir}]=$(
    bc <<< " $( ls *${ext[${ctDir}]} | wc -l) " )
  let "f = numPixInDir[${ctDir}]"
  echo "f == ${f}"
  echo "numPix == ${numPixInDir[${ctDir}]}"
done

for ((ctN=1; ctN<=${f}; ctN++));
do
  for ((ctDir=0; ctDir<${#directory[@]}; ctDir++));
  do
    printf "%05d\t" ${ctN}
  done
  printf '\n'
done

The above does what I want, provided the dirs have the same number of files, but if I set the variable ${f} to an array for each of the directories -

let "f[${ctDir}] += numPixInDir[${ctDir}]"

Then the first for loop raises error:

((: ctN<=: syntax error: operand expected (error token is "=")

Anyone figure how I can do this successfully?

Biffen
  • 6,249
  • 6
  • 28
  • 36
user1321964
  • 35
  • 1
  • 6
  • ls is always considerfed a risk in these cases because it may output differently to your expectation and filenames may contain other characters. You could look at extglob as described here: https://stackoverflow.com/questions/4691956/how-to-make-bash-expand-wildcards-in-variables – Gem Taylor Sep 12 '18 at 15:24
  • You say Bash but the shebang says sh. Which one is it? – Biffen Sep 13 '18 at 09:34
  • This looks unnecessarily complicated and error-prone. Maybe explain in clear, simple English what you are trying to achieve. – Mark Setchell Sep 13 '18 at 10:25
  • @Biffen The question only makes sense if we assume `bash`; the OP maybe be operating under the assumption that `#!/bin/sh` will always refer to an instance of `bash`. – chepner Sep 13 '18 at 16:14

0 Answers0