Bash scripts and libraries to make your life easier.
Installation with package manager
The easiest way to install simply-bash is via basher:
$ basher install hagenw/simply-bash
Afterwards all binaries are directly available and libraries can be included via
include hagenw/simply-bash lib/math.sh
Installation from repository
To manually install it from this repository, clone it with:
git clone https://github.com/hagenw/simply-bash.git ~/git/simply-bash
Replace ~/git/simply-bash
with your desired directory.
In order to use it you have to source the simply-bash.sh
file:
source ~/git/simply-bash/simply-bash.sh
To automate this, add it to your .bashrc
. Afterwards the libraries can be
included with:
include lib/math.sh
Usage
Every script comes with a help message explaining all of it options, e.g. is --help
. In the following only short examples are shown to demonstrate the main
purpose of the scripts. The included functions of the libraries are all shown.
is
Replacement for test command, included from is.sh. Example:
var=123
if is equal $var 123.0; then
echo "it just works"
fi
if is not a substring $var "foobar"; then
echo "and it's easy to read"
fi
filesize
Return the file size in bytes. Example:
$ filesize $(which filesize)
47
randomstring
Return a random alphanumeric string. Example:
$ randomstring --length 8
75v4dvoH
tmpfile
Create a temp file and return its name. Example:
$ tmpfile --basename myprog
/tmp/myprog-Gsfg6p
warning
and error
Show the provided message and copy it to stderr with >&2
.
Examples:
$ warning "Problem"
Warning: Problem
$ echo $?
0
$ error "Failed"
Error: Failed
$ echo $?
1
math.sh
Provide mathematical expressions for real valued calculations in your bash scripts.
Usage:
source lib/math.sh
math::plus 1 2 # 3
math::minus 3 2 # 1
math::divide 6 2 # 3.00000000
math::multiply 3 2 # 6
math::power 2 3 # 8
math::sqrt 9 3 # 3.00000000
math::exp 0 # 1.00000000
math::sin $PI # 0
math::cos $PI # -1.00000000
math::tan $PI # 0
math::floor 0.1 # 0
math::ceil 0.1 # 1
math::round 0.5 # 1
math::abs -1 # 1
units.sh
Convert between pixels, points, and inches.
Usage:
source lib/units.sh
res_in_ppi=300
units::pt_to_inch 300 # 4.16666666
units::inch_to_px 4 $res_in_ppi # 1200
units::pt_to_px 20 $res_in_ppi # 83.33333100