#!/bin/bash # v.0.0.0.2 # 23.4.2024 # is_number(value) # checks if value is whole number numeric string # returns: 0 - not a number # 1 - is a number is_number() { local value="$1" re='^[0-9]+$' if ! [[ $value =~ $re ]] ; then return 0 else return 1 fi } # is_email(value) # checks if value is valid email string # returns: 0 - not a email # 1 - is a email is_email() { local email="$1" local regex="^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$" if [[ "$email" =~ $regex ]]; then return 1 else return 0 fi } # argument_exists(argument_value argument_name) # checks if argument is empty, then print message and exit with code 1 # returns: 0 - not exists # 1 - exists argument_exists() { # Zkontroluj, zda je zadán první argument if [ -z "$1" ]; then echo "$2 argument must be filled." return 0 fi return 1 } # dir_get_size(path_to_dir) # returns directory size if exists, otherwise exit 1 # returns: size - size in bytes # exit 1 - path_to_dir not exists dir_get_size(){ # Zkontroluj, zda existuje adresář path_to_dir local dir="$1" if [ ! -d $dir ]; then exit 1 fi result=$(du -sb $dir | cut -f1) echo $result } # dir_ensure_trailing_slash(path_to_dir) # returns full path with trailing slash at the end, therefore full path not exists. # returns: full path wirh trailing slash dir_ensure_trailing_slash() { local path="$1" if [[ ! "$path" == */ ]]; then path="${path}/" fi echo "$path" } # dir_get_name(path_to_dir) # returns directory name from full path, therefore full path not exists. # returns: name of directory dir_get_name() { local full_path=$(dir_ensure_trailing_slash "$1") local directory_name="${full_path##*/}" echo "$directory_name" } # filename_get_ymwd(prefix extension) # creates filename that contains ____. # returns: filename filename_get_ymwd(){ # Získání aktuálního data local current_date=$(date +'%Y-%m-%d') # Například: 2024-04-22 # Získání čísla roku local year=$(date +'%Y') # Například: 2024 # Získání čísla měsíce local month=$(date +'%m') # Například: 04 # Získání čísla týdne v roce local week_number=$(date +'%U') # Například: 16 # Získání čísla dne v týdnu (0 = neděle, 1 = pondělí, ..., 6 = sobota) local day_of_week=$(date +'%w') # Například: 6 (sobota) # Sestavení jména souboru local file_name="$1_${year}_${month}_${week_number}_${day_of_week}.$2" echo $file_name } # mail_send(recipient subject body is-test) # sends text email with subject and body to recipient via sendmail # if is-test="true" then just print message if set to "false" or argument is missing send via sendmail mail_send(){ local recipient=$1 local subject="$2" local body="$3" local is-test=$4 is_email $recipient if [ $? -eq 0 ]; then echo " argument '$recipient' is not valid email address." exit 1 fi argument_exists "$subject" "" echo "$body" | mail -s "$subject" $recipient }