Toolbox

Toolbox is a collection of Bash modules that help you write maintainable Bash scripts. At the heart of toolbox is the include() function, which loads modules from system-wide and user-wide locations, somewhat similar to Python’s import statement. For example, the following code shares the temperature of thermal zone 0 through a pub-sub channel with other scripts.

#!/bin/bash

temp_publisher() {
	local zone="$1"
	local topic="$2"
	local interval="$3"

	local endpoint
	local sensor_path
	local temp

	sensor_path="/sys/devices/virtual/thermal/$zone/temp"

	# IPC endpoints can be used for pub-sub, 1-to-1, or 1-to-many communication
	if ! endpoint=$(ipc_endpoint_open); then
		return 1
	fi

	# Will run until stopped with the implicit "-s"/"--stop" argument
	while inst_running; do
		if ! temp=$(< "$sensor_path"); then
			log_error "Could not read $sensor_path"
			continue
		fi

		if ! ipc_endpoint_publish "$endpoint" "$topic" "$temp"; then
			log_error "Could not publish $temp on $topic (endpoint: $endpoint)"
			continue
		fi

		sleep "$interval"
	done

	ipc_endpoint_close "$endpoint"

	return 0
}

main() {
	local zone
	local topic
	local interval

	#           .----------------------------------------- Short name "-z"
	#           |   .------------------------------------- Long name "--zone"
	#           |   |          .-------------------------- v=followed by a value, r=required
	#           |   |          |    .--------------------- Default value
	#           |   |          |    |               .----- Description (for -h / --help)
	#           |   |          |    |               |  .-- Value must match this RegEx
	#           |   |          |    |               |  |   (omit to disable validation)
	#           |   |          |    |               |  '--------------------------.
	#           |   |          |    |               |                             |
	#           v   v          v    v               v                             v
	opt_add_arg "z" "zone"     "v"  "thermal_zone0" "The thermal zone to monitor" '^[^/]+$'
	opt_add_arg "t" "topic"    "rv" ""              "The topic to publish to"
	opt_add_arg "i" "interval" "v"  30              "Interval between messages"   '^[0-9]+$'

	if ! opt_parse "$@"; then
		# Missing/unrecognized arguments or invalid values
		return 1
	fi

	# retrieve value using long option name
	zone=$(opt_get "zone")
	topic=$(opt_get "topic")
	interval=$(opt_get "interval")

	# daemonize temp_publisher() - if you don't need a singleton, use inst_start()
	if ! inst_singleton temp_publisher "$zone" "$topic" "$interval"; then
		return 1
	fi

	return 0
}

{
	if ! . toolbox.sh; then
		exit 1
	fi

	if ! include "log" "opt" "inst" "ipc"; then
		exit 1
	fi

	main "$@"
	exit "$?"
}

Installation using apt

If you are using a Debian-based distribution, you can install toolbox through apt. First, import the GPG key used to sign packages in the repository and make sure you have apt-transport-https installed.

# wget -O - -- https://deb.m10k.eu/deb.m10k.eu.gpg.key | apt-key add -
# apt-get install apt-transport-https

Then add the following line to your /etc/apt/sources.lst and run apt-get update.

deb https://deb.m10k.eu stable main

Now you can install toolbox using the following command.

# apt-get install toolbox

Installation from the sources

If you would prefer to install toolbox from the sources, run the following commands.

$ git clone https://git.corax.cc/toolbox
$ cd toolbox
$ sudo make install

For the latest (and slightly more unstable) version, switch to the unstable branch using git checkout unstable or specify the branch when cloning the repository.

$ git clone http://git.corax.cc/toolbox -b unstable