Skip to content

helpers

A library of libbash helper functions.

Note

This library is automatically loaded by the init library.

Note

This library automatically loads the logging library.

lb_die msg

Send a fatal message and exit with a status of 1.

Parameters:
Name Type Description Default
msg string The message to be displayed required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
# helpers does not need to be sourced because it is automatically sourced by init.

function check_args(){
  lb_check_args_num "${@}" 2
}

function main() {
  check_args "one" "two" "three"
  # lb_fail bar
  # lb_die foo
}

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

# Check if logging has been loaded
# shellcheck source=/dev/null
[[ "${LIBBASH_LOG_LEVEL:-}" ]] || source "${LIBBASH_DIR}/logging"

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

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

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

function lb_die(){
  lb_fatalln "${@}"
  exit 1
}

function lb_fail(){
  lb_errorln "${@}"
  return 1
}

function lb_check_args_num(){
  # Number of arguments passed
  n=$(("${#@}"-1))
  # last argument
  i="${*: -1}"
  if (( i != n )); then
    if (( i == 1 )); then
      arg="arg"
    else
      arg="args"
    fi
    lb_fail "$(printf "expected %s %s but got %s" "${i}" "${arg}" "${n}")"
  fi
}

lb_fail msg

Send an error message and return a status of 1.

Parameters:
Name Type Description Default
msg string The message to be displayed required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
# helpers does not need to be sourced because it is automatically sourced by init.

function check_args(){
  lb_check_args_num "${@}" 2
}

function main() {
  check_args "one" "two" "three"
  # lb_fail bar
  # lb_die foo
}

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

# Check if logging has been loaded
# shellcheck source=/dev/null
[[ "${LIBBASH_LOG_LEVEL:-}" ]] || source "${LIBBASH_DIR}/logging"

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

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

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

function lb_die(){
  lb_fatalln "${@}"
  exit 1
}

function lb_fail(){
  lb_errorln "${@}"
  return 1
}

function lb_check_args_num(){
  # Number of arguments passed
  n=$(("${#@}"-1))
  # last argument
  i="${*: -1}"
  if (( i != n )); then
    if (( i == 1 )); then
      arg="arg"
    else
      arg="args"
    fi
    lb_fail "$(printf "expected %s %s but got %s" "${i}" "${arg}" "${n}")"
  fi
}

lb_check_args_num ${@} num

Return 0 if the number of arguments passed matches num.

Parameters:
Name Type Description Default
${@} string The arguments to check the number of required
num int The number of arguments to check for required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
# helpers does not need to be sourced because it is automatically sourced by init.

function check_args(){
  lb_check_args_num "${@}" 2
}

function main() {
  check_args "one" "two" "three"
  # lb_fail bar
  # lb_die foo
}

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

# Check if logging has been loaded
# shellcheck source=/dev/null
[[ "${LIBBASH_LOG_LEVEL:-}" ]] || source "${LIBBASH_DIR}/logging"

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

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

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

function lb_die(){
  lb_fatalln "${@}"
  exit 1
}

function lb_fail(){
  lb_errorln "${@}"
  return 1
}

function lb_check_args_num(){
  # Number of arguments passed
  n=$(("${#@}"-1))
  # last argument
  i="${*: -1}"
  if (( i != n )); then
    if (( i == 1 )); then
      arg="arg"
    else
      arg="args"
    fi
    lb_fail "$(printf "expected %s %s but got %s" "${i}" "${arg}" "${n}")"
  fi
}