pure-bash-bible/manuscript/chapter18.txt

148 lines
2.4 KiB
Plaintext
Raw Permalink Normal View History

2018-06-23 02:34:15 +02:00
# CONVERSION
2018-06-21 10:00:05 +02:00
2018-06-23 02:34:15 +02:00
## Convert a hex color to RGB
2018-06-21 10:00:05 +02:00
**Example Function:**
```sh
2018-06-23 02:34:15 +02:00
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"
2018-06-21 10:00:05 +02:00
}
```
**Example Usage:**
```shell
2018-06-23 02:34:15 +02:00
$ hex_to_rgb "#FFFFFF"
255 255 255
2018-06-21 10:00:05 +02:00
```
2018-06-23 02:34:15 +02:00
## Convert an RGB color to hex
2018-06-21 10:00:05 +02:00
**Example Function:**
```sh
2018-06-23 02:34:15 +02:00
rgb_to_hex() {
# Usage: rgb_to_hex "r" "g" "b"
printf '#%02x%02x%02x\n' "$1" "$2" "$3"
2018-06-21 10:00:05 +02:00
}
```
**Example Usage:**
```shell
2018-06-23 02:34:15 +02:00
$ rgb_to_hex "255" "255" "255"
#FFFFFF
2018-06-21 10:00:05 +02:00
```
2018-06-23 02:34:15 +02:00
# CODE GOLF
2018-06-21 10:00:05 +02:00
2018-06-23 02:34:15 +02:00
## Shorter `for` loop syntax
2018-06-21 10:00:05 +02:00
2018-06-23 02:34:15 +02:00
```shell
# Tiny C Style.
for((;i++<10;)){ echo "$i";}
2018-06-21 10:00:05 +02:00
2018-06-23 02:34:15 +02:00
# Undocumented method.
for i in {1..10};{ echo "$i";}
2018-06-21 10:00:05 +02:00
2018-06-23 02:34:15 +02:00
# Expansion.
for i in {1..10}; do echo "$i"; done
2018-06-21 10:00:05 +02:00
2018-06-23 02:34:15 +02:00
# C Style.
for((i=0;i<=10;i++)); do echo "$i"; done
2018-06-21 10:00:05 +02:00
```
2018-06-23 02:34:15 +02:00
## Shorter infinite loops
2018-06-21 10:00:05 +02:00
```shell
2018-06-23 02:34:15 +02:00
# Normal method
while :; do echo hi; done
2018-06-21 10:00:05 +02:00
2018-06-23 02:34:15 +02:00
# Shorter
for((;;)){ echo hi;}
2018-06-21 10:00:05 +02:00
```
2018-06-23 02:34:15 +02:00
## Shorter function declaration
2018-06-21 10:00:05 +02:00
```shell
2018-06-23 02:34:15 +02:00
# Normal method
f(){ echo hi;}
# Using a subshell
f()(echo hi)
# Using arithmetic
# This can be used to assign integer values.
# Example: f a=1
# f a++
f()(($1))
# Using tests, loops etc.
# NOTE: while, until, case, (()), [[]] can also be used.
f()if true; then echo "$1"; fi
f()for i in "$@"; do echo "$i"; done
2018-06-21 10:00:05 +02:00
```
2018-06-23 02:34:15 +02:00
## Shorter `if` syntax
2018-06-21 10:00:05 +02:00
2018-06-23 02:34:15 +02:00
```shell
# One line
# Note: The 3rd statement may run when the 1st is true
2019-01-19 10:20:13 +01:00
[[ $var == hello ]] && echo hi || echo bye
[[ $var == hello ]] && { echo hi; echo there; } || echo bye
2018-06-23 02:34:15 +02:00
# Multi line (no else, single statement)
# Note: The exit status may not be the same as with an if statement
2019-01-19 10:20:13 +01:00
[[ $var == hello ]] &&
2018-06-23 02:34:15 +02:00
echo hi
# Multi line (no else)
2019-01-19 10:20:13 +01:00
[[ $var == hello ]] && {
2018-06-23 02:34:15 +02:00
echo hi
# ...
2018-06-21 10:00:05 +02:00
}
```
2018-06-23 02:34:15 +02:00
## Simpler `case` statement to set variable
2018-06-21 10:00:05 +02:00
2018-06-23 02:34:15 +02:00
The `:` built-in can be used to avoid repeating `variable=` in a case statement. The `$_` variable stores the last argument of the last command. `:` always succeeds so it can be used to store the variable value.
2018-06-21 10:00:05 +02:00
```shell
2018-06-23 02:34:15 +02:00
# Modified snippet from Neofetch.
case "$OSTYPE" in
"darwin"*)
: "MacOS"
;;
"linux"*)
: "Linux"
;;
*"bsd"* | "dragonfly" | "bitrig")
: "BSD"
;;
"cygwin" | "msys" | "win32")
: "Windows"
;;
*)
printf '%s\n' "Unknown OS detected, aborting..." >&2
exit 1
;;
esac
# Finally, set the variable.
os="$_"
2018-06-21 10:00:05 +02:00
```
<!-- CHAPTER END -->