Added conditionals

This commit is contained in:
Dylan Araps 2018-06-23 10:34:10 +10:00
parent 809c8b5a03
commit c4a203bb40
2 changed files with 70 additions and 5 deletions

View File

@ -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}
<!-- CHAPTER END -->
<!-- CHAPTER START -->
# 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.*)
<!-- CHAPTER END -->
<!-- CHAPTER START -->
# 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"

View File

@ -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"
}
```