Sometimes it is needed to check what user is executing the bash script and whether the user supplied all required arguments:

#!/bin/bash 

 
display_usage() { 
	echo "This script must be run with super-user privileges." 
	echo -e "\nUsage:\n__g5_token5f05b09e64559 [arguments] \n" 
	} 
# if less than two arguments supplied, display usage 
	if [  $# -le 1 ] 
	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 !!!"

OUTPUT:

$ ./script.sh 1 
This script must be run with super-user privileges.

Usage: ./script.sh [arguments] 

$ su
Password: 
# ./script.sh 1 2
All good !!!
FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in: