#!/bin/sh ########################################################################### # # Copyright (c) 2021-2023 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file _functions_ # # @author (last) Behrouz NematiPour # @date (last) 15-Jan-2025 # @author (original) Behrouz NematiPour # @date (original) 15-Jan-2025 # ############################################################################ # Description # Definition of all the globally used functions in scripts. # Parameters # No specific parameter # Retruns # No specific response # Considerations # Any function need to follow the same documentation format, # to make sure the correct documentation is displayed by vscode editor, # when used in the caller script. # sources . ./_errors_ # functions # Description # Trimms the given string by removing leading spaces # Parameters # $1: the given string # Retruns # The left trimmed string. # Considerations # No specific considerations. ltrim() { string="$1" trimmed="${string#"${string%%[![:space:]]*}"}" echo """$trimmed""" } # Description # Trimms the given string by removing trailing spaces # Parameters # $1: the given string # Retruns # The right trimmed string. # Considerations # No specific considerations. rtrim() { string="$1" trimmed="${string%"${string##*[![:space:]]}"}" echo """$trimmed""" } # Description # Trimms the given string by removing leading and trailing spaces # Parameters # $1: the given string # Retruns # The trimmed string. # Considerations # No specific consideration. trim() { trimmed="$1" trimmed="${trimmed#"${trimmed%%[![:space:]]*}"}" trimmed="${trimmed%"${trimmed##*[![:space:]]}"}" echo """$trimmed""" } # Description # Checks if the given string is empty # Parameters # $1: the given string # Retruns # Exits by the $2 if set, on fail, # or by ERR_GENERAL_SCRIPT_FILE_EXIST by default. # Considerations # this function won't trim the given string. check_empty_string() { if [ "$1" = "" ]; then echo "" if [ "$2" = "" ]; then exit "$ERR_GENERAL_SCRIPT_EMPTY_STRING" else exit "$2" fi fi } # Description # Checks if the given string is empty # Parameters # $1: the given string # Retruns # returns true if the given string is empty # Considerations # this function won't trim the given string. # this function won't exit if the given string is empty. is_empty_string() { if [ "$1" = "" ]; then echo true else echo false fi } # Checks if the given parameter is more or equal to required number of parameters. # It also first checks the given parameters to this funcion is correct. # $1: always the $# of the caller script # $2: the number of required parameters. # Exits by ERR_GENERAL_SCRIPT_FILE_EXIST on fail. check_param_count() { # first do the self check PARAM_COUNT=2 if [ "$#" -lt "$PARAM_COUNT" ]; then echo "" exit $ERR_GENERAL_SCRIPT_PARAM_COUNT fi count_act="$1" count_exp="$2" if [ "$count_act" -lt "$count_exp" ]; then echo "" exit $ERR_GENERAL_SCRIPT_PARAM_COUNT fi } # Checks if command execution failed # $1: always the $? of the just executed command # $2: the exit error number # Exits by the $2 if set, on fail, # or by ERR_GENERAL_SCRIPT_FILE_EXIST by default. check_result() { PARAM_COUNT=2 check_param_count "$#" "$PARAM_COUNT" if [ ! "$1" -eq 0 ]; then ERR_ID="$2" if [ "$ERR_ID" = "" ]; then ERR_ID "$ERR_GENERAL_SCRIPT_FAIL" fi # echo $(errorMessage "$ERR_ID") exit "$ERR_ID" fi # echo "" } # Checks if the given file exists # $1: the file full path # Exits with ERR_GENERAL_SCRIPT_FILE_EXIST on fail check_file_exists() { if [ -f "$FILE" ]; then echo "" exit "$ERR_GENERAL_SCRIPT_FILE_EXIST" fi } errorMessage() { # this is the minimum required number of parameters # any error ID can have optional multiple extra parameters to get embedded in the message. PARAM_COUNT=1 check_param_count "$#" "$PARAM_COUNT" case "$1" in "$ERR_CMDFAIL_USB_FOLDER_MAKE" ) echo "usb mount point folder '$2' cannot be made." ;; # "$ERR_CMDFAIL_USB_DEVICE_MOUNT" ) echo "usb device '$2' cannot be mounted." ;; "$ERR_CMDFAIL_USB_DEVICE_UMOUNT" ) echo "usb drive '$2' cannot be unmounted." ;; "$ERR_CMDFAIL_USB_FOLDER_REMOVE" ) echo "usb mount point '$2' cannot be removed." ;; esac }