From 9d0e0162ebb078b441510ae4642f37cf3c629fa4 Mon Sep 17 00:00:00 2001 From: swg Date: Tue, 21 Nov 2023 08:18:31 +0000 Subject: [PATCH] apply style guide Applied [Google Shell Scripting Style Guide](https://google.github.io/styleguide/shellguide.html) to the check_folder snippet. fixed naming bug of filesnpaths variable. --- check_folder_for_extension.sh | 63 +++++++++++++++++------------------ 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/check_folder_for_extension.sh b/check_folder_for_extension.sh index 6a9f907..646e1ab 100755 --- a/check_folder_for_extension.sh +++ b/check_folder_for_extension.sh @@ -4,48 +4,47 @@ ## $1 Folder to check. ## $2 space separated list of extensions to check for. e.g.: "*.jpg *.JPG *.mp4 *.MP4". ## returns Count of files found with the desired extension. - -__check_folder() { - local _FOLDER=$1; - local _EXTENSIONS=$2; - local _FILE_C=0; - if [ -d "$_FOLDER" ]; then # is a folder - local _ITEM=""; - local _FC=${#FILESNPATHS[@]}; - pushd "$_FOLDER" 1>/dev/null; - for _ITEM in ${_EXTENSIONS}; do # lookup all files with the desired extensions - if [[ -f "$_ITEM" ]]; then # is a file - FILESNPATHS[$_FC]="$(pwd)/$_ITEM"; - ((_FC+=1)); - ((_FILE_C+=1)); +function check_folder() { + local _folder=$1; + local _extensions=$2; + local _file_count=0; + if [ -d "$_folder" ]; then # is a folder + local _item=""; + local _fc=${#Files_N_Paths[@]}; + pushd "$_folder" 1>/dev/null; + for _item in ${_extensions}; do # lookup all files with the desired extensions + if [[ -f "$_item" ]]; then # is a file + Files_N_Paths[$_FC]="$(pwd)/$_item"; + ((_fc+=1)); + ((_file_count+=1)); fi done # check subfolders if exist - local _SUBFOLDERS="$(/usr/bin/ls . 2>/dev/null)"; - local _CNT=0; - for _SUBITEM in ${_SUBFOLDERS}; do - if [[ -d "$_SUBITEM" ]]; then - __check_folder "$_SUBITEM" "$_EXTENSIONS"; - _CNT=$? - ((_FILE_C+=_CNT)); + local _sub_folders="$(/usr/bin/ls . 2>/dev/null)"; + local _cnt=0; + for _sub_item in ${_sub_folders}; do + if [[ -d "$_sub_item" ]]; then + check_folder "$_sub_item" "$_extensions"; + _cnt=$? + ((_file_count+=_cnt)); fi done popd 1>/dev/null; fi - return $_FILE_C + return $_file_count } - -__test() { - MYFOLDER="./cffe_test"; - __check_folder $MYFOLDER "*.jpg *.JPG" # space separated list for multiple extensions! - FILE_COUNT=$? - echo "Found $FILE_COUNT files in folder $MYFOLDER, length of files array is ${#FILESNPATHS[*]}."; - for ((FILE_C=0;FILE_C<${#FILESNPATHS[*]};FILE_C++)); do - echo "$FILE_C ${FILESNPATHS[$FILE_C]}"; +## Test the check_folder() function with local test folder and images. +function test() { + local _my_folder="./cffe_test"; + check_folder $_my_folder "*.jpg *.JPG" # space separated list for multiple extensions! + local _files_count=$? + echo "Found $_files_count files in folder $_my_folder, length of files array is ${#Files_N_Paths[*]}."; + for ((_fc=0;_fc<${#Files_N_Paths[*]};_fc++)); do + echo "$_fc : ${Files_N_Paths[$_fc]}"; done } -declare -A FILESNPAHTS; -__test +declare -A Files_N_Paths; +test