Skip to content

Instantly share code, notes, and snippets.

@neatshell
Last active May 31, 2024 14:19
Show Gist options
  • Save neatshell/5283811 to your computer and use it in GitHub Desktop.
Save neatshell/5283811 to your computer and use it in GitHub Desktop.
simple bash template that handles mandatory and optional arguments
#!/bin/bash
script="myscript"
#Declare the number of mandatory args
margs=2
# Common functions - BEGIN
function example {
echo -e "example: $script -m0 VAL -m1 VAL -o1 -o2 VAL"
}
function usage {
echo -e "usage: $script MANDATORY [OPTION]\n"
}
function help {
usage
echo -e "MANDATORY:"
echo -e " -m0, --mandatory0 VAL The desc of the mandatory0 parameter"
echo -e " -m1, --mandatory1 VAL The desc of the mandatory1 parameter\n"
echo -e "OPTION:"
echo -e " -o0, --optional1 The desc of the optional0 boolean parameter"
echo -e " -o1, --optional2 VAL The desc of the optional1 String parameter"
echo -e " -h, --help Prints this help\n"
example
}
# Ensures that the number of passed args are at least equals
# to the declared number of mandatory args.
# It also handles the special case of the -h or --help arg.
function margs_precheck {
if [ $2 ] && [ $1 -lt $margs ]; then
if [ $2 == "--help" ] || [ $2 == "-h" ]; then
help
exit
else
usage
example
exit 1 # error
fi
fi
}
# Ensures that all the mandatory args are not empty
function margs_check {
if [ $# -lt $margs ]; then
usage
example
exit 1 # error
fi
}
# Common functions - END
# Custom functions - BEGIN
# Put here your custom functions
# Custom functions - END
# Main
margs_precheck $# $1
marg0=
marg1=
oarg0="false"
oarg1="default"
# Args while-loop
while [ "$1" != "" ];
do
case $1 in
-m0 | --mandatory0 ) shift
marg0=$1
;;
-m1 | --mandatory1 ) shift
marg1=$1
;;
-o0 | --optional0 ) oarg0="true"
;;
-o1 | --optional1 ) shift
oarg1=$1
;;
-h | --help ) help
exit
;;
*)
echo "$script: illegal option $1"
usage
example
exit 1 # error
;;
esac
shift
done
# Pass here your mandatory args for check
margs_check $marg0 $marg1
# Your stuff goes here
@pomkos
Copy link

pomkos commented Jun 29, 2021

Excellent bash script template, thanks a lot!

@20NE
Copy link

20NE commented Jan 31, 2022

Thanks, good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment