BASH functions

This is a companion page to the Shell Scripts page, as this is a list of functions I have created for usage in my scripts. These functions are from scripts which are licensed under GNU GPL v2 so you are free to use them as you wish, in accordance to what the license authorizes of course.

# remTrailingSlash()
# Remove trailing forward slashes ex: fixedDir=$(rem_trailing_slash $Dir)
remTrailingSlash() {
    echo $1 | sed 's/\/*$//g'
}


# saveTables()
# Modified function from below, purpose is to detect which distribution is running so
# the rules may be saved in a way for each distribution as to work on a wider range of
# systems rather then just Gentoo.
# From: https://danielgibbs.co.uk/2013/04/bash-how-to-detect-os/
saveTables(){
    arch=$(uname -m)
    kernel=$(uname -r)
    voidLinux=$(cat /proc/version | cut -d " " -f 4)
    ubuntuLinux=$(cat /etc/lsb-release | head -n 1 | cut -d = -f 2)
    if [ "$ubuntuLinux" == "Ubuntu" ]; then
        echo "To easily manage iptables a new package named iptables-services must be installed. Proceed? Y/N"
        read -r packageAnswer
        if [[ $packageAnswer == "Y" || $packageAnswer == "y" ]]; then
            apt-get update
            apt-get install iptables-persistent
            echo
            echo " * Saving all iptables settings"
            /etc/init.d/iptables-persistent save
        fi
    elif [ -f /etc/debian_version ]; then
        echo " * Saving all iptables settings"
        mkdir /etc/iptables
        iptables-save > /etc/iptables/rules.v4
        ip6tables-save > /etc/iptables/rules.v6
        iptables-restore < /etc/iptables/rules.v4
        ip6tables-restore < /etc/iptables/rules.v6
    elif [ -f /etc/arch-release ]; then 
        echo " * Saving all iptables settings"
        iptables-save > /etc/iptables/iptables.rules
        ip6tables-save > /etc/iptables/ip6tables.rules
        echo "Enable iptables at boot and start service? Y/N"
        read -r systemdAnswer
        if [[ $systemdAnswer == "Y" || $systemdAnswer == "y" ]]; then
            echo "User entered: $systemdAnswer - enabling and starting iptables services"
            systemctl enable iptables.service
            systemctl enable ip6tables.service
            systemctl start iptables.service
            systemctl start ip6tables.service
        else
            echo "Skipping starting and enabling iptables systemd units"
        fi
    elif [ -f /etc/redhat-release ]; then
        echo " * Saving all iptables settings"
        echo "To easily manage iptables a new package named iptables-services must be installed. Proceed? Y/N"
        read -r packageAnswer
        if [[ $packageAnswer == "Y" || $packageAnswer == "y" ]]; then
            echo
            yum install -y iptables-services
        fi
        echo
        systemctl enable ip6tables
        systemctl enable iptables
        systemctl start iptables
        systemctl start ip6tables
        service iptables save
        service ip6tables save
        echo
        echo "It is necessary to disable firewalld if using iptables. Proceed? Y/N"
        read -r firewallAnswer
        if [[ $firewallAnswer == "Y" || $firewallAnswer == "y" ]]; then
            echo "User entered: $firewallAnswer - Disabling firewalld"
            systemctl stop firewalld
            systemctl mask firewalld
        fi
    elif [ "$voidLinux" == "(xbps-builder@build.voidlinux.eu)" ]; then
        echo " * Saving all iptables settings"
        iptables-save > /etc/iptables/iptables.rules
        ip6tables-save > /etc/iptables/ip6tables.rules
    elif [ -f /etc/lsb-release ] || [ -f /etc/gentoo-release ]; then
        echo " * Saving all iptables settings"
        /etc/init.d/iptables save
        /etc/init.d/ip6tables save
    else
        echo "Warning: Your distribution was unable to be detected which means the"
        echo "iptables rules are unable to be automatically saved and made persistent."
        echo "You will need to search of how to save them for your distribution." 
        echo
        echo "Please report this error! remember to include which distribution you are using"
        echo "https://github.com/jeekkd/restricted-iptables"
    fi
}

