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.
This commit is contained in:
swg
2023-11-21 08:18:31 +00:00
parent 7e5ed33058
commit 9d0e0162eb

View File

@@ -4,48 +4,47 @@
## $1 Folder to check. ## $1 Folder to check.
## $2 space separated list of extensions to check for. e.g.: "*.jpg *.JPG *.mp4 *.MP4". ## $2 space separated list of extensions to check for. e.g.: "*.jpg *.JPG *.mp4 *.MP4".
## returns Count of files found with the desired extension. ## returns Count of files found with the desired extension.
function check_folder() {
__check_folder() { local _folder=$1;
local _FOLDER=$1; local _extensions=$2;
local _EXTENSIONS=$2; local _file_count=0;
local _FILE_C=0; if [ -d "$_folder" ]; then # is a folder
if [ -d "$_FOLDER" ]; then # is a folder local _item="";
local _ITEM=""; local _fc=${#Files_N_Paths[@]};
local _FC=${#FILESNPATHS[@]}; pushd "$_folder" 1>/dev/null;
pushd "$_FOLDER" 1>/dev/null; for _item in ${_extensions}; do # lookup all files with the desired extensions
for _ITEM in ${_EXTENSIONS}; do # lookup all files with the desired extensions if [[ -f "$_item" ]]; then # is a file
if [[ -f "$_ITEM" ]]; then # is a file Files_N_Paths[$_FC]="$(pwd)/$_item";
FILESNPATHS[$_FC]="$(pwd)/$_ITEM"; ((_fc+=1));
((_FC+=1)); ((_file_count+=1));
((_FILE_C+=1));
fi fi
done done
# check subfolders if exist # check subfolders if exist
local _SUBFOLDERS="$(/usr/bin/ls . 2>/dev/null)"; local _sub_folders="$(/usr/bin/ls . 2>/dev/null)";
local _CNT=0; local _cnt=0;
for _SUBITEM in ${_SUBFOLDERS}; do for _sub_item in ${_sub_folders}; do
if [[ -d "$_SUBITEM" ]]; then if [[ -d "$_sub_item" ]]; then
__check_folder "$_SUBITEM" "$_EXTENSIONS"; check_folder "$_sub_item" "$_extensions";
_CNT=$? _cnt=$?
((_FILE_C+=_CNT)); ((_file_count+=_cnt));
fi fi
done done
popd 1>/dev/null; popd 1>/dev/null;
fi fi
return $_FILE_C return $_file_count
} }
## Test the check_folder() function with local test folder and images.
__test() { function test() {
MYFOLDER="./cffe_test"; local _my_folder="./cffe_test";
__check_folder $MYFOLDER "*.jpg *.JPG" # space separated list for multiple extensions! check_folder $_my_folder "*.jpg *.JPG" # space separated list for multiple extensions!
FILE_COUNT=$? local _files_count=$?
echo "Found $FILE_COUNT files in folder $MYFOLDER, length of files array is ${#FILESNPATHS[*]}."; echo "Found $_files_count files in folder $_my_folder, length of files array is ${#Files_N_Paths[*]}.";
for ((FILE_C=0;FILE_C<${#FILESNPATHS[*]};FILE_C++)); do for ((_fc=0;_fc<${#Files_N_Paths[*]};_fc++)); do
echo "$FILE_C ${FILESNPATHS[$FILE_C]}"; echo "$_fc : ${Files_N_Paths[$_fc]}";
done done
} }
declare -A FILESNPAHTS;
__test declare -A Files_N_Paths;
test