Calling functions with pattern matching in bash

Calling functions with pattern matching in bash

This script is pretty simple in its functionality, however it shows how you can utilize passing bash arguments to get different results, or to call different functions within the script.

In this example we are just echoing out the argument that is being passed, but only if it matches what the script is looking for. These types of functions are particularly useful if you have a multi function script which changes its behavior depending on the arguments which are passed to it.

#!/bin/bash
 
function init() {
 
	if [ "${1}" == "string1" ]; then 
			f_test1 
		elif [ "${1}" == "string2" ]; then 
			f_test2 
		elif [ "${1}" == "string3" ]; then
			f_test3 
	else
		echo "Please use an option: string1|string2|string3"	
fi
 
}
 
function f_test1() {
 
	echo "your argument is: string1"
 
}
function f_test2() {
 
	echo "your argument is: string2"
 
}
function f_test3() {
 
	echo "your argument is: string3"
 
}
 
init ${1}