units.sh

units – conversion program

This library provides a function to comfortable use GNU units in a shell script. Thus, it gives you the possibility to do calculations with decimal and exponential numbers with and without units in shell scripts.

Dependencies

  • Obviously it depends on units ;)

Usage

units_clc EQUATION
Returns the result of the EQUATION. You can use here the following build-in functions: sin, cos, tan, ln, log, log2, exp, acos, atan. The EQUATION should be enclosed by quotation marks.
units_clc [-n|-u] EQUATION_with_UNITS|VALUE_with_UNITS [UNITS]
Returns the result of the EQUATION_with_UNITS or the VALUE_with_UNITS in SI units by default. With the optionial second argument [UNITS] you define the desired units of the result. You can enforce (-u) or disable (-n) the output of the units in the result string.

Variables

units_with_units
(default: 1) Return units in result string by default (1) or supress the units (0).

Examples

Calculation

To make a simple calculation:

#!/bin/sh
. libinclude.sh
import units
units_clc "(45.88*2^4-1)/1.345e3"

the script will return 0.54504089.

Calculate with units

Do a calculation with values and units:

units_clc "2.3 km + 236.5m"
units_clc "2.3 km + 236.5m" 'km'

will return ‘2536.5 m’ and ‘2.5365 km’.

Convert between units

units_clc '56*kg/m3' 'g/cm3'
units_clc -n '56*kg/m3' 'g/cm3'

returns ‘0.056 g/cm3’ and ‘0.056’

Get definition

units_clc -u '255.67e-6 J'

returns the value in SI units: ‘0.00025567 kg m^2 / s^2’