1

I have a strange issue here using bash. In the following script (this is a simplified version) the find command should list all folders but it ignores some folders:

/Home Made videos
/Home made videos [1080p]

It will show only "/Home Made videos" and the count will be 1.

If I run the find command on the command line (outside script), it returns all files. Can someone say why and how to avoid it?

IFS=$'\n'
MAIN_DIR="$1"
DirArray=( $(find "$MAIN_DIR" -type d) ) 
tLenDir=${#DirArray[@]} 
for (( j=0; j<${tLenDir}; j++ ));
do
  echo "Folder: ${DirArray[$j]}"
done

Actually it was part of a bigger script (wrongly cut), the following code has the problem:

function main() {
    OLDIFS=$IFS
    IFS=$'\n'
    shopt -s nullglob
    MAIN_DIR="$1"
    [ "$MAIN_DIR" == "" ] && MAIN_DIR="."
    DirArray=( $(find "$MAIN_DIR" -type d) ) 
    tLenDir=${#DirArray[@]} 
    echo "Found ${tLenDir}"
    for (( j=0; j<${tLenDir}; j++ ));
    do
      if [[ "${DirArray[$j]}" != "." ]] && [[ "${DirArray[$j]}" != ".." ]] && [[ "${DirArray[$j]}" != "" ]] ; then
        echo "Folder: ${DirArray[$j]}"
      fi
    done
    IFS=$OLDIFS
    shopt -u nullglob
}

main "$1"

My bash is 4.3.39, the idea was (a) finding the folders then (b) for each one find the files. I managed to solve the problem changing to one find:

fileArray=($(find "$DIR" -type f -name '*.mkv' -o -name '*.mp4' -o -name '*.avi' ))

But if someone knows what was wrong with the old script I would like to know.

Sample:

"FolderA"
- "Folder B"
- "Folder [1080p]"
Thomas Dickey
  • 75,040
  • 9
  • 171
  • 268
Alex
  • 11
  • 2
  • 1
    I can't reproduce your problem. Which version of bash are you using? ./test.sh . Folder: . Folder: ./Home Made videos [108-p] Folder: ./Home Made videos – tink Aug 11 '15 at 03:38
  • Are you using `script dir1 dir2` as input? Then (if dir1 has no subdirs) of course only dir1 will show up as `MAIN_DIR="$1"` ... at least that is what I read from your question. – FelixJN Aug 11 '15 at 07:34
  • No, just one parameter. In the sample I just gave I run on FolderA – Alex Aug 13 '15 at 00:45
  • This question arguably falls under the umbrella of [Why does my shell script choke on whitespace or other special characters?](http://unix.stackexchange.com/q/131766/80216), but it isn’t clearly answered there. *That* question and its answers are 90% about whitespace and quoting — which are irrelevant to *this* question. This question is a cautionary tale regarding the inadvisability of setting shell variables to filename(s) by command substitution; not unlike [parsing the output of `ls`](http://mywiki.wooledge.org/ParsingLs). The short answer is to replace `shopt -s nullglob` with `set -f`. – G-Man Says 'Reinstate Monica' Oct 16 '15 at 04:20
  • And, unfortunately, this was a drive-by question — [Alex](http://unix.stackexchange.com/users/128086/alex) joined U&L (and no other Stack Exchange communities), asked this one question five days later, revised it, posted the one comment, and then vanished, never to return. – G-Man Says 'Reinstate Monica' Oct 16 '15 at 04:21

0 Answers0