Add scipt 'is_empty_dir.sh'.

This commit is contained in:
Christoph 2022-10-17 00:20:38 +02:00
parent 62bb096b20
commit 9201c89238

69
snippets/is_empty_dir.sh Executable file
View File

@ -0,0 +1,69 @@
#!/usr/bin/env bash
# Set the variable for bash behavior
shopt -s nullglob
shopt -s dotglob
# Die if dir name provided on command line
[[ $# -eq 0 ]] && { echo "" ; echo " Usage: $0 dir-name"; echo "" ; exit 1; }
# Check for empty files using arrays
chk_files=(${1}/*)
echo ""
(( ${#chk_files[*]} )) && echo " Files found in '$1' directory." || echo " Directory '$1' is empty."
echo ""
# Unset the variable for bash behavior
shopt -u nullglob
shopt -u dotglob
# ---------------------------------------------
# Check if directory is empty
#
is_empty_directory () {
# Set the variable for bash behavior
shopt -s nullglob
shopt -s dotglob
# Die if no dir name was provided on command line
if [[ $# -eq 0 ]]; then
echo ""
echo " [ Error ]: Wrong call of function 'is_empty_directory ()'.
Usage: is_empty_directory <dir-name>"
echo ""
return 1
fi
chk_empty_dir=(${1}/*)
# Unset the variable for bash behavior
shopt -u nullglob
shopt -u dotglob
if (( ${#chk_empty_dir[*]} )) ; then
# Files found in $1 directory (send false)
return 1
else
# Directory $1 is empty. (send true)
return 0
fi
}
echo ""
if $(is_empty_directory "${1}") ; then
echo " Directory '$1' is empty."
else
echo " Files found in '$1' directory."
fi
echo ""
exit 0