Snippets

Víctor Goñi Sanz Bash tutorial 2 - Usage

You are viewing an old version of this snippet. View the current version.
Revised by Víctor Goñi Sanz 6d6753f
#!/bin/bash 

# Check spell http://www.shellcheck.net/# online, or download, to test!

# Bash have variable when call an executable, $0 = CommandName, $1..$N = Arguments
argumentsLimit=1
commandName=$(basename "$0") 
commandPath=${0%/*}
argumentsRequired="first second"

###### Function declaration
# My first function, display usage info
f_display_usage() 
{ 
	echo "This script require arguments!" 
	echo -e "\nUsage:\n$0 [${argumentsRequired}] \n" 
} 
# My second function, check if
f_check_correct_parameters()
{
# if less than ${argumentsLimit} arguments supplied, display usage 
	if [  $# -le ${argumentsLimit} ] 
	then 
		display_usage
		exit 1
	fi 
}
f_main()
{
echo "------- My second script begins execution..."
echo "--------------------------------------------"
echo "   Executing command: ${commandName}" 
echo "   At path: ${commandPath}"
echo "   You want to have ${argumentsLimit} and provide ${#}"
 
f_check_correct_parameters
 
# check whether user had supplied -h or --help . If yes display usage 
	if [[ ( $# == "--help") ||  $# == "-h" ]] 
	then 
		display_usage
		exit 0
	fi 

echo "------- My second script end now!"
echo "---------------------------------"
}
###### End Function declaration

# Call main
f_main
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.