I am working on a housekeeping shell script to delete old files and update some files. I want to know whether the .sh is completed without problem. So i made a checking part in the shell script.
The following .sh example checks if a folder is empty or not.
#!/bin/sh # Read a folder path from user input and check if it is # empty or not # Read the folder location # "\c" means keep the cursor on the same line echo "Please input the folder location: \c" read _folder # Quit if the folder does not exist if [ ! -e $_folder ]; then echo 'Sorry, folder does not exist.' exit 1 fi # Check if the folder is empty or not if [ $(ls $_folder | wc -l) -eq 0 ]; then echo "$_folder is EMPTY" else echo "$_folder is NOT EMPTY" fi