Shell Helpers

Constants

Version of this script

  • SCRIPT_HELPER_VERSION: 1.0

Log Levels

  • DEBUG: 0

  • D: 0

  • INFO: 1

  • I: 1

  • WARN: 2

  • W: 2

  • ERROR: 3

  • E: 3

Functions

command_present

Check if a command (or alias/function) is available.

Usage: command_present bin

  • bin: name of command

for cmd in 'grep' 'xterm'; do
    command_present "$cmd" || die "$cmd not found"
done

detect_all

Run all detection routines.

Usage: detect_all

Returns: true if all detections succeed

detect all || die 'Detections failed'

detect_arch

Detect architecture of running system.

Usage: detect_arch

Returns: true if detection succeeds

Exports: H_ARCH

detect_arch || die 'Architecture is not supported'

detect_codename

Detect codename of running system.

Usage: detect_codename

Returns: true if detection succeeds

Exports: H_CODENAME

detect_codename || die 'Distro codename not found'

detect_distro

Detect distribution of running system.

Usage: detect_distro

Returns: true if detection succeeds

Exports: H_DISTRO

detect_distro || die 'Distribution is not supported'

detect_init

Detect init of running system.

Usage: detect_init

Returns: true if detection succeeds

Exports: H_INIT

detect_init || die 'Distro is not supported'

detect_kernel

Detect kernel of running system.

Usage: detect_kernel

Returns: true if detection succeeds

Exports: H_KERNEL

detect_kernel || die 'Kernel is not supported'

detect_release

Detect distro release of running system.

Usage: detect_release

Returns: true if detection succeeds

Exports: H_RELEASE

detect_release || die 'Release is not supported'

detect_virtual

Attempts to detect virtualization platform of running system

Usage: detect_virtual

Exports: H_VIRTUAL (H_VIRTUAL=’’ if no platform detected)

detect_virtual

die

Print a formatted (critical) message and exit with status.

Usage: die [exit_status] message

  • exit_status: exit code to use with script termination (default: 1)

  • message: message to print before terminating script execution

[ -n "$foo" ] || die 255 '$foo not defined'

ends_with

Check if string ends with another string

Usage: ends_with key str

  • key: string to search for

  • str: ascii string to search

Returns: true if str ends with key

ends_with 'init' "$input" || die 'invalid input'
low="$(to_lower "$up")"

helper_version

Return true if script version is between min/max (‘dev’ == git_HEAD).

Usage: helper_version min [max]

  • min: minimum supported version

  • max: maximum supported version

Returns: true if script version is between min and [optionally:max]

helper_version '1.0.0' '1.999' ||
    die 'Helper version 1.x required!'

in_string

Check if a value is present in a string.

Usage: in_string needle haystack

  • needle: value to search for

  • haystack: string to be searched

Returns: true if needle was found

in_string 'fuz' 'abcfuz123' && echo 'found some fuz'

internet_reachable

A best-effort attempt to check if the internet is accepssible.

Usage: internet_accessible

Defaults:

  • env[PING_HOST]: google.com

  • env[PING_ADDR]: 8.8.8.8

Returns: true if internet was probed

internet_accessible || die 'Cannot reach the internet'

is_arch

Evaluate if running arch matches provided argument(s).

Usage: is_arch match [match..]

  • match: any string to test against (debian, ubuntu, etc.)

Returns: true if running arch matches a provided ‘match’

is_arch 'i386' 'x86_64' || die 'Architecture not supported'

is_codename

Evaluate if running codename matches provided argument(s).

Usage: is_codename match [match..]

  • match: any string to test against (buster, catalina, etc.)

Returns: true if running codename matches a provided ‘match’

is_codename 'buster' || die 'Only Debian buster is supported'

is_distro

Evaluate if running distribution matches provided argument(s).

Usage: is_distro match [match..]

  • match: any string to test against (debian, ubuntu, etc.)

Returns: true if running distro matches a provided ‘match’

