bash function library - ghdrako/doc_snipets GitHub Wiki

Example

#!/bin/bash

# string_utils.sh

# Function to check if a string is empty.
# Arguments:
#   $1 - The string to check.
# Returns:
#   0 if the string is empty, 1 otherwise.
is_empty() {
    [ -z "$1" ]
}

# Function to convert a string to uppercase.
# Arguments:
#   $1 - The string to convert.
# Outputs:
#   The uppercase version of the string.
to_uppercase() {
    echo "$1" | tr '[:lower:]' '[:upper:]'
}

# Function to replace a substring in a string with another substring.
# Arguments:
#   $1 - The original string.
#   $2 - The substring to search for.
#   $3 - The substring to replace with.
# Outputs:
#   The modified string with the replacements.
replace_substring() {
    local str="$1"
    local search="$2"
    local replace="$3"
    echo "${str//$search/$replace}"
}

# Global constant
readonly VOWELS="aeiou"

# Function to count the number of vowels in a string.
# Arguments:
#   $1 - The string to analyze.
# Outputs:
#   The count of vowels in the string.
count_vowels() {
    local str="$1"
    local count=0

    for (( i=0; i<${#str}; i++ )); do
        if [ "$VOWELS" == *"${str:i:1}"* ](/ghdrako/doc_snipets/wiki/-"$VOWELS"-==-*"${str:i:1}"*-); then
            ((count++))
        fi
    done

    echo "$count"
}

Using library

To source a library file, we use the source command or the shorthand . operator. The syntax is straightforward:

source /path/to/library.sh
# or equivalently
. /path/to/library.sh

Example

#!/bin/bash

source string_utils.sh

# Example usage
if is_empty ""; then
    echo "String is empty."
fi

uppercase_string=$(to_uppercase "hello world")
echo "Uppercase: $uppercase_string"

replaced_string=$(replace_substring "hello world" "world" "Bash")
echo "Replaced: $replaced_string"

If a library depends on another library or external command, it is best to declare these dependencies at the beginning of the script. This practice ensures that all required resources are available before any function begins execution

#!/bin/bash

# main_script.sh

# Import libraries
source ./string_utils.sh

# Ensure dependent commands are available
command -v tr > /dev/null 2>&1 || {
    echo "tr command is required but it's not installed. Exiting."
    exit 1
}

# Example usage of imported functions
input_string="hello world"
vowel_count=$(count_vowels "$input_string")
echo "The number of vowels in '$input_string' is: $vowel_count"
#!/bin/bash

# math_utils.sh
# Provides basic mathematical operations

# Function: math_add
# Adds two numbers
# Usage: math_add num1 num2
math_add() {
    local num1=$1
    local num2=$2
    echo $((num1 + num2))
}

# Function: math_subtract
# Subtracts the second number from the

Check dependency

#!/bin/bash

# math_utils.sh
# Provides basic mathematical operations and dependency checks

# Function: check_dependency
# Checks if a command exists
# Usage: check_dependency command
check_dependency() {
    command -v "$1" &>/dev/null
}

# Function: math_divide
# Divides the first number by the second
# Usage: math_divide num1 num2
math_divide() {
    local num1=$1
    local num2=$2
    
    if ! check_dependency "bc"; then
        echo "Error: bc is not installed." >&2
        return 1
    fi
    
    if [ "$num2" -eq 0 ]; then
        echo "Error: Division by zero is not allowed." >&2
        return 1
    fi

    echo "scale=2; $num1 / $num2" | bc
}