# rsyncFunction()
# Put rsync portion in a function due to being called multiple times through out the 
# logic structure. Saves repeat code by functionizing it
rsyncFunction() {
    sourceArrayLength=${#sourceArray[@]}
    adjustedLength=$(( sourceArrayLength - 1 ))

    for i in $( eval echo {0..$adjustedLength} ); do
        rsync --log-file=/var/log/rsync_backup.log -urzq --delete $(printf '%q\n' "${sourceArray[$i]}") $(printf '%q\n' "$backupDir""${destinationArray[$i]}")
    done
}


# import()
# Important module(s) for the purpose of modularizing the script. Usage is simply
# import <name of module>, do not add the file extension.
import() {
  module=$1
  . "$script_dir"/modules/${module}.sh
}


# get_script_dir()
# Gets the directory the script is being ran from. To be used with the import() function
# so the configuration is imported from its absolute path
get_script_dir() {
    script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
}

# askInitramfs()
# Function to ask the user if they also need a initramfs, if yes it will create and install the initramfs.
askInitramfs() {
    local initramfsAnswer=
    local initramfsType=
    for (( ; ; )); do
        printf "\n"
        printf "Do you also need a initramfs? Y/N \n"
        read -r initramfsAnswer
        if [[ $initramfsAnswer == "Y" ]] || [[ $initramfsAnswer == "y" ]]; then
            printf "Press 1 - Standard genkernel initramfs. \n"
            printf "Press 2 - Genkernel initramfs with support for luks, lvm, busybox. \n"
            printf "Press 3 - Generic host-only dracut initramfs. \n"
            printf "Press 4 - To skip this part. \n"
            read -r initramfsType
            if [[ $initramfsType == "1" ]]; then
                isInstalled "sys-kernel/genkernel-next"
                genkernel --install initramfs
                if [ $? -gt 0 ]; then
                    genkernel --install initramfs
                fi
                break
            elif [[ $initramfsType == "2" ]]; then
                isInstalled "sys-kernel/genkernel-next"
                genkernel --luks --lvm --busybox initramfs
                if [ $? -gt 0 ]; then
                    genkernel --luks --lvm --busybox initramfs
                fi
                break
            elif [[ $initramfsType == "3" ]]; then
                mkdir -p /etc/portage/package.keywords/
                printf "sys-kernel/dracut" >> /etc/portage/package.keywords/dracut
                isInstalled "sys-kernel/dracut"
                dracut --hostonly '' "$currentKernel"
                break
            elif [[ $initramfsType == "4" ]]; then
                printf "Skipping adding an initramfs.. \n"
                break
            else
                printf "Error: Select an option that is the numeric value of 1 to 4 \n"
                printf "\n"
            fi
        elif [[ $initramfsAnswer == "N" || $initramfsAnswer == "n" ]]; then
            printf "Skipping adding an initramfs.. \n"
            break
        else
            printf "Error: Invalid selection, enter either Y or N \n"
        fi
    done
}




# confUpdate()
# if a configuration file needs to be updated during an emerge it will update it then retry the emerge
confUpdate() {
    emerge --autounmask-write -q $1
    if [ $? -eq 1 ]; then
        etc-update --automode -5
        emerge --autounmask-write -q $1
    fi
    env-update && source /etc/profile
}



# isInstalled
# If given a valid package atom it will check if the package is installed on the local system
# Example: isInstalled "sys-kernel/genkernel-next" 
# If the package is not installed it will call confUpdate and install the package
isInstalled() {
    local package=$1
    local packageTest=
    packageTest=$(equery -q list "$package")
    if [[ -z ${packageTest} ]]; then
        confUpdate "$package"
    fi
}

# unmaskKernel() 
# Unmask the users selected kernel so unstable versions may be installed
unmaskKernel() {
    local unmaskAnswer=
    local kernelName=
    if [[ $unmaskAnswer == "Y" ]] || [[ $unmaskAnswer == "y" ]]; then
        kernelName=$(printf "$1" | cut -f 2 -d "/") 
        mkdir -p /etc/portage/package.keywords/
        printf "$1\n" > /etc/portage/package.keywords/"$kernelName"
    fi
}