is_distro 'debian' || die 'Only Debian is supported'

is_false

Evaluate if a given string is not true-like.

Usage: is_false str

  • str: any string to test against

Returns: true if ($str != 0) or (lower($str) != true)

is_false 'lies' || die 'I am too gullible'

is_init

Evaluate if running init matches provided argument(s).

Usage: is_init match [match..]

  • match: any string to test against (sysv, systemd, etc.)

Returns: true if running init matches a provided ‘match’

is_init 'sysv' || die 'Only sysv is supported'

is_int

Evaluate if a given string is a int.

Usage: is_int str

  • str: any string to test against

Returns: true if str is an integer

is_int "$foo" || die 'Argument must be an integer.'

is_kernel

Evaluate if running kernel matches provided argument(s).

Usage: is_kernel match [match..]

  • match: any string to test against (Linux, BSD, etc.)

Returns: true if running kernel matches a provided ‘match’

is_kernel 'Linux' 'BSD' || die 'Only Linux/BSD are supported'

is_release

Evaluate if running distro release matches provided argument.

Usage: is_release match [match..]

  • match: any string to test against (12.04, 10.1, etc.)

Returns: true if running release matches a provided ‘match’

is_release '14.04' '16.04' '18.04' '20.04' ||
    die 'Only non-EOL LTS releases are supported'

is_true

Evaluate if a given string is true-like.

Usage: is_true str

  • str: any string to test against

Returns: true if ($str is 0) or (lower($str) == true)

is_true "$foo" || die "\$foo said it isn't so..."

is_virtual

Evaluate if the current system is running on a virtualization platform.

Usage: is_virt [type..]

  • type: if provided, check if virt platform matches at least one type

Returns: true if virtual OR if H_VIRT==type

is_virtual && die 'Must be run on physical hardware'
is_virtual 'kvm' || die 'Only KVM is supported'

lock

Manage a lock file.

Usage: lock operation [key]

  • operation: acquire, destroy

  • key: any unique value to identify this script (default: $0)

Returns: true if success

lock aquire "$0" || die 'Unable to grab lock.'
[...]
lock destroy "$0"

log

Print a formatted message if env[LOG_LEVEL] >= level.

Usage: log level message

  • level: 0:debug, 1:info, *2:warn, 3:error

  • message: any string of text to be sent to stdout

Defaults:

  • env[LOG_LEVEL]: 2

log "$INFO" 'Capturing quantum stabilizer'
capture_qs || die 'Capture failed!'

replace_substring

Find/replace substrings in a string.

Usage: replace_substring input find repl

  • input: full string to be manipulated

  • find: key to search for

  • repl: what to replace “key” with

Prints: modified string

log "$E" "$(replace_substring "$err" "$secret" '****')"

starts_with

Check if string starts with another string

Usage: starts_with key str

  • key: string to search for

  • str: ascii string to search

Returns: true if str starts with key

starts_with 'init' "$input" || die 'invalid input'
low="$(to_lower "$up")"

to_lower

Convert string from [:upper:] to [:lower:].

Usage: to_lower

  • str: any ascii string to convert

Prints: string in lower

low="$(to_lower "$up")"

to_upper

Convert string from [:lower:] to [:upper:].

Usage: to_upper

  • str: any ascii string to convert

Prints: string in uppercase

up="$(to_upper "$low")"

version_compare

Compare two versions.

Usage: version_compare version1 operator version2

  • operator:

    • -lt, <, lesser_than

    • -le, <=, lesser_than_or_equal

    • -gt, >, greater_than

    • -ge, >=, greater_than_or_equal

    • -eq, ==, equal

    • -ne, !=, not_equal

  • version{1,2}: arbitrary version strings to compare

Version Format: [0-9]+($VERSION_SEPARATOR[0-9]+)* (i.e. 90, 1.2.3.4)

Returns: true if comparison statement is correct

version_compare "$_version" '-lt' '1.0' ||
    die 'Unexpected (pre-release) version found'