User Tools

Site Tools


linux:bash_helper_howto

Bash Helper Howto

This is a set of dynamic helper functions that you can source in your bash script. It can help making your bash script easier to write and make it easier to read. The helper script itself can be found here

If you wish to use the helper functions all you need to do is source it from your main bash script. Once this is done you are ready to use the different functions. Below are some example on how to use them. Note that even though there are multiple helpers in the helper function you don't have to use them all you just call the specific helper function you need. Note the helper script has to be in the same directory as your main bash script.

You only need to load the helper function once in the top of your main script. It could looks something like this:

#!/usr/bin/env bash
 
source "$(dirname "$0")/helper"
 
# Rest of your code below

The above code in mandatory for this to work and hence I will not repeat the above code in the examples.

Check if root

Check if the main bash script is being executed as root or using sudo. If this is not the case the script will exit and alert the user.

must_be_root yes
 
# The line below can be omitted it's only for demonstration purpose
printf "\nScript executed as root or sudo continuing...\n\n"

Now if you run the above bash script like this:

./myscript.sh

It will fail with the following message:

This script must be run as root or use sudo

If you execute it like this or as root:

sudo ./myscript.sh

The script will continue and print the following message:

Script executed as root or sudo continuing...

Check for right directory

Check if the user is executing the main bash script from within the right directory. If this is not the case the script will exit and alert the user. Note the quotes are important around the directory name.

must_be_in_dir "some_dir"
 
# The line below can be omitted it's only for demonstration purpose
printf "\nRight directory detected continuing...\n\n"

If the user executes the script from for example test_dir the script will stop and print the following:

Wrong directory! This script must be run from the directory 'some_dir'

And if the user executes the script from the right directory if will continue and print the following:

Right directory detected continuing...

Output x characters

This function will output x predefined characters.

echo "Printing 10 hyphens"
line '-' 10
echo "printing 50 plus signs"
line '+' 50

Dynamic Getopts

Dynamic getopts This will blow you away It means what it says on the tin. Getops using flags and usage beeing output dynamically. But wait there is more “This is a gift that keeps on giving” We have dynamic flags so the getopts order is no longer relevant. I will explain with a few examples below.

Example 1 Working on it….

Usage example 1 no getopts

Usage one argument required i.e $1

declare -a flags=(
    "n:hostname:WordPress hostname:wp.example.com:true"
)
 
validate_input "$@"
 
echo "Hostname is: $hostname"

Output when running the script.

./myscript.sh
Error! WordPress hostname is empty.

Usage: ./myscript.sh <WordPress hostname>

Example:
./myscript.sh wp.example.com

./myscript.sh wp.something.xyz
Hostname is: wp.something.xyz

Usage example 2 no getopts

Usage three arguments required i.e $1 $2 $3

declare -a flags=(
    "n:hostname:WordPress hostname:wp.example.com:true"
    "u:dbuser:WordPress DB user:wpuser:true"
    "p:dbpass:WordPress DB password:secret:true"
)
 
validate_input "$@"
 
echo "WordPress hostname: $hostname"
echo "Database user: $dbuser"
echo "Database password: $dbpass"

Output when running the script.

./myscript.sh
Error! WordPress hostname is empty.
Error! WordPress DB user is empty.
Error! WordPress DB password is empty.

Usage: ./myscript.sh <WordPress hostname> <WordPress DB user> <WordPress DB password>

Example:
./myscript.sh wp.example.com wpuser secret

Usage example 3 using getopts

Usage one argument required -p MariaDB root passsword

declare -a flags=(
    "p:mariadbpass:MariaDB root password:secret:true"
)
 
while getopts ":p:" opt; do
    case $opt in
        p) mariadbpass="$OPTARG" ;;
        *) usage "$@"; exit 1 ;;
    esac
done
 
validate_input "$@"
 
echo "MariaDB root password is: $mariadbpass"

Output when running the script.

./myscript.sh
Error! MariaDB root password is empty.

