pure-bash-bible/manuscript/chapter2.txt

131 lines
2.0 KiB
Plaintext
Raw Normal View History

# Arrays
## Reverse an array
Enabling `extdebug` allows access to the `BASH_ARGV` array which stores
the current functions arguments in reverse.
**Example Function:**
```sh
reverse_array() {
# Usage: reverse_array "array"
shopt -s extdebug
f()(printf '%s\n' "${BASH_ARGV[@]}"); f "$@"
shopt -u extdebug
}
```
**Example Usage:**
```shell
$ reverse_array 1 2 3 4 5
5
4
3
2
1
$ arr=(red blue green)
$ reverse_array "${arr[@]}"
green
blue
red
```
## Remove duplicate array elements
Create a temporary associative array. When setting associative array
values and a duplicate assignment occurs, bash overwrites the key. This
allows us to effectively remove array duplicates.
**CAVEAT:** Requires `bash` 4+
**Example Function:**
```sh
remove_array_dups() {
# Usage: remove_array_dups "array"
declare -A tmp_array
for i in "$@"; do
[[ "$i" ]] && IFS=" " tmp_array["${i:- }"]=1
done
printf '%s\n' "${!tmp_array[@]}"
}
```
**Example Usage:**
```shell
$ remove_array_dups 1 1 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5
1
2
3
4
5
$ arr=(red red green blue blue)
$ remove_array_dups "${arr[@]}"
red
green
blue
```
## Random array element
**Example Function:**
```sh
random_array_element() {
# Usage: random_array_element "array"
local arr=("$@")
printf '%s\n' "${arr[RANDOM % $#]}"
}
```
**Example Usage:**
```shell
$ array=(red green blue yellow brown)
$ random_array_element "${array[@]}"
yellow
# You can also just pass multiple arguments.
$ random_array_element 1 2 3 4 5 6 7
3
```
## Cycle through an array
Each time the `printf` is called, the next array element is printed. When
the print hits the last array element it starts from the first element
again.
```sh
arr=(a b c d)
cycle() {
printf '%s ' "${arr[${i:=0}]}"
((i=i>=${#arr[@]}-1?0:++i))
}
```
## Toggle between two values
This works the same as above, this is just a different use case.
```sh
arr=(true false)
cycle() {
printf '%s ' "${arr[${i:=0}]}"
((i=i>=${#arr[@]}-1?0:++i))
}
```
<!-- CHAPTER END -->