Skip to content

Instantly share code, notes, and snippets.

@TakahikoKawasaki
Last active December 20, 2015 04:48
Show Gist options
  • Save TakahikoKawasaki/6073017 to your computer and use it in GitHub Desktop.
Save TakahikoKawasaki/6073017 to your computer and use it in GitHub Desktop.
Bash script template like C
#!/bin/bash
#--------------------------------------------------
# Global Variables
#--------------------------------------------------
__v_verbose=0
__v_dry_run="false"
#--------------------------------------------------
# main
#--------------------------------------------------
__main()
{
# Parse the command line arguments of this script.
# <command> and [command-arguments>] are set to __v_command_line.
__parse_arguments "$@"
# Execute the <command>
__execute_command
}
#--------------------------------------------------
# parse_arguments
#--------------------------------------------------
__parse_arguments()
{
# Array of the arguments
local argv=("$@")
# Number of the arguments
local argc=${#argv[@]}
# The first non-option argument and its followings.
local -a remaining
# Flag to indicate remaining arguments should be processed
# as non-option arguments even if they start with a hyphen.
local no_following_options="false"
# Work variables
local i
# For each argument
for ((i = 0; i < argc; ++i))
do
# The argument at the position
local argument="${argv[$i]}"
# If the argument does not start with a hyphen.
if [ "${argument:0:1}" != "-" -o $no_following_options = "true" ]; then
# Append the argument as the last element of 'remaining'.
remaining[${#remaining[@]}]="$argument"
no_following_options="true"
continue
fi
case "$argument" in
# Help
-h | -help | --help )
# Shows help of this script and exits.
__help
;;
# Verbose mode.
-v | -verbose | --verbose )
# Increment the value of __v_verbose
((++__v_verbose))
;;
# Dry-run mode.
-d | -dry-run | --dry-run )
__v_dry_run="true"
;;
# Stop parsing remaining arguments as options.
-- )
no_following_options="true"
;;
# Unknown option
* )
# Shows an error message about the unknown option and exits.
__unknown_option "$argument"
;;
esac
done
__v_command_line=("${remaining[@]}")
}
#--------------------------------------------------
# help
#--------------------------------------------------
__help()
{
cat <<__HELP__
USAGE:
$0 [global-options] <command> [command-arguments]
GLOBAL OPTIONS:
-h | -help | --help
Shows this help and exits.
-v | -verbose | --verbose
Turns on the verbose mode.
-d | -dry-run | --dry-run
Turns on the dry-run mode.
COMMANDS:
test
__HELP__
exit 0
}
#--------------------------------------------------
# is_verbose
#--------------------------------------------------
__is_verbose()
{
if [ "$__v_verbose" -gt 0 ]; then
return 0
else
return 1
fi
}
#--------------------------------------------------
# is_dry_run
#--------------------------------------------------
__is_dry_run()
{
if [ "$__v_dry_run" = "true" ]; then
return 0
else
return 1
fi
}
#--------------------------------------------------
# echo_if_verbose
#--------------------------------------------------
__echo_if_verbose()
{
if __is_verbose; then
echo "$@"
fi
}
#--------------------------------------------------
# unknown_option
#--------------------------------------------------
__unknown_option()
{
local unknown="$1"
__error "Unknown option: $unknown"
exit 1
}
#--------------------------------------------------
# error
#--------------------------------------------------
__error()
{
local message
for message in "$@"
do
echo "ERROR: $message" >&2
done
}
#--------------------------------------------------
# execute_command
#--------------------------------------------------
__execute_command()
{
local command_argv=("${__v_command_line[@]}")
local command_argc=${#command_argv[@]}
if [ $command_argc -eq 0 ]; then
__error "No command is specified." "Try --help option."
exit 1
fi
local command=${command_argv[0]}
case "$command" in
# Supported commands.
test )
# Append "__command_".
command_argv[0]="__command_${command}"
# Execute the command.
"${command_argv[@]}"
;;
# Unknown command.
* )
__unknown_command "$command"
;;
esac
}
#--------------------------------------------------
# unknown_command
#--------------------------------------------------
__unknown_command()
{
local unknown="$1"
__error "Unknown command: $unknown"
exit 1
}
#--------------------------------------------------
# command_test
#--------------------------------------------------
__command_test()
{
echo "test $@"
}
#--------------------------------------------------
# S T A R T
#--------------------------------------------------
# Entry point of this script
__main "$@"
# Exit normally.
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment