Added build files to turn the bible into a book
This commit is contained in:
@@ -1,193 +0,0 @@
|
||||
# Other
|
||||
|
||||
## 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
|
||||
read_sleep() {
|
||||
# Usage: sleep 1
|
||||
# sleep 0.2
|
||||
read -rst "${1:-1}" -N 999
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
read_sleep 1
|
||||
read_sleep 0.1
|
||||
read_sleep 30
|
||||
```
|
||||
|
||||
## Check if a program is in the user's PATH
|
||||
|
||||
```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`
|
||||
|
||||
Bash’s `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
|
||||
date() {
|
||||
# Usage: date "format"
|
||||
# See: 'man strftime' for format.
|
||||
printf "%($1)T\\n" "-1"
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
# 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
|
||||
|
||||
**Example Function:**
|
||||
|
||||
```sh
|
||||
uuid() {
|
||||
# Usage: uuid
|
||||
C="89ab"
|
||||
|
||||
for ((N=0;N<16;++N)); do
|
||||
B="$((RANDOM%256))"
|
||||
|
||||
case "$N" in
|
||||
6) printf '4%x' "$((B%16))" ;;
|
||||
8) printf '%c%x' "${C:$RANDOM%${#C}:1}" "$((B%16))" ;;
|
||||
|
||||
3|5|7|9)
|
||||
printf '%02x-' "$B"
|
||||
;;
|
||||
|
||||
*)
|
||||
printf '%02x' "$B"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
printf '\n'
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
$ uuid
|
||||
d5b6c731-1310-4c24-9fe3-55d556d44374
|
||||
```
|
||||
|
||||
## Progress bars
|
||||
|
||||
This is a simple way of drawing progress bars without needing a for loop
|
||||
in the function itself.
|
||||
|
||||
**Example Function:**
|
||||
|
||||
```sh
|
||||
bar() {
|
||||
# Usage: bar 1 10
|
||||
# ^----- Elapsed Percentage (0-100).
|
||||
# ^-- Total length in chars.
|
||||
((elapsed=$1*$2/100))
|
||||
|
||||
# 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 -->
|
||||
|
||||
Reference in New Issue
Block a user