Added loops
This commit is contained in:
12
README.md
12
README.md
@@ -62,7 +62,9 @@ scripts and not full blown utilities.
|
|||||||
* [Toggle between two values.](#toggle-between-two-values)
|
* [Toggle between two values.](#toggle-between-two-values)
|
||||||
* [Loops](#loops)
|
* [Loops](#loops)
|
||||||
* [Loop over a range of numbers.](#loop-over-a-range-of-numbers)
|
* [Loop over a range of numbers.](#loop-over-a-range-of-numbers)
|
||||||
|
* [Loop over a variable range of numbers.](#loop-over-a-variable-range-of-numbers)
|
||||||
* [Loop over an array.](#loop-over-an-array)
|
* [Loop over an array.](#loop-over-an-array)
|
||||||
|
* [Loop over an array with an index.](#loop-over-an-array-with-an-index)
|
||||||
* [Loop over files and directories.](#loop-over-files-and-directories)
|
* [Loop over files and directories.](#loop-over-files-and-directories)
|
||||||
* [File handling](#file-handling)
|
* [File handling](#file-handling)
|
||||||
* [Read a file to a string.](#read-a-file-to-a-string)
|
* [Read a file to a string.](#read-a-file-to-a-string)
|
||||||
@@ -565,7 +567,13 @@ Don't use `seq`.
|
|||||||
for i in {0..100}; do
|
for i in {0..100}; do
|
||||||
printf '%s\n' "$i"
|
printf '%s\n' "$i"
|
||||||
done
|
done
|
||||||
|
```
|
||||||
|
|
||||||
|
## Loop over a variable range of numbers.
|
||||||
|
|
||||||
|
Don't use `seq`.
|
||||||
|
|
||||||
|
```shell
|
||||||
# Loop from 0-VAR.
|
# Loop from 0-VAR.
|
||||||
VAR=50
|
VAR=50
|
||||||
for ((i=0;i<=VAR;i++)); do
|
for ((i=0;i<=VAR;i++)); do
|
||||||
@@ -582,7 +590,11 @@ arr=(apples oranges tomatoes)
|
|||||||
for element in "${arr[@]}"; do
|
for element in "${arr[@]}"; do
|
||||||
printf '%s\n' "$element"
|
printf '%s\n' "$element"
|
||||||
done
|
done
|
||||||
|
```
|
||||||
|
|
||||||
|
## Loop over an array with an index.
|
||||||
|
|
||||||
|
```shell
|
||||||
# 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]}"
|
||||||
|
|||||||
Reference in New Issue
Block a user