Skip to content

script

A library of script related functions.

lb_show_usage ${0}

Show a script usage message.

Parameters:
Name Type Description Default
${0} string The name of the script required
Variables:
Name Type Description Default
LIBBASH_USAGE_MSG Environmental The suffix of the usage message [OPTIONS] ARGS
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/script"

# shellcheck disable=SC2034
LIBBASH_USAGE_MSG="my custom usage message"

function main() {
  printf "LIBBASH_USAGE_MSG: %s\n" "${LIBBASH_USAGE_MSG}"
  # lb_show_usage "${0}"
  # lb_show_version "${0}" "0.1.0"
  lb_usage_error "${0}"
}

main "${@}"
Show source code in script
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

LIBBASH_USAGE_MSG=${LIBBASH_USAGE_MSG:-"[OPTIONS] ARGS"}

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

# Show a usage message
function lb_show_usage(){
  printf "Usage: %s %s\n" "${1}" "${LIBBASH_USAGE_MSG}"
}

function lb_show_version(){
  printf "%s version %s\n" "${1}" "${2}"
  exit 0
}

function lb_usage_error() {
  lb_show_usage "${1}"
  printf "\nTry %s -h for more options.\n" "${1}" >&2
  exit 1
}

Note

The message has a prefix of Usage:

lb_show_version version

Show the script version and exit with a status of 0.

Parameters:
Name Type Description Default
version string The version of the script required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/script"

# shellcheck disable=SC2034
LIBBASH_USAGE_MSG="my custom usage message"

function main() {
  printf "LIBBASH_USAGE_MSG: %s\n" "${LIBBASH_USAGE_MSG}"
  # lb_show_usage "${0}"
  # lb_show_version "${0}" "0.1.0"
  lb_usage_error "${0}"
}

main "${@}"
Show source code in script
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

LIBBASH_USAGE_MSG=${LIBBASH_USAGE_MSG:-"[OPTIONS] ARGS"}

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

# Show a usage message
function lb_show_usage(){
  printf "Usage: %s %s\n" "${1}" "${LIBBASH_USAGE_MSG}"
}

function lb_show_version(){
  printf "%s version %s\n" "${1}" "${2}"
  exit 0
}

function lb_usage_error() {
  lb_show_usage "${1}"
  printf "\nTry %s -h for more options.\n" "${1}" >&2
  exit 1
}

lb_usage_error ${0}

Show the script usage and help options and exit with a status of 1.

Parameters:
Name Type Description Default
${0} string The name of the script required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/script"

# shellcheck disable=SC2034
LIBBASH_USAGE_MSG="my custom usage message"

function main() {
  printf "LIBBASH_USAGE_MSG: %s\n" "${LIBBASH_USAGE_MSG}"
  # lb_show_usage "${0}"
  # lb_show_version "${0}" "0.1.0"
  lb_usage_error "${0}"
}

main "${@}"
Show source code in script
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

LIBBASH_USAGE_MSG=${LIBBASH_USAGE_MSG:-"[OPTIONS] ARGS"}

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

# Show a usage message
function lb_show_usage(){
  printf "Usage: %s %s\n" "${1}" "${LIBBASH_USAGE_MSG}"
}

function lb_show_version(){
  printf "%s version %s\n" "${1}" "${2}"
  exit 0
}

function lb_usage_error() {
  lb_show_usage "${1}"
  printf "\nTry %s -h for more options.\n" "${1}" >&2
  exit 1
}