Miscellaneous improvements
This commit is contained in:
parent
2b558f2962
commit
2414ae9f42
64
README.md
64
README.md
|
@ -274,7 +274,7 @@ $ regex "red" '^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$'
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
is_hex_color() {
|
is_hex_color() {
|
||||||
if [[ "$1" =~ ^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$ ]]; then
|
if [[ $1 =~ ^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$ ]]; then
|
||||||
printf '%s\n' "${BASH_REMATCH[1]}"
|
printf '%s\n' "${BASH_REMATCH[1]}"
|
||||||
else
|
else
|
||||||
printf '%s\n' "error: $1 is an invalid color."
|
printf '%s\n' "error: $1 is an invalid color."
|
||||||
|
@ -487,17 +487,17 @@ The Quick Brown
|
||||||
**Using a test:**
|
**Using a test:**
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
if [[ "$var" == *sub_string* ]]; then
|
if [[ $var == *sub_string* ]]; then
|
||||||
printf '%s\n' "sub_string is in var."
|
printf '%s\n' "sub_string is in var."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Inverse (substring not in string).
|
# Inverse (substring not in string).
|
||||||
if [[ "$var" != *sub_string* ]]; then
|
if [[ $var != *sub_string* ]]; then
|
||||||
printf '%s\n' "sub_string is not in var."
|
printf '%s\n' "sub_string is not in var."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# This works for arrays too!
|
# This works for arrays too!
|
||||||
if [[ "${arr[*]}" == *sub_string* ]]; then
|
if [[ ${arr[*]} == *sub_string* ]]; then
|
||||||
printf '%s\n' "sub_string is in array."
|
printf '%s\n' "sub_string is in array."
|
||||||
fi
|
fi
|
||||||
```
|
```
|
||||||
|
@ -523,12 +523,12 @@ esac
|
||||||
## Check if string starts with sub-string
|
## Check if string starts with sub-string
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
if [[ "$var" == sub_string* ]]; then
|
if [[ $var == sub_string* ]]; then
|
||||||
printf '%s\n' "var starts with sub_string."
|
printf '%s\n' "var starts with sub_string."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Inverse (var does not start with sub_string).
|
# Inverse (var does not start with sub_string).
|
||||||
if [[ "$var" != sub_string* ]]; then
|
if [[ $var != sub_string* ]]; then
|
||||||
printf '%s\n' "var does not start with sub_string."
|
printf '%s\n' "var does not start with sub_string."
|
||||||
fi
|
fi
|
||||||
```
|
```
|
||||||
|
@ -536,12 +536,12 @@ fi
|
||||||
## Check if string ends with sub-string
|
## Check if string ends with sub-string
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
if [[ "$var" == *sub_string ]]; then
|
if [[ $var == *sub_string ]]; then
|
||||||
printf '%s\n' "var ends with sub_string."
|
printf '%s\n' "var ends with sub_string."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Inverse (var does not end with sub_string).
|
# Inverse (var does not end with sub_string).
|
||||||
if [[ "$var" != *sub_string ]]; then
|
if [[ $var != *sub_string ]]; then
|
||||||
printf '%s\n' "var does not end with sub_string."
|
printf '%s\n' "var does not end with sub_string."
|
||||||
fi
|
fi
|
||||||
```
|
```
|
||||||
|
@ -600,7 +600,7 @@ remove_array_dups() {
|
||||||
declare -A tmp_array
|
declare -A tmp_array
|
||||||
|
|
||||||
for i in "$@"; do
|
for i in "$@"; do
|
||||||
[[ "$i" ]] && IFS=" " tmp_array["${i:- }"]=1
|
[[ $i ]] && IFS=" " tmp_array["${i:- }"]=1
|
||||||
done
|
done
|
||||||
|
|
||||||
printf '%s\n' "${!tmp_array[@]}"
|
printf '%s\n' "${!tmp_array[@]}"
|
||||||
|
@ -723,12 +723,12 @@ arr=(apples oranges tomatoes)
|
||||||
|
|
||||||
# Elements and index.
|
# Elements and index.
|
||||||
for i in "${!arr[@]}"; do
|
for i in "${!arr[@]}"; do
|
||||||
printf '%s\n' "${arr[$i]}"
|
printf '%s\n' "${arr[i]}"
|
||||||
done
|
done
|
||||||
|
|
||||||
# Alternative method.
|
# Alternative method.
|
||||||
for ((i=0;i<${#arr[@]};i++)); do
|
for ((i=0;i<${#arr[@]};i++)); do
|
||||||
printf '%s\n' "${arr[$i]}"
|
printf '%s\n' "${arr[i]}"
|
||||||
done
|
done
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -945,11 +945,11 @@ printf '' >file
|
||||||
extract() {
|
extract() {
|
||||||
# Usage: extract file "opening marker" "closing marker"
|
# Usage: extract file "opening marker" "closing marker"
|
||||||
while IFS=$'\n' read -r line; do
|
while IFS=$'\n' read -r line; do
|
||||||
[[ "$extract" && "$line" != "$3" ]] && \
|
[[ $extract && $line != "$3" ]] &&
|
||||||
printf '%s\n' "$line"
|
printf '%s\n' "$line"
|
||||||
|
|
||||||
[[ "$line" == "$2" ]] && extract=1
|
[[ $line == "$2" ]] && extract=1
|
||||||
[[ "$line" == "$3" ]] && extract=
|
[[ $line == "$3" ]] && extract=
|
||||||
done < "$1"
|
done < "$1"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -1022,14 +1022,28 @@ Downloads
|
||||||
## Assign and access a variable using a variable
|
## Assign and access a variable using a variable
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
hello_world="test"
|
$ hello_world="value"
|
||||||
|
|
||||||
# Create the variable name.
|
# Create the variable name.
|
||||||
var1="world"
|
$ var="world"
|
||||||
var2="hello_${var1}"
|
$ ref="hello_$var"
|
||||||
|
|
||||||
# Print the value of the variable name stored in 'hello_$var1'.
|
# Print the value of the variable name stored in 'hello_$var'.
|
||||||
printf '%s\n' "${!var2}"
|
$ printf '%s\n' "${!ref}"
|
||||||
|
value
|
||||||
|
```
|
||||||
|
|
||||||
|
Alternatively, on `bash` 4.3+:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ hello_world="value"
|
||||||
|
$ var="world"
|
||||||
|
|
||||||
|
# Declare a nameref.
|
||||||
|
$ declare -n ref=hello_$var
|
||||||
|
|
||||||
|
$ printf '%s\n' "$ref"
|
||||||
|
value
|
||||||
```
|
```
|
||||||
|
|
||||||
<!-- CHAPTER END -->
|
<!-- CHAPTER END -->
|
||||||
|
@ -1101,7 +1115,7 @@ Contrary to popular belief, there is no issue in utilizing raw escape sequences.
|
||||||
| --------- | ---------------- |
|
| --------- | ---------------- |
|
||||||
| `${!VAR}` | Access a variable based on the value of `VAR`.
|
| `${!VAR}` | Access a variable based on the value of `VAR`.
|
||||||
| `${!VAR*}` | Expand to `IFS` separated list of variable names starting with `VAR`. |
|
| `${!VAR*}` | Expand to `IFS` separated list of variable names starting with `VAR`. |
|
||||||
| `${!VAR@}` | Expand to `IFS` separated list of variable names starting with `VAR`. |
|
| `${!VAR@}` | Expand to `IFS` separated list of variable names starting with `VAR`. If double-quoted, each variable name expands to a separate word. |
|
||||||
|
|
||||||
|
|
||||||
## Replacement
|
## Replacement
|
||||||
|
@ -1741,16 +1755,16 @@ f()for i in "$@"; do echo "$i"; done
|
||||||
```shell
|
```shell
|
||||||
# One line
|
# One line
|
||||||
# Note: The 3rd statement may run when the 1st is true
|
# Note: The 3rd statement may run when the 1st is true
|
||||||
[[ "$var" == hello ]] && echo hi || echo bye
|
[[ $var == hello ]] && echo hi || echo bye
|
||||||
[[ "$var" == hello ]] && { echo hi; echo there; } || echo bye
|
[[ $var == hello ]] && { echo hi; echo there; } || echo bye
|
||||||
|
|
||||||
# Multi line (no else, single statement)
|
# Multi line (no else, single statement)
|
||||||
# Note: The exit status may not be the same as with an if statement
|
# Note: The exit status may not be the same as with an if statement
|
||||||
[[ "$var" == hello ]] && \
|
[[ $var == hello ]] &&
|
||||||
echo hi
|
echo hi
|
||||||
|
|
||||||
# Multi line (no else)
|
# Multi line (no else)
|
||||||
[[ "$var" == hello ]] && {
|
[[ $var == hello ]] && {
|
||||||
echo hi
|
echo hi
|
||||||
# ...
|
# ...
|
||||||
}
|
}
|
||||||
|
@ -1882,7 +1896,7 @@ Fri 15 Jun - 10:00 AM
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ : \\u
|
$ : \\u
|
||||||
# Expand the parameter as if it were a prompt string
|
# Expand the parameter as if it were a prompt string.
|
||||||
$ printf '%s\n' "${_@P}"
|
$ printf '%s\n' "${_@P}"
|
||||||
black
|
black
|
||||||
```
|
```
|
||||||
|
|
|
@ -100,7 +100,7 @@ $ regex "red" '^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$'
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
is_hex_color() {
|
is_hex_color() {
|
||||||
if [[ "$1" =~ ^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$ ]]; then
|
if [[ $1 =~ ^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$ ]]; then
|
||||||
printf '%s\n' "${BASH_REMATCH[1]}"
|
printf '%s\n' "${BASH_REMATCH[1]}"
|
||||||
else
|
else
|
||||||
printf '%s\n' "error: $1 is an invalid color."
|
printf '%s\n' "error: $1 is an invalid color."
|
||||||
|
@ -313,17 +313,17 @@ The Quick Brown
|
||||||
**Using a test:**
|
**Using a test:**
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
if [[ "$var" == *sub_string* ]]; then
|
if [[ $var == *sub_string* ]]; then
|
||||||
printf '%s\n' "sub_string is in var."
|
printf '%s\n' "sub_string is in var."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Inverse (substring not in string).
|
# Inverse (substring not in string).
|
||||||
if [[ "$var" != *sub_string* ]]; then
|
if [[ $var != *sub_string* ]]; then
|
||||||
printf '%s\n' "sub_string is not in var."
|
printf '%s\n' "sub_string is not in var."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# This works for arrays too!
|
# This works for arrays too!
|
||||||
if [[ "${arr[*]}" == *sub_string* ]]; then
|
if [[ ${arr[*]} == *sub_string* ]]; then
|
||||||
printf '%s\n' "sub_string is in array."
|
printf '%s\n' "sub_string is in array."
|
||||||
fi
|
fi
|
||||||
```
|
```
|
||||||
|
@ -349,12 +349,12 @@ esac
|
||||||
## Check if string starts with sub-string
|
## Check if string starts with sub-string
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
if [[ "$var" == sub_string* ]]; then
|
if [[ $var == sub_string* ]]; then
|
||||||
printf '%s\n' "var starts with sub_string."
|
printf '%s\n' "var starts with sub_string."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Inverse (var does not start with sub_string).
|
# Inverse (var does not start with sub_string).
|
||||||
if [[ "$var" != sub_string* ]]; then
|
if [[ $var != sub_string* ]]; then
|
||||||
printf '%s\n' "var does not start with sub_string."
|
printf '%s\n' "var does not start with sub_string."
|
||||||
fi
|
fi
|
||||||
```
|
```
|
||||||
|
@ -362,12 +362,12 @@ fi
|
||||||
## Check if string ends with sub-string
|
## Check if string ends with sub-string
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
if [[ "$var" == *sub_string ]]; then
|
if [[ $var == *sub_string ]]; then
|
||||||
printf '%s\n' "var ends with sub_string."
|
printf '%s\n' "var ends with sub_string."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Inverse (var does not end with sub_string).
|
# Inverse (var does not end with sub_string).
|
||||||
if [[ "$var" != *sub_string ]]; then
|
if [[ $var != *sub_string ]]; then
|
||||||
printf '%s\n' "var does not end with sub_string."
|
printf '%s\n' "var does not end with sub_string."
|
||||||
fi
|
fi
|
||||||
```
|
```
|
||||||
|
|
|
@ -95,16 +95,16 @@ f()for i in "$@"; do echo "$i"; done
|
||||||
```shell
|
```shell
|
||||||
# One line
|
# One line
|
||||||
# Note: The 3rd statement may run when the 1st is true
|
# Note: The 3rd statement may run when the 1st is true
|
||||||
[[ "$var" == hello ]] && echo hi || echo bye
|
[[ $var == hello ]] && echo hi || echo bye
|
||||||
[[ "$var" == hello ]] && { echo hi; echo there; } || echo bye
|
[[ $var == hello ]] && { echo hi; echo there; } || echo bye
|
||||||
|
|
||||||
# Multi line (no else, single statement)
|
# Multi line (no else, single statement)
|
||||||
# Note: The exit status may not be the same as with an if statement
|
# Note: The exit status may not be the same as with an if statement
|
||||||
[[ "$var" == hello ]] && \
|
[[ $var == hello ]] &&
|
||||||
echo hi
|
echo hi
|
||||||
|
|
||||||
# Multi line (no else)
|
# Multi line (no else)
|
||||||
[[ "$var" == hello ]] && {
|
[[ $var == hello ]] && {
|
||||||
echo hi
|
echo hi
|
||||||
# ...
|
# ...
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,7 +88,7 @@ Fri 15 Jun - 10:00 AM
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ : \\u
|
$ : \\u
|
||||||
# Expand the parameter as if it were a prompt string
|
# Expand the parameter as if it were a prompt string.
|
||||||
$ printf '%s\n' "${_@P}"
|
$ printf '%s\n' "${_@P}"
|
||||||
black
|
black
|
||||||
```
|
```
|
||||||
|
|
|
@ -49,7 +49,7 @@ remove_array_dups() {
|
||||||
declare -A tmp_array
|
declare -A tmp_array
|
||||||
|
|
||||||
for i in "$@"; do
|
for i in "$@"; do
|
||||||
[[ "$i" ]] && IFS=" " tmp_array["${i:- }"]=1
|
[[ $i ]] && IFS=" " tmp_array["${i:- }"]=1
|
||||||
done
|
done
|
||||||
|
|
||||||
printf '%s\n' "${!tmp_array[@]}"
|
printf '%s\n' "${!tmp_array[@]}"
|
||||||
|
|
|
@ -41,12 +41,12 @@ arr=(apples oranges tomatoes)
|
||||||
|
|
||||||
# Elements and index.
|
# Elements and index.
|
||||||
for i in "${!arr[@]}"; do
|
for i in "${!arr[@]}"; do
|
||||||
printf '%s\n' "${arr[$i]}"
|
printf '%s\n' "${arr[i]}"
|
||||||
done
|
done
|
||||||
|
|
||||||
# Alternative method.
|
# Alternative method.
|
||||||
for ((i=0;i<${#arr[@]};i++)); do
|
for ((i=0;i<${#arr[@]};i++)); do
|
||||||
printf '%s\n' "${arr[$i]}"
|
printf '%s\n' "${arr[i]}"
|
||||||
done
|
done
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -167,11 +167,11 @@ printf '' >file
|
||||||
extract() {
|
extract() {
|
||||||
# Usage: extract file "opening marker" "closing marker"
|
# Usage: extract file "opening marker" "closing marker"
|
||||||
while IFS=$'\n' read -r line; do
|
while IFS=$'\n' read -r line; do
|
||||||
[[ "$extract" && "$line" != "$3" ]] && \
|
[[ $extract && $line != "$3" ]] &&
|
||||||
printf '%s\n' "$line"
|
printf '%s\n' "$line"
|
||||||
|
|
||||||
[[ "$line" == "$2" ]] && extract=1
|
[[ $line == "$2" ]] && extract=1
|
||||||
[[ "$line" == "$3" ]] && extract=
|
[[ $line == "$3" ]] && extract=
|
||||||
done < "$1"
|
done < "$1"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
|
@ -3,14 +3,28 @@
|
||||||
## Assign and access a variable using a variable
|
## Assign and access a variable using a variable
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
hello_world="test"
|
$ hello_world="value"
|
||||||
|
|
||||||
# Create the variable name.
|
# Create the variable name.
|
||||||
var1="world"
|
$ var="world"
|
||||||
var2="hello_${var1}"
|
$ ref="hello_$var"
|
||||||
|
|
||||||
# Print the value of the variable name stored in 'hello_$var1'.
|
# Print the value of the variable name stored in 'hello_$var'.
|
||||||
printf '%s\n' "${!var2}"
|
$ printf '%s\n' "${!ref}"
|
||||||
|
value
|
||||||
|
```
|
||||||
|
|
||||||
|
Alternatively, on `bash` 4.3+:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ hello_world="value"
|
||||||
|
$ var="world"
|
||||||
|
|
||||||
|
# Declare a nameref.
|
||||||
|
$ declare -n ref=hello_$var
|
||||||
|
|
||||||
|
$ printf '%s\n' "$ref"
|
||||||
|
value
|
||||||
```
|
```
|
||||||
|
|
||||||
<!-- CHAPTER END -->
|
<!-- CHAPTER END -->
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
| --------- | ---------------- |
|
| --------- | ---------------- |
|
||||||
| `${!VAR}` | Access a variable based on the value of `VAR`.
|
| `${!VAR}` | Access a variable based on the value of `VAR`.
|
||||||
| `${!VAR*}` | Expand to `IFS` separated list of variable names starting with `VAR`. |
|
| `${!VAR*}` | Expand to `IFS` separated list of variable names starting with `VAR`. |
|
||||||
| `${!VAR@}` | Expand to `IFS` separated list of variable names starting with `VAR`. |
|
| `${!VAR@}` | Expand to `IFS` separated list of variable names starting with `VAR`. If double-quoted, each variable name expands to a separate word. |
|
||||||
|
|
||||||
|
|
||||||
## Replacement
|
## Replacement
|
||||||
|
|
Loading…
Reference in New Issue