dimanche 27 septembre 2015

CShell Script - checking if directory is empty within an "if" condition

I'm trying to write a C shell script that sets up a junk directory, like a trash bin, so that you can "junk" a file to the directory instead of "rm" it so that it is permanently gone. One of the options of this junk command is -p, which purges the directory (empties the trash bin). In testing, I saw that, if I tried to call my foreach loop while the directory was empty, I got an error. I want to add a simple check that says "if the junk directory is not empty" then go remove all of its files. Below is code that happened to work when the directory was, indeed, empty (it outputted the echo statement "empty"). However, when the directory actually had files in it, I was met with an "if: Syntax error" message. Any help would be greatly appreciated.

echo "Purging junk directory..."
            if (`find ~/.junk -not -empty`) then
                    foreach file (~/.junk/*)
                            rm $file
                    end
            else
                    echo "empty"
            endif

Aucun commentaire:

Enregistrer un commentaire