2023-02-04 09:26:34 +00:00
|
|
|
---
|
|
|
|
categories:
|
|
|
|
- Programming Languages
|
|
|
|
tags:
|
|
|
|
- shell
|
|
|
|
---
|
|
|
|
|
2023-03-16 06:58:39 +00:00
|
|
|
# Loops in Bash
|
2023-02-04 09:26:34 +00:00
|
|
|
|
2023-03-16 06:58:39 +00:00
|
|
|
## Traditional for loop
|
|
|
|
|
|
|
|
```bash
|
|
|
|
for (( i=0; i<=5;i++ )); do
|
|
|
|
echo $i
|
|
|
|
done
|
|
|
|
|
|
|
|
# 1 2 3 4 5
|
|
|
|
```
|
|
|
|
|
|
|
|
## `for..in`: loop through an array
|
2023-03-05 08:02:49 +00:00
|
|
|
|
2023-02-04 09:26:34 +00:00
|
|
|
```bash
|
|
|
|
for element in "${arr[@]}"
|
|
|
|
do
|
|
|
|
echo "$element"
|
|
|
|
done
|
|
|
|
```
|
2023-03-05 08:02:49 +00:00
|
|
|
|
2023-03-16 06:58:39 +00:00
|
|
|
## While loop
|
2023-03-05 08:02:49 +00:00
|
|
|
|
2023-03-16 06:58:39 +00:00
|
|
|
> `while` loops execute while a condition is true (0)
|
|
|
|
|
|
|
|
We can use a `while` loop as a condition in two senses:
|
|
|
|
|
|
|
|
- execute while a given condition obtains
|
2024-02-02 15:58:13 +00:00
|
|
|
- expand and execute a given command as long as the final command in the `while'
|
|
|
|
for the command has an exit status of zero (i.e. truthy)
|
2023-03-16 06:58:39 +00:00
|
|
|
|
|
|
|
Here is an exampe of using `while` in the former case:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
declare -i n=0
|
|
|
|
while (( n<10 ))
|
|
|
|
do
|
|
|
|
echo "n:$n"
|
|
|
|
(( n++ ))
|
2023-03-05 08:02:49 +00:00
|
|
|
done
|
2023-03-16 06:58:39 +00:00
|
|
|
```
|
2023-03-05 08:02:49 +00:00
|
|
|
|
2023-03-16 06:58:39 +00:00
|
|
|
Here is an example of using `while` in the latter case:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
while read line;
|
|
|
|
do
|
|
|
|
# something
|
|
|
|
done < "$file_to_read"
|
2023-03-05 08:02:49 +00:00
|
|
|
```
|
2023-03-16 06:58:39 +00:00
|
|
|
|
|
|
|
## Until loop
|
|
|
|
|
|
|
|
> `until` loops execute until a condition is false (1)
|
|
|
|
|
|
|
|
```sh
|
|
|
|
declare -i m=0
|
|
|
|
until (( m==10 )): do
|
|
|
|
echo "m:$m"
|
|
|
|
(( m++ ))
|
|
|
|
done
|
|
|
|
```
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
This gives us the same output as `n` with the while loop but here it runs so
|
|
|
|
long as `m==10` is false. As soon as `m` is equal to 100, the condition becomes
|
|
|
|
true and hence the loop stops.
|