Usage: ./myscript.sh [options]
  -p  MariaDB root password
  -h  Show help

Example:
./myscript.sh -p secret

./myscript.sh -p supersecret
MariaDB root password is: supersecret

Usage example 4 using getopts

Usage four arguments required -n -m -d -p

declare -a flags=(
    "n:hostname:WordPress hostname:wp.example.com:true"
    "m:mariadbroot:MariaDB root password:rootpass:true"
    "d:dbname:WordPress DB name:wpdb:true"
    "p:dbpass:WordPress DB password:secret:true"
)
 
# Parse flags with getopts
while getopts ":n:m:d:p:" opt; do
    case $opt in
        n) hostname="$OPTARG" ;;
        m) mariadbroot="$OPTARG" ;;
        d) dbname="$OPTARG" ;;
        p) dbpass="$OPTARG" ;;
        *) usage "$@"; exit 1 ;;
    esac
done
 
validate_input "$@"
 
echo "WordPress hostname: $hostname"
echo "MariaDB root password: $mariadbroot"
echo "WordPress DB name: $dbname"
echo "WordPress DB password: $dbpass"

Output when running the script.

./myscript.sh
Error! WordPress hostname is empty.
Error! MariaDB root password is empty.
Error! WordPress DB name is empty.
Error! WordPress DB password is empty.

Usage: ./myscript.sh [options]
  -n  WordPress hostname
  -m  MariaDB root password
  -d  WordPress DB name
  -p  WordPress DB password
  -h  Show help

Example:
./myscript.sh -n wp.example.com -m rootpass -d wpdb -p secret

./myscript.sh -n wp.something.xyz -m rootpass -d wpdb -p mysecret
WordPress hostname: wp.something.xyz
MariaDB root password: rootpass
WordPress DB name: wpdb
WordPress DB password: mysecret

Service check

The purpose of this function is to check if a service is up. This can be used as a failsafe if one tries to install let's say MariaDB but there is allready a MariaDB service running in which case the script will exit.

Service check example 1

MariaDB has to be running.

must_be_running mariadb

If MariaDB is running no output is given and the script continues. If MariaDB is not running the script will exit with the following output.

The following service(s) appear not to be running, cannot continue...
  - mariadb

Service check example 2

MariaDB and Nginx has to be running.

must_be_running mariadb nginx

If MariaDB and Nginx is running no output is given and the script continues. If MariaDB is running, but Nginx is not running the script will exit with the following output.

The following service(s) appear not to be running, cannot continue...
  - nginx

Service check example 2

MariaDB and Nginx has to be running postfix must not be running.

must_be_running mariadb nginx
must_not_be_running postfix

If MariaDB and Nginx running and Postfix is not running no output is given and the script continues. If MariaDB is running, but Nginx is not running and Postfix is not running the script will exit with the following output:

The following service(s) appear not to be running, cannot continue...
  - nginx

Note in the above example we will never reach the Postfix check as the script has exited because Nginx is not running.

Only when MariaDB and Nginx is running the “must not be running” function triggers. And therefore if MariaDB and Nginx as well as Postfix is running the script will exit (bacause Postfix should not be running) with the following output:

The following service(s) appear already to be running, cannot continue...
  - postfix

Git clone

Does what it says on the tin. It clones a git repository to any given path if the repository does not exists. If “$default_branch” is not provided it resolves to main.

nginxsnippets="/etc/nginx/nginxsnippets"
repo="https://git.x-files.dk/ubuntu-web-server/nginxsnippets.git"
 
git_clone_if_missing "$repo" "$nginxsnippets"

Here is an example where we pull a branch called test instead.

nginxsnippets="/etc/nginx/nginxsnippets"
repo="https://git.x-files.dk/ubuntu-web-server/nginxsnippets.git"
default_branch="test"
 
git_clone_if_missing "$repo" "$nginxsnippets" "$default_branch"
linux/bash_helper_howto.txt · Last modified: by Allan