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 4965828
#!/bin/bash 

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

display_usage() 
{ 
	echo "This script require arguments!" 
	echo -e "\nUsage:\n$0 [${argumentsRequired}] \n" 
} 

# 
echo "   Executing command: ${commandName}" 

echo "   You want to have ${argumentsLimit} and provide ${#}"
 
# if less than two arguments supplied, display usage 
	if [  $# -le ${ArgumentsLimit} ] 
	then 
		display_usage
		exit 1
	fi 
 
# check whether user had supplied -h or --help . If yes display usage 
	if [[ ( $# == "--help") ||  $# == "-h" ]] 
	then 
		display_usage
		exit 0
	fi 
 
# display usage if the script is not run as root user 
	if [[ $USER != "root" ]]; then 
		echo "This script must be run as root!" 
		exit 1
	fi 
 
echo "All good !!!"
HTTPS SSH

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