Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions concepts/looping/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ done
This is an anti-pattern you'll often see: iterating over the output of `cat`.

```bash
for line in $(cat some.file); do
for line in $(cat some.file); do
do_something_with "$line"
done
```
Expand All @@ -83,7 +83,7 @@ This is wrong on 2 counts.
2. The command substitution is unquoted, so it is subject to filename expansion.
Every word in the file will be matched as a glob pattern.

As we learned in the [Quoting concept][quoting], word splitting can be controlled with the `IFS` variable, and filename expansion can be turned off.
Word splitting can be controlled with the `IFS` variable, and filename expansion can be turned off.
But this tends to be a fragile solution.
`for` is the wrong method to iterate over the lines of a file.
The idiomatic way to read a file is with a `while` loop.
Expand All @@ -96,7 +96,7 @@ Use `while` to repeat a sequence of commands _while_ some condition is true.
while CONDITION_COMMANDS; do COMMANDS; done
```

As with `if` (as we learned in the [Conditionals concept][conditionals], there is no special syntax for CONDITION_COMMANDS.
As with `if` (as we learned in the [Conditionals concept][conditionals]), there is no special syntax for CONDITION_COMMANDS.
The exit status of the command list will determine "true" or "false".

## Controlling Loops
Expand All @@ -108,7 +108,7 @@ The `continue` command jumps to the next iteration of the loop.

## Reading the Lines of a File

As mentioned, a while loop is the idiomatic way to read a file.
As mentioned, a while loop is the idiomatic way to read a file.

```bash
while IFS= read -r line; do
Expand All @@ -119,12 +119,15 @@ done < some.file
* The content of the file is provided as input to the loop with the `<` redirection.
* The `read` command returns "true" if it can read a full line from its input.
When the input is consumed, or if the last line ends without a trailing newline, then read returns "false".
In the latter case, `read` still stores the partial last line in the variable.
* The _truly_ idiomatic way to read the lines of a file, even if the last line does not end with a newline, is

```bash
while IFS= read -r line || [[ -n "$line" ]]; do ...
```

The `-n` test is true when the variable is not empty, so the loop body also runs for that partial line.

* The `-r` option tells `read` to not substitute backslash sequences: a backslash is just a plain character.
* `IFS=` assigns the empty string to IFS only for the duration of the read command.
This temporarily turns off word splitting so that any leading or trailing whitespace in the incoming line of text is preserved.
Expand All @@ -142,7 +145,7 @@ It is rare to use `until`.
It is more common to use a "while not" loop.

```bash
while ! CONDITION_COMANDS; do ...
while ! CONDITION_COMMANDS; do ...
```

## Infinite Loops
Expand All @@ -156,7 +159,7 @@ Here are two ways to do it.
while true; do ...
```

2. an arithmetic for loop with empty expressions
2. an arithmetic for loop with empty expressions

```bash
for ((;;)); do ...
Expand Down
15 changes: 9 additions & 6 deletions concepts/looping/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ done
This is an anti-pattern you'll often see: iterating over the output of `cat`.

```bash
for line in $(cat some.file); do
for line in $(cat some.file); do
do_something_with "$line"
done
```
Expand All @@ -83,7 +83,7 @@ This is wrong on 2 counts.
2. The command substitution is unquoted, so it is subject to filename expansion.
Every word in the file will be matched as a glob pattern.

As we learned in the [Quoting concept][quoting], word splitting can be controlled with the `IFS` variable, and filename expansion can be turned off.
Word splitting can be controlled with the `IFS` variable, and filename expansion can be turned off.
But this tends to be a fragile solution.
`for` is the wrong method to iterate over the lines of a file.
The idiomatic way to read a file is with a `while` loop.
Expand All @@ -96,7 +96,7 @@ Use `while` to repeat a sequence of commands _while_ some condition is true.
while CONDITION_COMMANDS; do COMMANDS; done
```

As with `if` (as we learned in the [Conditionals concept][conditionals], there is no special syntax for CONDITION_COMMANDS.
As with `if` (as we learned in the [Conditionals concept][conditionals]), there is no special syntax for CONDITION_COMMANDS.
The exit status of the command list will determine "true" or "false".

## Controlling Loops
Expand All @@ -108,7 +108,7 @@ The `continue` command jumps to the next iteration of the loop.

## Reading the Lines of a File

As mentioned, a while loop is the idiomatic way to read a file.
As mentioned, a while loop is the idiomatic way to read a file.

```bash
while IFS= read -r line; do
Expand All @@ -119,12 +119,15 @@ done < some.file
* The content of the file is provided as input to the loop with the `<` redirection.
* The `read` command returns "true" if it can read a full line from its input.
When the input is consumed, or if the last line ends without a trailing newline, then read returns "false".
In the latter case, `read` still stores the partial last line in the variable.
* The _truly_ idiomatic way to read the lines of a file, even if the last line does not end with a newline, is

```bash
while IFS= read -r line || [[ -n "$line" ]]; do ...
```

The `-n` test is true when the variable is not empty, so the loop body also runs for that partial line.

