Help to understand what the problem is, like the script has to dig deep into the folders, but in the end it looks like it goes so that it flies higher in the hierarchy. In the console complains of too deep recursion

code

#!/bin/ksh fileList= catalogHier() { fileList=( "$1" ) curContent=`ls --format single-column` for str in $curContent do curWay=`pwd` fileList[${#fileList[@]}]="$curWay/$str" if [ -d ${fileList[i]} ] then cd ${fileList[i]} fileList=( `catalogHier $fileList` ) fi done cd ../ echo ${fileList[@]} } finalFileList=( `catalogHier $fileList` ) echo There flistSize=${#finalFileList[@]} # for (( i = 0; i < flistSize; i++ )); do # #statements # echo "Content : ${finalFileList[$i]}" # done 

Attempt to solve through find

 while `read -rd $'\0' file`; do echo "Content $file" done < `find ./dir0 ` 
  • or maybe just use find - can it also find all files recursively? And make it faster. - KoVadim
  • @KoVadim, really possible. It is just necessary to sort through the found files for str in``find ./dir0``; do for str in``find ./dir0``; do , but then folders / files with spaces are poorly handled. Or is there also a more simple way? - I. Smirnov
  • Yes, the well-known thing, the bottom line is that the default space is the entity separator, so the trick is to replace the separator (for example, with a null character). Here is more detail askubuntu.com/questions/343727/… - KoVadim
  • @KoVadim, found the solution by reference. thank you - I. Smirnov
  • @KoVadim, there are a couple more questions in the framework of find : why when running the script while read -rd $'\0' file ; do echo "$file" done < " ; do echo "$file" done < " find ./dir0 " the first line displays the path to the script file that is executed, and the last line shows cannot open *последний найденный файл* - I. Smirnov

1 answer 1

since you decided to use the find program, I’ll sound a couple of moments:

  1. no manipulations with line endings for correct processing of file names containing spaces and other special characters are not required:

     $ touch "файл с пробелами в имени" $ find -type f | while read f; do echo "обрабатываем: $f"; done обрабатываем: ./файл с пробелами в имени 
  2. when using a variable that may contain a string with spaces, for example, to transfer its contents as a parameter to some function / program, this variable must be enclosed in double quotes:

     $ find -type f | while read f; do echo "обрабатываем: $f"; ls -l "$f"; done обрабатываем: ./файл с пробелами в имени -rw-r--r-- 1 user user 0 Apr 4 14:08 './файл с пробелами в имени' 

about the source of your script - an error (at least) here:

 if [ -d ${fileList[i]} ] 

You probably wanted to use the “i-th” element of the array. but, first, it was necessary to specify the variable as $i , and secondly, you did not assign the variable $i any value.

accordingly, the expression ${fileList[i]} always returns the first element of the array. like the ${fileList} and $fileList .

  • Thanks, but with find too, as it turned out, not everything was super smooth. And this does not negate the desire to receive an answer to the question asked in the subject - I. Smirnov
  • @ I.Smirnov, I added the answer. - aleksandr barakin
  • So after all, if the condition block were not met, then the recursion would not have happened. - I. Smirnov
  • @ I.Smirnov, programming without debugging - like shooting at a target blindly. Please use debugging messages to understand what exactly happens in your code: echo интересующая_переменная . - aleksandr barakin
  • @ I.Smirnov, I apologize, I misled you. The default behavior of the ${var[произвольный-текст]} operator is to return the first element of the array. I made changes in response. - aleksandr barakin