From 9201c89238651c82b5957798bb581e11af85d800 Mon Sep 17 00:00:00 2001 From: Christoph Date: Mon, 17 Oct 2022 00:20:38 +0200 Subject: [PATCH] Add scipt 'is_empty_dir.sh'. --- snippets/is_empty_dir.sh | 69 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100755 snippets/is_empty_dir.sh diff --git a/snippets/is_empty_dir.sh b/snippets/is_empty_dir.sh new file mode 100755 index 0000000..171e163 --- /dev/null +++ b/snippets/is_empty_dir.sh @@ -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 " + 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