From d2c42689d88f635ab6f3bddb82af616032b6ea97 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Wed, 20 Jun 2018 16:00:33 +1000 Subject: [PATCH] update manuscript --- manuscript/chapter0.txt | 8 ++++---- manuscript/chapter1.txt | 6 +++--- manuscript/chapter10.txt | 4 ++-- manuscript/chapter11.txt | 6 +++--- manuscript/chapter12.txt | 4 ++-- manuscript/chapter13.txt | 4 ++-- manuscript/chapter14.txt | 8 +------- manuscript/chapter15.txt | 2 +- manuscript/chapter16.txt | 8 ++++---- manuscript/chapter17.txt | 14 ++++++-------- manuscript/chapter2.txt | 4 ++-- manuscript/chapter3.txt | 6 +++--- manuscript/chapter4.txt | 7 +++---- manuscript/chapter5.txt | 2 +- manuscript/chapter6.txt | 2 +- manuscript/chapter7.txt | 4 ++-- manuscript/chapter8.txt | 4 ++-- manuscript/chapter9.txt | 2 +- 18 files changed, 43 insertions(+), 52 deletions(-) diff --git a/manuscript/chapter0.txt b/manuscript/chapter0.txt index b7200f3..fe58b4d 100644 --- a/manuscript/chapter0.txt +++ b/manuscript/chapter0.txt @@ -1,10 +1,10 @@ -# Introduction +# FOREWORD -A collection of pure `bash` alternatives to external processes and programs. The `bash` scripting language is more powerful than people realise and you can accomplish most tasks without the need or dependency of external programs. +A collection of pure `bash` alternatives to external processes and programs. The `bash` scripting language is more powerful than people realise and most tasks can be accomplished without the need for or dependence on external programs. -Calling an external process in `bash` is expensive and excessive use will cause a noticeable slowdown. By sticking to built-in methods (*where possible*) your scripts and programs will be faster, require less dependencies and you'll gain a better understanding of the language itself. +Calling an external process in `bash` is expensive and excessive use will cause a noticeable slowdown. Scripts and programs written using built-in methods (*where applicable*) will be faster, require less dependencies and afford a better understanding of the language itself. -The contents of this book provide a reference for solving the problems encountered when writing programs and scripts in `bash`. The examples are in function format showcasing how to incorporate these solutions into your code. +The content of this book provides a reference for solving problems encountered when writing programs and scripts in `bash`. Examples are in function format showcasing how to incorporate these solutions into code. diff --git a/manuscript/chapter1.txt b/manuscript/chapter1.txt index 37a625f..1f76b0c 100644 --- a/manuscript/chapter1.txt +++ b/manuscript/chapter1.txt @@ -1,4 +1,4 @@ -# Strings +# STRINGS ## Trim leading and trailing white-space from string @@ -353,7 +353,7 @@ if [[ "$var" == sub_string* ]]; then printf '%s\n' "var starts with sub_string." fi -# Inverse (var doesn't start with sub_string). +# Inverse (var does not start with sub_string). if [[ "$var" != sub_string* ]]; then printf '%s\n' "var does not start with sub_string." fi @@ -366,7 +366,7 @@ if [[ "$var" == *sub_string ]]; then printf '%s\n' "var ends with sub_string." fi -# Inverse (var doesn't start with sub_string). +# Inverse (var does not start with sub_string). if [[ "$var" != *sub_string ]]; then printf '%s\n' "var does not end with sub_string." fi diff --git a/manuscript/chapter10.txt b/manuscript/chapter10.txt index 5fc7c0c..e7e3f0b 100644 --- a/manuscript/chapter10.txt +++ b/manuscript/chapter10.txt @@ -1,4 +1,4 @@ -# Arithmetic +# ARITHMETIC ## Simpler syntax to set variables @@ -16,7 +16,7 @@ ((var=var2*arr[2])) ``` -## Ternary tests +## Ternary Tests ```shell # Set the value of var to var2 if var2 is greater than var. diff --git a/manuscript/chapter11.txt b/manuscript/chapter11.txt index fd3dfd7..3d91ecb 100644 --- a/manuscript/chapter11.txt +++ b/manuscript/chapter11.txt @@ -1,8 +1,8 @@ -# Traps +# TRAPS -Traps allow you to execute code on various signals. In `pxltrm` I'm using traps to redraw the user interface on window resize. Another use case is cleaning up temporary files on script exit. +Traps allow a script to execute code on various signals. In [pxltrm](https://github.com/dylanaraps/pxltrm) (*a pixel art editor written in bash*) traps are used to redraw the user interface on window resize. Another use case is cleaning up temporary files on script exit. -These `trap` lines should be added near the start of your script so any early errors are also caught. +Traps should be added near the start of scripts so any early errors are also caught. **NOTE:** For a full list of signals, see `trap -l`. diff --git a/manuscript/chapter12.txt b/manuscript/chapter12.txt index 81071a2..3e49886 100644 --- a/manuscript/chapter12.txt +++ b/manuscript/chapter12.txt @@ -1,8 +1,8 @@ -# Performance +# PERFORMANCE ## Disable Unicode -If your script doesn't require unicode, you can disable it for a speed boost. Results may vary but I've seen an improvement in Neofetch and some other smaller programs. +If unicode is not required, it can be disabled for a performance increase. Results may vary however there have been noticeable improvements in [neofetch](https://github.com/dylanaraps/neofetch) and other programs. ```shell # Disable unicode. diff --git a/manuscript/chapter13.txt b/manuscript/chapter13.txt index d3857db..e03af79 100644 --- a/manuscript/chapter13.txt +++ b/manuscript/chapter13.txt @@ -1,4 +1,4 @@ -# Obsolete Syntax +# OBSOLETE SYNTAX ## Shebang @@ -34,7 +34,7 @@ var="$(command "$(command)")" ## Function Declaration -Don't use the `function` keyword, it reduces compatibility with older versions of `bash`. +Do not use the `function` keyword, it reduces compatibility with older versions of `bash`. ```shell # Right. diff --git a/manuscript/chapter14.txt b/manuscript/chapter14.txt index f743554..2aaaa32 100644 --- a/manuscript/chapter14.txt +++ b/manuscript/chapter14.txt @@ -1,10 +1,4 @@ -# Internal Variables - -**NOTE**: This list does not include every internal variable (*You can -help by adding a missing entry!*). - -For a complete list, see: -http://tldp.org/LDP/abs/html/internalvariables.html +# INTERNAL VARIABLES ## Get the location to the `bash` binary diff --git a/manuscript/chapter15.txt b/manuscript/chapter15.txt index ca647b9..9ebc228 100644 --- a/manuscript/chapter15.txt +++ b/manuscript/chapter15.txt @@ -1,4 +1,4 @@ -# Information about the terminal +# INFORMATION ABOUT THE TERMINAL ## Get the terminal size in lines and columns (*from a script*) diff --git a/manuscript/chapter16.txt b/manuscript/chapter16.txt index 98e8e64..3aad5d6 100644 --- a/manuscript/chapter16.txt +++ b/manuscript/chapter16.txt @@ -1,4 +1,4 @@ -# Conversion +# CONVERSION ## Convert a hex color to RGB @@ -42,7 +42,7 @@ $ rgb_to_hex "255" "255" "255" ``` -# Code Golf +# CODE GOLF ## Shorter `for` loop syntax @@ -80,13 +80,13 @@ f(){ echo hi;} f()(echo hi) # Using arithmetic -# You can use this to assign integer values. +# This can be used to assign integer values. # Example: f a=1 # f a++ f()(($1)) # Using tests, loops etc. -# NOTE: You can also use ‘while’, ‘until’, ‘case’, ‘(())’, ‘[[]]’. +# NOTE: ‘while’, ‘until’, ‘case’, ‘(())’, ‘[[]]’ can also be used. f()if true; then echo "$1"; fi f()for i in "$@"; do echo "$i"; done ``` diff --git a/manuscript/chapter17.txt b/manuscript/chapter17.txt index 756faa9..8f5f2ee 100644 --- a/manuscript/chapter17.txt +++ b/manuscript/chapter17.txt @@ -1,9 +1,8 @@ -# Other +# 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. +Surprisingly, `sleep` is an external command and not a `bash` built-in. **CAVEAT:** Requires `bash` 4+ @@ -28,8 +27,7 @@ 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. +# There are 3 ways to do this and either one can be used. type -p executable_name &>/dev/null hash executable_name &>/dev/null command -v executable_name &>/dev/null @@ -44,9 +42,9 @@ if ! type -p executable_name &>/dev/null; then # Program is not in PATH. fi -# Example (Exit early if program isn't installed). +# Example (Exit early if program is not installed). if ! type -p convert &>/dev/null; then - printf '%s\n' "error: convert isn't installed, exiting..." + printf '%s\n' "error: convert is not installed, exiting..." exit 1 fi ``` @@ -158,7 +156,7 @@ done printf '\n' ``` -## Get the list of functions from your script +## Get the list of functions in a script ```sh get_functions() { diff --git a/manuscript/chapter2.txt b/manuscript/chapter2.txt index e677440..2d7c27c 100644 --- a/manuscript/chapter2.txt +++ b/manuscript/chapter2.txt @@ -1,4 +1,4 @@ -# Arrays +# ARRAYS ## Reverse an array @@ -92,7 +92,7 @@ $ array=(red green blue yellow brown) $ random_array_element "${array[@]}" yellow -# You can also just pass multiple arguments. +# Multiple arguments can also be passed. $ random_array_element 1 2 3 4 5 6 7 3 ``` diff --git a/manuscript/chapter3.txt b/manuscript/chapter3.txt index 90fc12f..75f88de 100644 --- a/manuscript/chapter3.txt +++ b/manuscript/chapter3.txt @@ -1,8 +1,8 @@ -# Loops +# LOOPS ## Loop over a range of numbers -Don't use `seq`. +Alternative to `seq`. ```shell # Loop from 0-100 (no variable support). @@ -13,7 +13,7 @@ done ## Loop over a variable range of numbers -Don't use `seq`. +Alternative to `seq`. ```shell # Loop from 0-VAR. diff --git a/manuscript/chapter4.txt b/manuscript/chapter4.txt index cdef4a2..079c060 100644 --- a/manuscript/chapter4.txt +++ b/manuscript/chapter4.txt @@ -1,6 +1,6 @@ -# File handling +# FILE HANDLING -**CAVEAT:** `bash` doesn't handle binary data properly in versions `< 4.4`. +**CAVEAT:** `bash` does not handle binary data properly in versions `< 4.4`. ## Read a file to a string @@ -92,8 +92,7 @@ lines() { **Example Function (bash 3):** -This method uses less memory than the `mapfile` method and it's more -compatible but it's slower for bigger files. +This method uses less memory than the `mapfile` method and works in `bash` 3 but it is slower for bigger files. ```sh lines_loop() { diff --git a/manuscript/chapter5.txt b/manuscript/chapter5.txt index 397e4ad..8725e1e 100644 --- a/manuscript/chapter5.txt +++ b/manuscript/chapter5.txt @@ -1,4 +1,4 @@ -# File Paths +# FILE PATHS ## Get the directory name of a file path diff --git a/manuscript/chapter6.txt b/manuscript/chapter6.txt index ef9d0ce..8ff5947 100644 --- a/manuscript/chapter6.txt +++ b/manuscript/chapter6.txt @@ -1,4 +1,4 @@ -# Variables +# VARIABLES ## Assign and access a variable using a variable diff --git a/manuscript/chapter7.txt b/manuscript/chapter7.txt index b83c87a..8f4ad8c 100644 --- a/manuscript/chapter7.txt +++ b/manuscript/chapter7.txt @@ -1,6 +1,6 @@ -# Escape Sequences +# ESCAPE SEQUENCES -Contrary to popular belief, there's no issue in using raw escape sequences. Using `tput` just abstracts the same ANSI escape sequences. What's worse is that `tput` isn't actually portable, there are a number of different `tput` variants on different Operating Systems each with different commands (*try and run `tput setaf 3` on a FreeBSD system*). The easiest solution ends up being raw ANSI sequences. +Contrary to popular belief, there is no issue in utilizing raw escape sequences. Using `tput` abstracts the same ANSI sequences as if printed manually. Worse still, `tput` is not actually portable. There are a number of `tput` variants each with different commands and syntaxes (*try `tput setaf 3` on a FreeBSD system*). Raw sequences are fine. ## Text Colors diff --git a/manuscript/chapter8.txt b/manuscript/chapter8.txt index 187315c..a40660a 100644 --- a/manuscript/chapter8.txt +++ b/manuscript/chapter8.txt @@ -1,4 +1,4 @@ -# Parameter Expansion +# PARAMETER EXPANSION ## Indirection @@ -58,7 +58,7 @@ | `${VAR-STRING}` | If `VAR` is unset, use `STRING` as it's value. | `${VAR:=STRING}` | If `VAR` is empty or unset, set the value of `VAR` to `STRING`. | `${VAR=STRING}` | If `VAR` is unset, set the value of `VAR` to `STRING`. -| `${VAR:+STRING}` | If `VAR` isn't empty, use `STRING` as it's value. +| `${VAR:+STRING}` | If `VAR` is not empty, use `STRING` as it's value. | `${VAR+STRING}` | If `VAR` is set, use `STRING` as it's value. | `${VAR:?STRING}` | Display an error if empty or unset. | `${VAR?STRING}` | Display an error if unset. diff --git a/manuscript/chapter9.txt b/manuscript/chapter9.txt index 4a926e1..3426fb6 100644 --- a/manuscript/chapter9.txt +++ b/manuscript/chapter9.txt @@ -1,4 +1,4 @@ -# Brace Expansion +# BRACE EXPANSION ## Ranges