From c4a203bb4071f63a4c4943fad184d22fc0e0ddae Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 23 Jun 2018 10:34:10 +1000 Subject: [PATCH] Added conditionals --- README.md | 69 +++++++++++++++++++++++++++++++++++++++- manuscript/chapter17.txt | 6 ++-- 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6d1f96a..f5aaeb6 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,11 @@ See something that is incorrectly described, buggy or outright wrong? Open an is * [BRACE EXPANSION](#brace-expansion) * [Ranges](#ranges) * [String Lists](#string-lists) +* [CONDITIONAL EXPRESSIONS](#conditional-expressions) + * [File Conditionals](#file-conditionals) + * [File Comparisons](#file-comparisons) + * [Variable Conditionals](#variable-conditionals) + * [Variable Comparisons](#variable-comparisons) * [ARITHMETIC OPERATORS](#arithmetic-operators) * [Assignment](#assignment) * [Arithmetic](#arithmetic) @@ -1197,6 +1202,67 @@ rm -rf ~/Downloads/{Movies,Music,ISOS} + + + +# CONDITIONAL EXPRESSIONS + +## File Conditionals + +| Expression | Value | What does it do? | +| ---------- | ------ | ---------------- | +| `-a` | `file` | If file exists. +| `-b` | `file` | If file exists and is a block special file. +| `-c` | `file` | If file exists and is a character special file. +| `-d` | `file` | If file exists and is a directory. +| `-e` | `file` | If file exists. +| `-f` | `file` | If file exists and is a regular file. +| `-g` | `file` | If file exists and its set-group-id bit is set. +| `-h` | `file` | If file exists and is a symbolic link. +| `-k` | `file` | If file exists and its sticky-bit is set +| `-p` | `file` | If file exists and is a named pipe (*FIFO*). +| `-r` | `file` | If file exists and is readable. +| `-s` | `file` | If file exists and its size is greater than zero. +| `-t` | `fd` | If file descriptor is open and refers to a terminal. +| `-u` | `file` | If file exists and its set-user-id bit is set. +| `-w` | `file` | If file exists and is writable. +| `-x` | `file` | If file exists and is executable. +| `-G` | `file` | If file exists and is owned by the effective group ID. +| `-L` | `file` | If file exists and is a symbolic link. +| `-N` | `file` | If file exists and has been modified since last read. +| `-O` | `file` | If file exists and is owned by the effective user ID. +| `-S` | `file` | If file exists and is a socket. + +## File Comparisons + +| Expression | What does it do? | +| ---------- | ---------------- | +| `file -ef file2` | If both files refer to the same inode and device numbers. +| `file -nt file2` | If `file` is newer than `file2` (*uses modification ime*) or `file` exists and `file2` does not. +| `file -ot file2` | If `file` is older than `file2` (*uses modification ime*) or `file2` exists and `file` does not. + +## Variable Conditionals + +| Expression | Value | What does it do? | +| ---------- | ----- | ---------------- | +| `-o` | `opt` | If shell option is enabled. +| `-v` | `var` | If variable has a value assigned. +| `-R` | `var` | If variable is a name reference. +| `-z` | `var` | If the length of string is zero. +| `-n` | `var` | If the length of string is non-zero. + +## Variable Comparisons + +| Expression | What does it do? | +| ---------- | ---------------- | +| `var = var2` | Equal to. +| `var == var2` | Equal to (*synonym for `=`*). +| `var != var2` | Not equal to. +| `var < var2` | Less than (*in ASCII alphabetical order.*) +| `var > var2` | Greater than (*in ASCII alphabetical order.*) + + + # ARITHMETIC OPERATORS @@ -1205,7 +1271,7 @@ rm -rf ~/Downloads/{Movies,Music,ISOS} | Operators | What does it do? | | --------- | ---------------- | -| `=` | Initialize or change the value of a variable. +| `=` | Initialize or change the value of a variable. ## Arithmetic @@ -1585,6 +1651,7 @@ $ get_cursor_pos ```sh hex_to_rgb() { # Usage: hex_to_rgb "#FFFFFF" + # hex_to_rgb "000000" : "${1/\#}" ((r=16#${_:0:2},g=16#${_:2:2},b=16#${_:4:2})) printf '%s\n' "$r $g $b" diff --git a/manuscript/chapter17.txt b/manuscript/chapter17.txt index 72b3881..0da32b3 100644 --- a/manuscript/chapter17.txt +++ b/manuscript/chapter17.txt @@ -7,10 +7,8 @@ ```sh hex_to_rgb() { # Usage: hex_to_rgb "#FFFFFF" - ((r=16#${1:1:2})) - ((g=16#${1:3:2})) - ((b=16#${1:5:6})) - + : "${1/\#}" + ((r=16#${_:0:2},g=16#${_:2:2},b=16#${_:4:2})) printf '%s\n' "$r $g $b" } ```