Compare commits
6 Commits
feature/cl
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5350db8dc3 | ||
|
|
3db5c7fd0f | ||
|
|
683115b818 | ||
|
|
76e890992c | ||
| 9d0e0162eb | |||
| 7e5ed33058 |
49
README.md
49
README.md
@@ -4,14 +4,22 @@ Every script file includes the function and a test to show how it's meant to be
|
|||||||
|
|
||||||
## Check folder for extension
|
## Check folder for extension
|
||||||
|
|
||||||
Checks a given folder for certain file extensions recursively. The [check_folder_for_extension test folder](cffe_test) contains of 13 files, 12 of them images, which should be found by the test.
|
Checks a given folder for certain file extensions recursively. All found files with the extension(s) are returned in a global array; New files are appended to the array.
|
||||||
|
|
||||||
function call:
|
The [check_folder_for_extension test folder](cffe_test) contains of 13 files, 12 of them images, which should be found by the test.
|
||||||
|
|
||||||
|
Function call:
|
||||||
```
|
```
|
||||||
check_folder_for_extension ./cffe_test "*.jpg *.JPG"
|
check_folder $my_folder $extension_list
|
||||||
|
```
|
||||||
|
|
||||||
|
To test the function call:
|
||||||
|
```
|
||||||
|
check_folder_for_extension.sh ./cffe_test "*.jpg *.JPG"
|
||||||
```
|
```
|
||||||
For multiple extensions (or other parts of the file name to match) space separate the list, like shown above.
|
For multiple extensions (or other parts of the file name to match) space separate the list, like shown above.
|
||||||
|
|
||||||
|
|
||||||
## Clean up
|
## Clean up
|
||||||
|
|
||||||
Cleaning up in a script and exit with a given exit code.
|
Cleaning up in a script and exit with a given exit code.
|
||||||
@@ -26,3 +34,38 @@ For testing the function call the script with:
|
|||||||
clean_up.sh test
|
clean_up.sh test
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Logger
|
||||||
|
|
||||||
|
[Logger](logger.sh) to write messages with individual levels to a file.
|
||||||
|
|
||||||
|
In level *debug* all logged messages are printed to stdout, too.
|
||||||
|
|
||||||
|
Log levels are
|
||||||
|
|
||||||
|
5 LLDebug, *for exceptionally verbose messages, also on std out*
|
||||||
|
|
||||||
|
4 LLInfo, *informative messages*
|
||||||
|
|
||||||
|
3 LLWarning, *warning messages*
|
||||||
|
|
||||||
|
2 LLError, *error messages*
|
||||||
|
|
||||||
|
1 LLMdtry and *mandatory messages to get minimal info in a log file*
|
||||||
|
|
||||||
|
0 LLMute *really nothing gets written*
|
||||||
|
|
||||||
|
The higher the level set in `$Log_Level` to the logger, the more verbose it gets.
|
||||||
|
|
||||||
|
```
|
||||||
|
logger $LLWARNING "This is my message";
|
||||||
|
```
|
||||||
|
produces the following message in the log file:
|
||||||
|
|
||||||
|
```
|
||||||
|
logger 20231122_22:43:19 Warning: This is my message!
|
||||||
|
```
|
||||||
|
|
||||||
|
For testing the function call the script with just:
|
||||||
|
```
|
||||||
|
logger.sh
|
||||||
|
```
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
112
logger.sh
Executable file
112
logger.sh
Executable file
@@ -0,0 +1,112 @@
|
|||||||
|
# Log levels for messages
|
||||||
|
LLMute=0
|
||||||
|
LLMdtry=1 # mandatory: messages that are always written to the log (except mute)
|
||||||
|
LLError=2
|
||||||
|
LLWarning=3
|
||||||
|
LLInfo=4
|
||||||
|
LLDebug=5
|
||||||
|
## Actual log level of the logger used
|
||||||
|
Log_Level=$LLInfo
|
||||||
|
|
||||||
|
## base name of the logging script.
|
||||||
|
Log_Name="${0##*/}";
|
||||||
|
|
||||||
|
## Write log of the script.
|
||||||
|
## $1 Loglevel to log the message with.
|
||||||
|
## $2 message to log.
|
||||||
|
function logger() {
|
||||||
|
local _logging_level=$1;
|
||||||
|
local _log_message=$2;
|
||||||
|
if [[ $Log_Level -lt $_logging_level ]]; then
|
||||||
|
return;
|
||||||
|
fi
|
||||||
|
local _now=$($DATE +%Y%m%d_%H:%M:%S);
|
||||||
|
if [[ $_logging_level -eq $LLInfo ]]; then
|
||||||
|
_type=" Info:";
|
||||||
|
elif [[ $_logging_level -eq $LLError ]]; then
|
||||||
|
_type=" ERROR!";
|
||||||
|
elif [[ $_logging_level -eq $LLWarning ]]; then
|
||||||
|
_type=" Warning:";
|
||||||
|
elif [[ $_logging_level -eq $LLDebug ]]; then
|
||||||
|
_type=" DEBUG:";
|
||||||
|
else
|
||||||
|
_type=":";
|
||||||
|
fi
|
||||||
|
printf "$Log_Name ${_now}${_type} ${_log_message}\n" >> $Log_File;
|
||||||
|
if [[ $Log_Level -eq $LLDebug ]]; then
|
||||||
|
printf "${_now}${_type} ${_log_message}\n";
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function test() {
|
||||||
|
printf "Running tests for logger().\n";
|
||||||
|
local _exit_val=0;
|
||||||
|
# Test log level mute: no logfile should be created!
|
||||||
|
Log_Level=$LLMute;
|
||||||
|
logger $LLMdtry "In level mute nothing should be logged!";
|
||||||
|
if [ -e "$Log_File" ]; then # the logfile should not exist!
|
||||||
|
((_exit_val+=1))
|
||||||
|
printf "logger() test on log level Mute: FAILED!\n";
|
||||||
|
rm $Log_File;
|
||||||
|
else
|
||||||
|
printf "logger() test on log level Mute: OK.\n";
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Test log level Mdtry, but log with level error: still no logfile should be created!
|
||||||
|
Log_Level=$LLMdtry;
|
||||||
|
logger $LLError "In level Mdtry an error should not be logged!";
|
||||||
|
if [ -e "$Log_File" ]; then # the logfile should not exist!
|
||||||
|
((_exit_val+=1))
|
||||||
|
printf "logger() test on log level Mandatory; logging an error happend, so test FAILED!\n";
|
||||||
|
rm $Log_File;
|
||||||
|
else
|
||||||
|
printf "logger() test on log level Mandatory: when logging an error failed: test OK.\n";
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Test log level Info, but log with level error: logfile should be created!
|
||||||
|
Log_Level=$LLInfo;
|
||||||
|
logger $LLError "In level Info an error should be logged!";
|
||||||
|
if [ -e "$Log_File" ]; then # the logfile should now exist.
|
||||||
|
printf "logger() test on log level Info: logging an error succeeded, test OK.\n";
|
||||||
|
rm $Log_File;
|
||||||
|
else
|
||||||
|
((_exit_val+=1))
|
||||||
|
printf "logger() test on log level Info: logging an error FAILED!\n";
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Test log level Info, but log with level debug: logfile should not be created!
|
||||||
|
Log_Level=$LLInfo;
|
||||||
|
logger $LLDebug "In level Info a debug message should not be logged!";
|
||||||
|
if [ -e "$Log_File" ]; then # the logfile should not exist.
|
||||||
|
((_exit_val+=1))
|
||||||
|
printf "logger() test on log level Info: logging a debug message succeeded, so test FAILED!\n";
|
||||||
|
rm $Log_File;
|
||||||
|
else
|
||||||
|
printf "logger() test on log level Info: logging a debug message failed: test OK.\n";
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Test log level Debug, but log with level debug: logfile should be created!
|
||||||
|
Log_Level=$LLDebug;
|
||||||
|
logger $LLDebug "In level Debug a debug message should be logged. ALso the message should appaer on std out.";
|
||||||
|
if [ -e "$Log_File" ]; then # the logfile should not exist.
|
||||||
|
printf "logger() test on log level Debug: logging a debug message succeeded, so test OK.\n";
|
||||||
|
rm $Log_File;
|
||||||
|
else
|
||||||
|
((_exit_val+=1))
|
||||||
|
printf "logger() test on log level Debug: logging a debug message failed: test FAILED!\n";
|
||||||
|
fi
|
||||||
|
|
||||||
|
return $_exit_val;
|
||||||
|
}
|
||||||
|
|
||||||
|
DATE=$(which date);
|
||||||
|
|
||||||
|
Run_Date=$($DATE +%Y_%m_%d)
|
||||||
|
Log_File="${Run_Date}_${Log_Name}.log";
|
||||||
|
|
||||||
|
test
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
printf "All test succeeded.\n";
|
||||||
|
else
|
||||||
|
printf "$? of 5 tests FAILED!\n";
|
||||||
|
fi
|
||||||
Reference in New Issue
Block a user