Added build files to turn the bible into a book

This commit is contained in:
Dylan Araps
2018-06-20 12:40:31 +10:00
parent e0aadbde13
commit fe6e2cd195
21 changed files with 1310 additions and 1440 deletions

View File

@@ -1,150 +1,192 @@
# Conversion
# Other
## Convert a hex color to RGB
## Use `read` as an alternative to the `sleep` command
I was surprised to find out `sleep` is an external command and isn't a
built-in.
**CAVEAT:** Requires `bash` 4+
**Example Function:**
```sh
hex_to_rgb() {
# Usage: hex_to_rgb "#FFFFFF"
((r=16#${1:1:2}))
((g=16#${1:3:2}))
((b=16#${1:5:6}))
printf '%s\n' "$r $g $b"
read_sleep() {
# Usage: sleep 1
# sleep 0.2
read -rst "${1:-1}" -N 999
}
```
**Example Usage:**
```shell
$ hex_to_rgb "#FFFFFF"
255 255 255
read_sleep 1
read_sleep 0.1
read_sleep 30
```
## Check if a program is in the user's PATH
## Convert an RGB color to hex
```shell
# There are 3 ways to do this and you can use either of
# these in the same way.
type -p executable_name &>/dev/null
hash executable_name &>/dev/null
command -v executable_name &>/dev/null
# As a test.
if type -p executable_name &>/dev/null; then
# Program is in PATH.
fi
# Inverse.
if ! type -p executable_name &>/dev/null; then
# Program is not in PATH.
fi
# Example (Exit early if program isn't installed).
if ! type -p convert &>/dev/null; then
printf '%s\n' "error: convert isn't installed, exiting..."
exit 1
fi
```
## Get the current date using `strftime`
Bashs `printf` has a built-in method of getting the date which we can use
in place of the `date` command in a lot of cases.
**CAVEAT:** Requires `bash` 4+
**Example Function:**
```sh
rgb_to_hex() {
# Usage: rgb_to_hex "r" "g" "b"
printf '#%02x%02x%02x\n' "$1" "$2" "$3"
date() {
# Usage: date "format"
# See: 'man strftime' for format.
printf "%($1)T\\n" "-1"
}
```
**Example Usage:**
```shell
$ rgb_to_hex "255" "255" "255"
#FFFFFF
# Using above function.
$ date "%a %d %b - %l:%M %p"
Fri 15 Jun - 10:00 AM
# Using printf directly.
$ printf '%(%a %d %b - %l:%M %p)T\n' "-1"
Fri 15 Jun - 10:00 AM
# Assigning a variable using printf.
$ printf -v date '%(%a %d %b - %l:%M %p)T\n' '-1'
$ printf '%s\n' "$date"
Fri 15 Jun - 10:00 AM
```
## Generate a UUID V4
# Code Golf
**Example Function:**
## Shorter `for` loop syntax
```sh
uuid() {
# Usage: uuid
C="89ab"
```shell
# Tiny C Style.
for((;i++<10;)){ echo "$i";}
for ((N=0;N<16;++N)); do
B="$((RANDOM%256))"
# Undocumented method.
for i in {1..10};{ echo "$i";}
case "$N" in
6) printf '4%x' "$((B%16))" ;;
8) printf '%c%x' "${C:$RANDOM%${#C}:1}" "$((B%16))" ;;
# Expansion.
for i in {1..10}; do echo "$i"; done
3|5|7|9)
printf '%02x-' "$B"
;;
# C Style.
for((i=0;i<=10;i++)); do echo "$i"; done
```
*)
printf '%02x' "$B"
;;
esac
done
## Shorter infinite loops
```shell
# Normal method
while :; do echo hi; done
# Shorter
for((;;)){ echo hi;}
```
## Shorter function declaration
```shell
# Normal method
f(){ echo hi;}
# Using a subshell
f()(echo hi)
# Using arithmetic
# You can use this to assign integer values.
# Example: f a=1
# f a++
f()(($1))
# Using tests, loops etc.
# NOTE: You can also use while, until, case, (()), [[]].
f()if true; then echo "$1"; fi
f()for i in "$@"; do echo "$i"; done
```
## Shorter `if` syntax
```shell
# One line
# Note: The 3rd statement may run when the 1st is true
[[ "$var" == hello ]] && echo hi || echo bye
[[ "$var" == hello ]] && { echo hi; echo there; } || echo bye
# Multi line (no else, single statement)
# Note: The exit status may not be the same as with an if statement
[[ "$var" == hello ]] && \
echo hi
# Multi line (no else)
[[ "$var" == hello ]] && {
echo hi
# ...
printf '\n'
}
```
## Simpler `case` statement to set variable
We can use the `:` builtin to avoid repeating `variable=` in a case
statement. The `$_` variable stores the last argument of the last
successful command. `:` always succeeds so we can abuse it to store the
variable value.
**Example Usage:**
```shell
# Modified snippet from Neofetch.
case "$OSTYPE" in
"darwin"*)
: "MacOS"
;;
$ uuid
d5b6c731-1310-4c24-9fe3-55d556d44374
```
"linux"*)
: "Linux"
;;
## Progress bars
*"bsd"* | "dragonfly" | "bitrig")
: "BSD"
;;
This is a simple way of drawing progress bars without needing a for loop
in the function itself.
"cygwin" | "msys" | "win32")
: "Windows"
;;
**Example Function:**
*)
printf '%s\n' "Unknown OS detected, aborting..." >&2
exit 1
;;
esac
```sh
bar() {
# Usage: bar 1 10
# ^----- Elapsed Percentage (0-100).
# ^-- Total length in chars.
((elapsed=$1*$2/100))
# Finally, set the variable.
os="$_"
# Create the bar with spaces.
printf -v prog "%${elapsed}s"
printf -v total "%$(($2-elapsed))s"
printf '%s\r' "[${prog// /-}${total}]"
}
```
**Example Usage:**
```shell
for ((i=0;i<=100;i++)); do
# Pure bash micro sleeps (for the example).
(:;:) && (:;:) && (:;:) && (:;:) && (:;:)
# Print the bar.
bar "$i" "10"
done
printf '\n'
```
## Get the list of functions from your script
```sh
get_functions() {
# Usage: get_functions
IFS=$'\n' read -d "" -ra functions < <(declare -F)
printf '%s\n' "${functions[@]//declare -f }"
}
```
## Bypass shell aliases
```shell
# alias
ls
# command
# shellcheck disable=SC1001
\ls
```
## Bypass shell functions
```shell
# function
ls
# command
command ls
```
<!-- CHAPTER END -->