From b0bab50bbb9e683756bbaa63da8fafd3f21747a4 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Sat, 5 Dec 2026 08:40:04 -0500 Subject: [PATCH] Address bird-watcher review findings Fix errors in the looping concept and explain how the last line of a file is read, stop the hints giving away solution code, and state the goal-of-zero case in the instructions. Co-Authored-By: Claude Sonnet 5.5 --- concepts/looping/about.md | 15 +++++++++------ concepts/looping/introduction.md | 15 +++++++++------ exercises/concept/bird-watcher/.docs/hints.md | 6 +++--- .../concept/bird-watcher/.docs/instructions.md | 1 + .../concept/bird-watcher/.docs/introduction.md | 15 +++++++++------ 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/concepts/looping/about.md b/concepts/looping/about.md index d1080931..e9ba48fa 100644 --- a/concepts/looping/about.md +++ b/concepts/looping/about.md @@ -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 ``` @@ -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. @@ -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 @@ -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 @@ -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. @@ -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 @@ -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 ... diff --git a/concepts/looping/introduction.md b/concepts/looping/introduction.md index aec6a4e5..b3af2eac 100644 --- a/concepts/looping/introduction.md +++ b/concepts/looping/introduction.md @@ -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 ``` @@ -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. @@ -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 @@ -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 @@ -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. @@ -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 @@ -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 ... diff --git a/exercises/concept/bird-watcher/.docs/hints.md b/exercises/concept/bird-watcher/.docs/hints.md index 69f621e4..554f6540 100644 --- a/exercises/concept/bird-watcher/.docs/hints.md +++ b/exercises/concept/bird-watcher/.docs/hints.md @@ -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 @@ -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 diff --git a/exercises/concept/bird-watcher/.docs/instructions.md b/exercises/concept/bird-watcher/.docs/instructions.md index 3ba7a35c..6fcd9bb4 100644 --- a/exercises/concept/bird-watcher/.docs/instructions.md +++ b/exercises/concept/bird-watcher/.docs/instructions.md @@ -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. diff --git a/exercises/concept/bird-watcher/.docs/introduction.md b/exercises/concept/bird-watcher/.docs/introduction.md index 6bb92b6b..4898927f 100644 --- a/exercises/concept/bird-watcher/.docs/introduction.md +++ b/exercises/concept/bird-watcher/.docs/introduction.md @@ -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 ``` @@ -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. @@ -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 @@ -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 @@ -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. @@ -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 @@ -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 ...