* The `-r` option tells `read` to not substitute backslash sequences: a backslash is just a plain character.
* `IFS=` assigns the empty string to IFS only for the duration of the read command.
This temporarily turns off word splitting so that any leading or trailing whitespace in the incoming line of text is preserved.
Expand All @@ -142,7 +145,7 @@ It is rare to use `until`.
It is more common to use a "while not" loop.

```bash
while ! CONDITION_COMANDS; do ...
while ! CONDITION_COMMANDS; do ...
```

## Infinite Loops
Expand All @@ -156,7 +159,7 @@ Here are two ways to do it.
while true; do ...
```

2. an arithmetic for loop with empty expressions
2. an arithmetic for loop with empty expressions

```bash
for ((;;)); do ...
Expand Down
6 changes: 3 additions & 3 deletions exercises/concept/bird-watcher/.docs/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
## 3. Draw a bar of a chart

- An [arithmetic `for` loop][looping-constructs] can repeat commands a given number of times.
- Build up the stars in a variable: `bar="${bar}*"`.
- Build up the stars in a variable, adding one each time around the loop.
- Print the variable after the loop, inside double quotes.

## 4. Predict when you will reach your goal
Expand All @@ -33,8 +33,8 @@

## 5. Summarize your log file

- Read the file line by line with `while IFS= read -r line`, and redirect the file into the loop after `done`.
- Add `|| [[ -n $line ]]` to the condition to handle a last line without a newline.
- Read the file line by line with a `while` loop and the `read` command, and redirect the file into the loop.
- A last line without a newline needs an extra check in the loop condition: see "Reading the Lines of a File" in the introduction.
- Use [`continue`][builtins] to skip a rainy day.

[looping-constructs]: https://www.gnu.org/software/bash/manual/bash.html#Looping-Constructs
Expand Down
1 change: 1 addition & 0 deletions exercises/concept/bird-watcher/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ The first argument is the total number of birds you want to see.
The second argument is the number of birds you expect to see on the first day.

Print the number of days needed for the total to reach or pass the goal.
If the goal is 0, print `0`.

For example, with 2 birds on the first day, you expect to see 2, 3, 4 and then 5 birds.
The totals after each day are 2, 5, 9 and 14, so a goal of 10 birds takes 4 days.
Expand Down
15 changes: 9 additions & 6 deletions exercises/concept/bird-watcher/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ done
This is an anti-pattern you'll often see: iterating over the output of `cat`.

```bash
for line in $(cat some.file); do
for line in $(cat some.file); do
do_something_with "$line"
done
```
Expand All @@ -85,7 +85,7 @@ This is wrong on 2 counts.
2. The command substitution is unquoted, so it is subject to filename expansion.
Every word in the file will be matched as a glob pattern.

As we learned in the [Quoting concept][quoting], word splitting can be controlled with the `IFS` variable, and filename expansion can be turned off.
Word splitting can be controlled with the `IFS` variable, and filename expansion can be turned off.
But this tends to be a fragile solution.
`for` is the wrong method to iterate over the lines of a file.
The idiomatic way to read a file is with a `while` loop.
Expand All @@ -98,7 +98,7 @@ Use `while` to repeat a sequence of commands _while_ some condition is true.
while CONDITION_COMMANDS; do COMMANDS; done
```

As with `if` (as we learned in the [Conditionals concept][conditionals], there is no special syntax for CONDITION_COMMANDS.
As with `if` (as we learned in the [Conditionals concept][conditionals]), there is no special syntax for CONDITION_COMMANDS.
The exit status of the command list will determine "true" or "false".

### Controlling Loops
Expand All @@ -110,7 +110,7 @@ The `continue` command jumps to the next iteration of the loop.

### Reading the Lines of a File

As mentioned, a while loop is the idiomatic way to read a file.
As mentioned, a while loop is the idiomatic way to read a file.

```bash
while IFS= read -r line; do
Expand All @@ -121,12 +121,15 @@ done < some.file
* The content of the file is provided as input to the loop with the `<` redirection.
* The `read` command returns "true" if it can read a full line from its input.
When the input is consumed, or if the last line ends without a trailing newline, then read returns "false".
In the latter case, `read` still stores the partial last line in the variable.
* The _truly_ idiomatic way to read the lines of a file, even if the last line does not end with a newline, is

```bash
while IFS= read -r line || [[ -n "$line" ]]; do ...
```

The `-n` test is true when the variable is not empty, so the loop body also runs for that partial line.

* The `-r` option tells `read` to not substitute backslash sequences: a backslash is just a plain character.
* `IFS=` assigns the empty string to IFS only for the duration of the read command.
This temporarily turns off word splitting so that any leading or trailing whitespace in the incoming line of text is preserved.
Expand All @@ -144,7 +147,7 @@ It is rare to use `until`.
It is more common to use a "while not" loop.

```bash
while ! CONDITION_COMANDS; do ...
while ! CONDITION_COMMANDS; do ...
```

### Infinite Loops
Expand All @@ -158,7 +161,7 @@ Here are two ways to do it.
while true; do ...
```

2. an arithmetic for loop with empty expressions
2. an arithmetic for loop with empty expressions

```bash
for ((;;)); do ...
Expand Down
Loading