Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I was trying to find the folders in my music collection that do not contain any mp3 files (some have only album art, or playlists and the like). So I had something like this in a shell script.

#!/bin/bash
for DIR in *
do
        [[ -d "${DIR}" ]] || continue
        cd "${DIR}"

        ls *.mp3 > /dev/null 2>&1
        if [[ "$?" != "0" ]]
        then
                echo $DIR has no MP3 files!
                continue
        fi

        cd ..
done

Yet, even though there are many folders to go through, this script only checks the first one before exiting... why is this? It's as though the continue inside the if is breaking out of the loop entirely.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

The continue statement inside the if block is the source of your problem:

If no MP3 files were found, cd .. doesn't get executed and the acondition [[ -d "${DIR}" ]] will most likely fail for all remaining directories.

share|improve this answer
    
Yup! You were exactly right! Can't believe I didn't catch that earlier. Thank you :) –  Alex Reinking Jan 23 at 22:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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