Skip to content

Instantly share code, notes, and snippets.

@Ryllaz
Last active June 30, 2023 11:45
Show Gist options
  • Save Ryllaz/07447319d5a5c429ec8eb077a28cfced to your computer and use it in GitHub Desktop.
Save Ryllaz/07447319d5a5c429ec8eb077a28cfced to your computer and use it in GitHub Desktop.
Template for bootstrap script for any project
#!/bin/bash
SCRIPT_DIR="$(dirname "$(readlink -f "${BASH_SOURCE}")")"
# ----------------- START OF THE USER FUNCTIONS SECTION -----------------
my_first_command_help() {
echo "Build project"
}
my_first_command() { ## It is my first command
echo "doing something important"
}
# ----------------- END OF THE USER FUNCTIONS SECTION -----------------
# Render help message according to comments for functions
__help() {
echo "Usage: $BASH_SOURCE [COMMAND] [arg1 arg2 ...]"
echo "Available commands:"
grep -Eo "^(.*)\(\)(.*)\#\#(.*)$" $BASH_SOURCE | sort | awk 'BEGIN { FS = "\\\(\\\).*## " } { printf "\033[36m%-30s\033[0m %s\n", $1, $2 }'
}
# Check function existing
__function_exists() {
declare -f -F "$1" > /dev/null
return $?
}
# Check if function exists and run it
if __function_exists "$1"; then
if [ "--help" = "${@:2}" ]; then
help_func=$1"_help"
if __function_exists $help_func; then
$help_func
else
echo "Function '$1' does not have help message"
fi
else
$1 "${@:2}"
fi
else
__help
fi
@Ryllaz
Copy link
Author

Ryllaz commented Jun 30, 2023

  1. cd path-to-script-dir/
  2. chmod +x ./run - allow execute the script
  3. ./run help - get help message
  4. ./run my_first_command - execute command sample

@Ryllaz
Copy link
Author

Ryllaz commented Jun 30, 2023

For new commands you need add this data (carefully check all spaces and # symbols):

my_second_command_help() {
    echo "Build project"
}
my_second_command() { ## It is my second command
    echo "doing something VERY important"
}

Function with _help suffix not required, you can skip it if you do not need extra documenting for command.

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