From ffb320220dac3ae2cb94fa5b15570c5bd57de2b6 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Mon, 30 Nov 2026 14:54:29 -0500 Subject: [PATCH 1/2] Add the vehicle-purchase concept exercise for conditionals Move the arithmetic conditional from the arithmetic concept into the conditionals concept, which the exercise teaches in its third task. In the conditionals concept, also explain string comparison with `==` and add a note on the style of putting `then` on the same line. Update the concept exercise road map. Co-Authored-By: Claude Sonnet 5.5 --- concepts/arithmetic/about.md | 73 +- concepts/arithmetic/introduction.md | 73 +- concepts/conditionals/about.md | 57 +- concepts/conditionals/introduction.md | 57 +- config.json | 12 + exercises/concept/road-map.md | 6 +- .../concept/vehicle-purchase/.docs/hints.md | 29 + .../vehicle-purchase/.docs/instructions.md | 70 ++ .../vehicle-purchase/.docs/introduction.md | 219 ++++++ .../.docs/introduction.md.tpl | 3 + .../vehicle-purchase/.meta/config.json | 23 + .../concept/vehicle-purchase/.meta/design.md | 37 + .../vehicle-purchase/.meta/exemplar.sh | 34 + .../concept/vehicle-purchase/bats-extra.bash | 642 ++++++++++++++++++ .../vehicle-purchase/vehicle_purchase.bats | 154 +++++ .../vehicle-purchase/vehicle_purchase.sh | 20 + 16 files changed, 1394 insertions(+), 115 deletions(-) create mode 100644 exercises/concept/vehicle-purchase/.docs/hints.md create mode 100644 exercises/concept/vehicle-purchase/.docs/instructions.md create mode 100644 exercises/concept/vehicle-purchase/.docs/introduction.md create mode 100644 exercises/concept/vehicle-purchase/.docs/introduction.md.tpl create mode 100644 exercises/concept/vehicle-purchase/.meta/config.json create mode 100644 exercises/concept/vehicle-purchase/.meta/design.md create mode 100644 exercises/concept/vehicle-purchase/.meta/exemplar.sh create mode 100644 exercises/concept/vehicle-purchase/bats-extra.bash create mode 100644 exercises/concept/vehicle-purchase/vehicle_purchase.bats create mode 100644 exercises/concept/vehicle-purchase/vehicle_purchase.sh diff --git a/concepts/arithmetic/about.md b/concepts/arithmetic/about.md index 6872fdcd..7dab52fb 100644 --- a/concepts/arithmetic/about.md +++ b/concepts/arithmetic/about.md @@ -15,60 +15,36 @@ As we saw in the [Variables][variables] lesson, math can be performed in an arit x=$(( 3 + 4 )) ``` -## Arithmetic Conditional +## Arithmetic Command -The arithmetic conditional is performed within double parentheses _without_ the leading `$`: +The `(( expression ))` syntax is also used as a conditional, as described in the [Arithmetic Conditional][arithmetic-conditional] section of the Conditionals concept. + +It can also be used as a standalone command to modify a variable's value. ```bash -(( expression )) +(( x = x + 1 )) +echo "new value for x is $x" ``` -This does not expand to the result. -However it produces an _exit status_. - -* The exit status is 0 (success) if the expression's result is **non-zero**. -* The exit status is 1 (failure) if the expression's result is **zero**. - -~~~~exercism/note -Inside `((` Bash uses the same sense of true/false as the C language: zero is false, non-zero is true. -**This is the opposite of how Bash evaluates exit statuses!** -~~~~ - -This syntax is typically used in two places: - -1. as the COMMAND part of an [`if` statement][conditional-if] or a [`while` loop][looping-while]. - - ```bash - if (( x == y - 1 )); then ... - while (( y > 0 )); do ... - ``` +~~~~exercism/caution +Be careful using arithmetic this way combined with `set -e`. +If the expression evaluates to zero, the exit status will be non-zero and Bash will terminate. -1. as a standalone command to modify a variable's value. - - ```bash - (( x = x + 1 )) - echo "new value for x is $x" - ``` - - ~~~~exercism/caution - Be careful using arithmetic this way combined with `set -e`. - If the expression evaluates to zero, the exit status will be non-zero and Bash will terminate. - - ```bash - x=0 - (( x++ )) - echo "you will see this" - - set -e - x=0 - (( x++ )) - echo "shell exits on the previous line; you won't see this" - ``` +```bash +x=0 +(( x++ )) +echo "you will see this" + +set -e +x=0 +(( x++ )) +echo "shell exits on the previous line; you won't see this" +``` - For a more thorough discussion about the `++` operator, see this article on [Postincrement vs Preincrement][incr] +For a more thorough discussion about the `++` operator, see this article on [Postincrement vs Preincrement][incr] - [incr]: https://thisvsthat.io/postincrement-vs-preincrement - ~~~~ +[incr]: https://thisvsthat.io/postincrement-vs-preincrement +~~~~ ## Other Places With Arithmetic Context @@ -95,7 +71,7 @@ There are a few other places that are arithmetic contexts: * The offset and length parts of the `${var:offset:length}` expansion. * The [`let` builtin command][let]. * Note that `(( expression ))` is preferred over `let "expresson"`. - Unlike the arithmetic conditional construct, the `let` expression is subject to filename-expansion and word-splitting, and it requires quoting. + Unlike the [arithmetic conditional][arithmetic-conditional] construct, the `let` expression is subject to filename-expansion and word-splitting, and it requires quoting. * Variable assignment, when the variable has the "integer attribute". ```bash @@ -157,8 +133,7 @@ area=$(( $1 * $2 )) ``` [arithmetic]: https://www.gnu.org/software/bash/manual/bash.html#Shell-Arithmetic -[conditional-if]: https://exercism.org/tracks/bash/concepts/conditionals#h-the-if-command -[looping-while]: https://exercism.org/tracks/bash/concepts/looping#h-while-loops +[arithmetic-conditional]: https://exercism.org/tracks/bash/concepts/conditionals#h-arithmetic-conditional [looping-for]: https://exercism.org/tracks/bash/concepts/looping#h-arithmetic-for-loop [arrays]: https://exercism.org/tracks/bash/concepts/arrays [let]: https://www.gnu.org/software/bash/manual/bash.html#index-let diff --git a/concepts/arithmetic/introduction.md b/concepts/arithmetic/introduction.md index 58de104a..9bead108 100644 --- a/concepts/arithmetic/introduction.md +++ b/concepts/arithmetic/introduction.md @@ -15,60 +15,36 @@ As we saw in the [Variables][variables] lesson, math can be performed in an arit x=$(( 3 + 4 )) ``` -## Arithmetic Conditional +## Arithmetic Command -The arithmetic conditional is performed within double parentheses _without_ the leading `$`: +The `(( expression ))` syntax is also used as a conditional, as described in the [Arithmetic Conditional][arithmetic-conditional] section of the Conditionals concept. + +It can also be used as a standalone command to modify a variable's value. ```bash -(( expression )) +(( x = x + 1 )) +echo "new value for x is $x" ``` -This does not expand to the result. -However it produces an _exit status_. - -* The exit status is 0 (success) if the expression's result is **non-zero**. -* The exit status is 1 (failure) if the expression's result is **zero**. - -~~~~exercism/note -Inside `((` Bash uses the same sense of true/false as the C language: zero is false, non-zero is true. -**This is the opposite of how Bash evaluates exit statuses!** -~~~~ - -This syntax is typically used in two places: - -1. as the COMMAND part of an [`if` statement][conditional-if] or a [`while` loop][looping-while]. - - ```bash - if (( x == y - 1 )); then ... - while (( y > 0 )); do ... - ``` +~~~~exercism/caution +Be careful using arithmetic this way combined with `set -e`. +If the expression evaluates to zero, the exit status will be non-zero and Bash will terminate. -1. as a standalone command to modify a variable's value. - - ```bash - (( x = x + 1 )) - echo "new value for x is $x" - ``` - - ~~~~exercism/caution - Be careful using arithmetic this way combined with `set -e`. - If the expression evaluates to zero, the exit status will be non-zero and Bash will terminate. - - ```bash - x=0 - (( x++ )) - echo "you will see this" - - set -e - x=0 - (( x++ )) - echo "shell exits on the previous line; you won't see this" - ``` +```bash +x=0 +(( x++ )) +echo "you will see this" + +set -e +x=0 +(( x++ )) +echo "shell exits on the previous line; you won't see this" +``` - For a more thorough discussion about the `++` operator, see this article on [Postincrement vs Preincrement][incr] +For a more thorough discussion about the `++` operator, see this article on [Postincrement vs Preincrement][incr] - [incr]: https://thisvsthat.io/postincrement-vs-preincrement - ~~~~ +[incr]: https://thisvsthat.io/postincrement-vs-preincrement +~~~~ ## Other Places With Arithmetic Context @@ -95,7 +71,7 @@ There are a few other places that are arithmetic contexts: * The offset and length parts of the `${var:offset:length}` expansion. * The [`let` builtin command][let]. * Note that `(( expression ))` is preferred over `let "expresson"`. - Unlike the arithmetic conditional construct, the `let` expression is subject to filename-expansion and word-splitting, and it requires quoting. + Unlike the [arithmetic conditional][arithmetic-conditional] construct, the `let` expression is subject to filename-expansion and word-splitting, and it requires quoting. * Variable assignment, when the variable has the "integer attribute". ```bash @@ -157,8 +133,7 @@ area=$(( $1 * $2 )) ``` [arithmetic]: https://www.gnu.org/software/bash/manual/bash.html#Shell-Arithmetic -[conditional-if]: https://exercism.org/tracks/bash/concepts/conditionals#h-the-if-command -[looping-while]: https://exercism.org/tracks/bash/concepts/looping#h-while-loops +[arithmetic-conditional]: https://exercism.org/tracks/bash/concepts/conditionals#h-arithmetic-conditional [looping-for]: https://exercism.org/tracks/bash/concepts/looping#h-arithmetic-for-loop [arrays]: https://exercism.org/tracks/bash/concepts/arrays [let]: https://www.gnu.org/software/bash/manual/bash.html#index-let diff --git a/concepts/conditionals/about.md b/concepts/conditionals/about.md index 24ba758e..8206894d 100644 --- a/concepts/conditionals/about.md +++ b/concepts/conditionals/about.md @@ -35,23 +35,34 @@ The CONDITIONAL_COMMANDS are executed, and Cascading branches can be given with `elif` ```bash -if CONDITIONAL_COMMANDS -then TRUE_COMMANDS -elif CONDITIONAL_COMMANDS_2 -then TRUE_COMMANDS_2 +if CONDITIONAL_COMMANDS; then + TRUE_COMMANDS +elif CONDITIONAL_COMMANDS_2; then + TRUE_COMMANDS_2 # more elif branches ... -else FALSE_COMMANDS +else + FALSE_COMMANDS fi ``` There must be a semicolon or a newline before the `then`, `elif`, `else` and `fi` words. +~~~~exercism/note +The accepted style is to "cuddle" `then`: put `; then` on the same line as `if` and `elif`, as in the example above. +Bash also accepts `then` on a line of its own, but you will see that less often. +(See the [Google Shell Style Guide][goog-control].) + +[goog-control]: https://google.github.io/styleguide/shellguide.html#s5.4-control-flow +~~~~ + To emphasize: it is the **exit status** of the conditional commands that controls the flow. As an example, `grep` returns 0 if a match is found, and 1 if a match is not found. The `-q` option suppresses output, only producing the exit status. ```bash -if grep -q "my pattern" my_file; then echo "the pattern is found in the file"; fi +if grep -q "my pattern" my_file; then + echo "the pattern is found in the file" +fi ``` ## "[" and "test" Commands @@ -126,13 +137,44 @@ if [[ -n $str ]]; then echo "not empty"; else echo "empty"; fi In addition, `[[` provides * the `=~` regular-expression matching operator, -* `==` and `!=` operate as a glob-pattern matching operator, +* `==` and `!=` compare strings. An unquoted right-hand side is treated as a glob pattern; quote it to match it literally, * `&&` and `||` as logical operators (special parsing rule), * `<` and `>` as "bare" string comparison operators (special parsing rule: because these are redirection symbols, in `[` they must be escaped). +```bash +[[ $name == "Alice" ]] # string equality +[[ $name == A* ]] # pattern: starts with "A" +``` + It is widely held that these special features offer so much benefit that `[[` should be used exclusively. (For example, the [Google Shell Style Guide][goog-test].) +## Arithmetic Conditional + +The arithmetic conditional is performed within double parentheses _without_ the leading `$`: + +```bash +(( expression )) +``` + +This does not expand to the result of the expression. +However it produces an _exit status_. + +* The exit status is 0 (success) if the expression's result is **non-zero**. +* The exit status is 1 (failure) if the expression's result is **zero**. + +~~~~exercism/note +Inside `((` Bash uses the same sense of true/false as the C language: zero is false, non-zero is true. +**This is the opposite of how Bash evaluates exit statuses!** +~~~~ + +This syntax is typically used as the COMMAND part of an `if` statement or a [`while` loop][looping-while]. + +```bash +if (( x == y - 1 )); then ... +while (( y > 0 )); do ... +``` + ## The "case" Command [`case`][case] is another control flow command. @@ -171,4 +213,5 @@ Each COMMANDS clause must end with **two** semicolons, `;;`. [case]: https://www.gnu.org/software/bash/manual/bash.html#index-case [cond-expr]: https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions [cond-construct]: https://www.gnu.org/software/bash/manual/bash.html#index-_005b_005b +[looping-while]: https://exercism.org/tracks/bash/concepts/looping#h-while-loops [goog-test]: https://google.github.io/styleguide/shellguide.html#s6.3-tests diff --git a/concepts/conditionals/introduction.md b/concepts/conditionals/introduction.md index d9ee6549..c31e38ff 100644 --- a/concepts/conditionals/introduction.md +++ b/concepts/conditionals/introduction.md @@ -35,23 +35,34 @@ The CONDITIONAL_COMMANDS are executed, and Cascading branches can be given with `elif` ```bash -if CONDITIONAL_COMMANDS -then TRUE_COMMANDS -elif CONDITIONAL_COMMANDS_2 -then TRUE_COMMANDS_2 +if CONDITIONAL_COMMANDS; then + TRUE_COMMANDS +elif CONDITIONAL_COMMANDS_2; then + TRUE_COMMANDS_2 # more elif branches ... -else FALSE_COMMANDS +else + FALSE_COMMANDS fi ``` There must be a semicolon or a newline before the `then`, `elif`, `else` and `fi` words. +~~~~exercism/note +The accepted style is to "cuddle" `then`: put `; then` on the same line as `if` and `elif`, as in the example above. +Bash also accepts `then` on a line of its own, but you will see that less often. +(See the [Google Shell Style Guide][goog-control].) + +[goog-control]: https://google.github.io/styleguide/shellguide.html#s5.4-control-flow +~~~~ + To emphasize: it is the **exit status** of the conditional commands that controls the flow. As an example, `grep` returns 0 if a match is found, and 1 if a match is not found. The `-q` option suppresses output, only producing the exit status. ```bash -if grep -q "my pattern" my_file; then echo "the pattern is found in the file"; fi +if grep -q "my pattern" my_file; then + echo "the pattern is found in the file" +fi ``` ## "[" and "test" Commands @@ -126,13 +137,44 @@ if [[ -n $str ]]; then echo "not empty"; else echo "empty"; fi In addition, `[[` provides * the `=~` regular-expression matching operator, -* `==` and `!=` operate as a glob-pattern matching operator, +* `==` and `!=` compare strings. An unquoted right-hand side is treated as a glob pattern; quote it to match it literally, * `&&` and `||` as logical operators (special parsing rule), * `<` and `>` as "bare" string comparison operators (special parsing rule: because these are redirection symbols, in `[` they must be escaped). +```bash +[[ $name == "Alice" ]] # string equality +[[ $name == A* ]] # pattern: starts with "A" +``` + It is widely held that these special features offer so much benefit that `[[` should be used exclusively. (For example, the [Google Shell Style Guide][goog-test].) +## Arithmetic Conditional + +The arithmetic conditional is performed within double parentheses _without_ the leading `$`: + +```bash +(( expression )) +``` + +This does not expand to the result of the expression. +However it produces an _exit status_. + +* The exit status is 0 (success) if the expression's result is **non-zero**. +* The exit status is 1 (failure) if the expression's result is **zero**. + +~~~~exercism/note +Inside `((` Bash uses the same sense of true/false as the C language: zero is false, non-zero is true. +**This is the opposite of how Bash evaluates exit statuses!** +~~~~ + +This syntax is typically used as the COMMAND part of an `if` statement or a [`while` loop][looping-while]. + +```bash +if (( x == y - 1 )); then ... +while (( y > 0 )); do ... +``` + ## The "case" Command [`case`][case] is another control flow command. @@ -171,4 +213,5 @@ Each COMMANDS clause must end with **two** semicolons, `;;`. [case]: https://www.gnu.org/software/bash/manual/bash.html#index-case [cond-expr]: https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions [cond-construct]: https://www.gnu.org/software/bash/manual/bash.html#index-_005b_005b +[looping-while]: https://exercism.org/tracks/bash/concepts/looping#h-while-loops [goog-test]: https://google.github.io/styleguide/shellguide.html#s6.3-tests diff --git a/config.json b/config.json index 00d988d4..2d24bd67 100644 --- a/config.json +++ b/config.json @@ -67,6 +67,18 @@ "variables" ], "status": "wip" + }, + { + "slug": "vehicle-purchase", + "name": "Vehicle Purchase", + "uuid": "d75f0220-d4ab-4138-881e-0b1d7a71c813", + "concepts": [ + "conditionals" + ], + "prerequisites": [ + "quoting" + ], + "status": "wip" } ], "practice": [ diff --git a/exercises/concept/road-map.md b/exercises/concept/road-map.md index 5f5f74c5..615addfc 100644 --- a/exercises/concept/road-map.md +++ b/exercises/concept/road-map.md @@ -6,9 +6,9 @@ Ref: [Syllabus][syllabus] | ------- | -------- | ------------ | ------ | | [commands-and-arguments][commands-and-arguments] | shopping | [980][980] | - | | [variables][variables] | [lasagna][lasagna] | [985][985] | cmds-args | -| [quoting][quoting] | messy-downloads (OC) | [branch][concept-expercise-quoting] | variables | +| [quoting][quoting] | messy-downloads (OC) | [987][987] | variables | | [conditionals][conditionals] | [vehicle-purchase][vehicle-purchase] | | quoting | -| [arithmetic][arithmetic] | [vehicle-purchase][vehicle-purchase] | | quoting | +| [arithmetic][arithmetic] | | | conditionals | | [looping][looping] | | | conditionals | | [arrays][arrays] | | | quoting | | [more-arrays][more-arrays] | | | arrays | @@ -24,7 +24,7 @@ Ref: [Syllabus][syllabus] [lasagna]: https://github.com/glennj/exercism-bash/tree/concept-exercise-lasagna [985]: https://github.com/exercism/bash/pull/985 [quoting]: https://github.com/exercism/bash/tree/main/concepts/quoting -[concept-expercise-quoting]: https://github.com/glennj/exercism-bash/tree/concept-expercise-quoting +[987]: https://github.com/exercism/bash/pull/987 [conditionals]: https://github.com/exercism/bash/tree/main/concepts/conditionals [vehicle-purchase]: https://exercism.org/docs/building/tracks/stories/conditionals-vehicle-purchase [arithmetic]: https://github.com/exercism/bash/tree/main/concepts/arithmetic diff --git a/exercises/concept/vehicle-purchase/.docs/hints.md b/exercises/concept/vehicle-purchase/.docs/hints.md new file mode 100644 index 00000000..23d9fa47 --- /dev/null +++ b/exercises/concept/vehicle-purchase/.docs/hints.md @@ -0,0 +1,29 @@ +# Hints + +## General + +- Quote your variable expansions in conditional expressions when you use `[` or `test`. + Inside `[[ ... ]]`, variables are not split or glob-expanded, but quoting the right-hand side of `==` makes it match literally. +- Read about [conditional constructs][conditional-constructs] in the Bash manual, or the track's conditionals concept page. + +## 1. Determine if you will need a license + +- Use an [`if` command][conditional-constructs] with an `else` branch. +- Use `[[ ... ]]` to compare the kind with `==`. +- Inside `[[ ... ]]`, you can combine two comparisons with `||`. + +## 2. Choose between two potential vehicles to buy + +- Inside `[[ ... ]]`, the `<` operator compares strings in dictionary order. +- Store the winning option in a variable, then build the sentence once. + +## 3. Calculate an estimation for the price of a used vehicle + +- Start by determining the percentage based on the age of the vehicle. + Use an `if`, `elif`, `else` command. +- Compare numbers with `<` and `>` inside an [arithmetic conditional][conditional-constructs], `(( ... ))`. + Inside `[[ ... ]]`, those operators compare strings instead. +- To calculate the result, apply the percentage to the original price. + For example, 30% of `x` can be calculated with arithmetic expansion: `$(( x * 30 / 100 ))`. + +[conditional-constructs]: https://www.gnu.org/software/bash/manual/bash.html#Conditional-Constructs diff --git a/exercises/concept/vehicle-purchase/.docs/instructions.md b/exercises/concept/vehicle-purchase/.docs/instructions.md new file mode 100644 index 00000000..ff3051d0 --- /dev/null +++ b/exercises/concept/vehicle-purchase/.docs/instructions.md @@ -0,0 +1,70 @@ +# Instructions + +In this exercise, you will write some code to help you prepare to buy a vehicle. + +You have three tasks: determine if you will need a license; choose between two vehicles; and estimate the acceptable price for a used vehicle. +Each task adds to the same script, so preserve the output from the previous tasks. +Do not print anything other than the requested output. + +The script accepts five command-line arguments: + +1. the kind of vehicle, such as `car` or `bike`. +2. the first vehicle you are considering. +3. the second vehicle you are considering. +4. the original price of the vehicle, as a whole number. +5. the age of the vehicle in years, as a whole number. + +Run the script with: + +```bash +bash vehicle_purchase.sh car 'Wuling Hongguang' 'Toyota Corolla' 1000 5 +``` + +## 1. Determine if you will need a license + +Some kinds of vehicles require a license to operate them. +Assume only the kinds `car` and `truck` require a license; everything else can be operated without a license. + +The kind of vehicle is given as the first argument. +Print `License needed for KIND: yes` or `License needed for KIND: no`, with `KIND` replaced by that argument. + +```text +License needed for car: yes +``` + +## 2. Choose between two potential vehicles to buy + +You have evaluated your options of available vehicles. +You managed to narrow it down to two options but you need help making the final decision. +The decision is the option that comes first in dictionary order. + +Print the decision as a sentence. +For the arguments `'Wuling Hongguang' 'Toyota Corolla'`: + +```text +Toyota Corolla is clearly the better choice. +``` + +## 3. Calculate an estimation for the price of a used vehicle + +Now that you have made your decision, you want to make sure you get a fair price at the dealership. +Since you are interested in buying a used vehicle, the price depends on how old the vehicle is. +For a rough estimate, assume: + +- if the vehicle is less than 3 years old, it costs 80% of the original price it had when it was brand new. +- if it is more than 10 years old, it costs 50%. +- if the vehicle is at least 3 years old but not older than 10 years, it costs 70% of the original price. + +~~~~exercism/note +Bash cannot do floating point math, so you can't multiply by a decimal such as `0.3`. +Express each percentage with an integer arithmetic expression instead: 30% of `x` is `$(( x * 30 / 100 ))`. +Multiply before you divide. +Integer division discards the remainder, so if you divide 30 by 100 first, you get 0, and the price comes out as 0 too. +~~~~ + +Print the estimated price. +For an original price of `1000` and an age of `5`: + +```text +Estimated price: 700 +``` diff --git a/exercises/concept/vehicle-purchase/.docs/introduction.md b/exercises/concept/vehicle-purchase/.docs/introduction.md new file mode 100644 index 00000000..6fd0bbdf --- /dev/null +++ b/exercises/concept/vehicle-purchase/.docs/introduction.md @@ -0,0 +1,219 @@ +# Introduction + +## Conditionals + +### Truthiness in Bash + +Bash does not have the concept of boolean values. +There are strings and numbers, and arrays of strings or numbers. +So how do conditional commands deal with true and false? + +### Exit Status + +Commands produce an _exit status_ when they end. +An exit status is an integer between 0 and 255 inclusive. +Bash considers a **zero** exit status to represent _success_. +Any other exit status represents _failure_. + +This applies to all commands, including bash builtin commands, keywords, and functions. + +### The "if" Command + +The basic syntax of the `if` command is + +```bash +if CONDITIONAL_COMMANDS; then TRUE_COMMANDS; else FALSE_COMMANDS; fi +``` + +It starts with `if`; the condition commands are separated from the "success" commands by `then`; and it is terminated with `fi`. +The `else` clause is optional. + +CONDITIONAL_COMMANDS can be a single command or it can be a [list of commands][command-list]. + +The CONDITIONAL_COMMANDS are executed, and + +* if the exit status is zero (success), then the TRUE_COMMANDS are executed. +* if the exit status is non-zero (failure), then the FALSE_COMMANDS are executed, if they are present. + +Cascading branches can be given with `elif` +```bash +if CONDITIONAL_COMMANDS; then + TRUE_COMMANDS +elif CONDITIONAL_COMMANDS_2; then + TRUE_COMMANDS_2 +# more elif branches ... +else + FALSE_COMMANDS +fi +``` + +There must be a semicolon or a newline before the `then`, `elif`, `else` and `fi` words. + +~~~~exercism/note +The accepted style is to "cuddle" `then`: put `; then` on the same line as `if` and `elif`, as in the example above. +Bash also accepts `then` on a line of its own, but you will see that less often. +(See the [Google Shell Style Guide][goog-control].) + +[goog-control]: https://google.github.io/styleguide/shellguide.html#s5.4-control-flow +~~~~ + +To emphasize: it is the **exit status** of the conditional commands that controls the flow. +As an example, `grep` returns 0 if a match is found, and 1 if a match is not found. +The `-q` option suppresses output, only producing the exit status. + +```bash +if grep -q "my pattern" my_file; then + echo "the pattern is found in the file" +fi +``` + +### "[" and "test" Commands + +There is no special syntax around the CONDITIONAL_COMMANDS. +You may be used to seeing if statements that look like this: + +```if +if [ "$password" = "secure" ]; then + echo "Welcome!" +fi +``` + +`[` is not special syntax. +It is a _command_ that evaluates the conditional expression and exits with a success/failure status. +Like all commands, whitespace is **required** between it and its arguments. + +`[` is actually a synonym for the `test` command. +They are exactly the same, except that the last argument to `[` _must_ be `]`. + +#### Conditional Expressions + +Within `[` and `]`, you write a conditional expression. +Some typical conditional expressions include: + +```bash +[ -f "$filename" ] # file operations +[ "$string1" = "$string2" ] # string comparisons +[ "$num1" -eq "$num2" ] # arithmetic comparisons +``` + +There are many more operations available; they are listed in the [Bash Conditional Expressions][cond-expr] section of the manual. + +~~~~exercism/note +In the examples above, notice that all the variables are quoted. +The `test` and `[` commands are plain commands, where the arguments are subject to word splitting and filename expansion like any other command. + +This is important to point out because conditional expressions are evaluated differently based on _how many arguments_ you provide: + +* 0 arguments: the exit status is non-zero (failure) +* 1 argument: the exit status is zero (success) if the argument is not empty, non-zero if it is empty. +* 2 arguments: the first argument must be a unary operator (such as `[ -z "$name" ]`), or a `!` (negating the status of the 1-argument test) +* and [more][test]. + +You can get unexpected results if you forget to quote: + +```bash +str="" + +# this prints "empty" +if [ -n "$str" ]; then echo "not empty"; else echo "empty"; fi + +# leaving the variable unquoted results in incorrect "not empty" output +if [ -n $str ]; then echo "not empty"; else echo "empty"; fi +``` + +[test]: https://www.gnu.org/software/bash/manual/bash.html#index-test +~~~~ + +### "[[" Keyword + +The [`[[...]]` conditional construct][cond-construct] is not a _command_, it is a **keyword**. +This means that, although it is handled like any other command, it has special parsing rules. +What's special about `[[` is that the variables expanded within it are **not** subject to word splitting or filename expansion. +That means this command acts as you expect, even without quoting. + +```bash +if [[ -n $str ]]; then echo "not empty"; else echo "empty"; fi +``` + +`[[` supports all the conditional expressions that `test` and `[` can handle. +In addition, `[[` provides + +* the `=~` regular-expression matching operator, +* `==` and `!=` compare strings. An unquoted right-hand side is treated as a glob pattern; quote it to match it literally, +* `&&` and `||` as logical operators (special parsing rule), +* `<` and `>` as "bare" string comparison operators (special parsing rule: because these are redirection symbols, in `[` they must be escaped). + +```bash +[[ $name == "Alice" ]] # string equality +[[ $name == A* ]] # pattern: starts with "A" +``` + +It is widely held that these special features offer so much benefit that `[[` should be used exclusively. +(For example, the [Google Shell Style Guide][goog-test].) + +### Arithmetic Conditional + +The arithmetic conditional is performed within double parentheses _without_ the leading `$`: + +```bash +(( expression )) +``` + +This does not expand to the result of the expression. +However it produces an _exit status_. + +* The exit status is 0 (success) if the expression's result is **non-zero**. +* The exit status is 1 (failure) if the expression's result is **zero**. + +~~~~exercism/note +Inside `((` Bash uses the same sense of true/false as the C language: zero is false, non-zero is true. +**This is the opposite of how Bash evaluates exit statuses!** +~~~~ + +This syntax is typically used as the COMMAND part of an `if` statement or a [`while` loop][looping-while]. + +```bash +if (( x == y - 1 )); then ... +while (( y > 0 )); do ... +``` + +### The "case" Command + +[`case`][case] is another control flow command. +It is like a "switch" statement in other languages. + +```bash +case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac +``` + +The WORD is matched against each PATTERN. +When one matches, the COMMANDS are executed. + +```bash +read -p "Guess the secret word: " word +case "$word" in + secret) echo "Yes, you guessed it!" ;; + ??????) echo "That's the right number of letters." ;; + s*) echo "You guessed the first letter." ;; + *) echo "Not even close! Try again." ;; +esac +``` + +Each COMMANDS clause must end with **two** semicolons, `;;`. + +~~~~exercism/note +1. There are alternatives that provide functionality for fall-through command execution, and for pattern matching to continue. + Check [the manual][case] for details. +2. Recall that we talked about patterns in the [Filename Expansion][glob] section of the Quoting concept. + +[case]: https://www.gnu.org/software/bash/manual/bash.html#index-case +[glob]: https://exercism.org/tracks/bash/concepts/quoting#h-filename-expansion +~~~~ + +[command-list]: https://www.gnu.org/software/bash/manual/bash.html#Lists +[if]: https://www.gnu.org/software/bash/manual/bash.html#index-if +[case]: https://www.gnu.org/software/bash/manual/bash.html#index-case +[cond-expr]: https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions +[cond-construct]: https://www.gnu.org/software/bash/manual/bash.html#index-_005b_005b +[looping-while]: https://exercism.org/tracks/bash/concepts/looping#h-while-loops +[goog-test]: https://google.github.io/styleguide/shellguide.html#s6.3-tests diff --git a/exercises/concept/vehicle-purchase/.docs/introduction.md.tpl b/exercises/concept/vehicle-purchase/.docs/introduction.md.tpl new file mode 100644 index 00000000..4626cc01 --- /dev/null +++ b/exercises/concept/vehicle-purchase/.docs/introduction.md.tpl @@ -0,0 +1,3 @@ +# Introduction + +%{concept:conditionals} diff --git a/exercises/concept/vehicle-purchase/.meta/config.json b/exercises/concept/vehicle-purchase/.meta/config.json new file mode 100644 index 00000000..28c6ae21 --- /dev/null +++ b/exercises/concept/vehicle-purchase/.meta/config.json @@ -0,0 +1,23 @@ +{ + "authors": [ + "glennj" + ], + "files": { + "solution": [ + "vehicle_purchase.sh" + ], + "test": [ + "vehicle_purchase.bats" + ], + "exemplar": [ + ".meta/exemplar.sh" + ], + "invalidator": [ + "bats-extra.bash" + ] + }, + "forked_from": [ + "jq/vehicle-purchase" + ], + "blurb": "Learn about conditionals while preparing for your next vehicle purchase." +} diff --git a/exercises/concept/vehicle-purchase/.meta/design.md b/exercises/concept/vehicle-purchase/.meta/design.md new file mode 100644 index 00000000..dc7ba903 --- /dev/null +++ b/exercises/concept/vehicle-purchase/.meta/design.md @@ -0,0 +1,37 @@ +# Design + +## Goal + +The goal of this exercise is to introduce conditionals in Bash scripts. + +## Learning objectives + +- Know that a zero exit status is "true" and a non-zero exit status is "false". +- Write an `if` command with `then` and `else` branches. +- Write an `if` command with `elif` branches. +- Use `[[ ... ]]` to compare strings for equality with `==`. +- Combine two comparisons within `[[ ... ]]` with `||`. +- Compare strings in dictionary order with `<` within `[[ ... ]]`. +- Compare integers within `(( ... ))` using `<` and `>`. + +## Out of scope + +- The `case` command. +- Writing conditions with `[` or `test`, and how they differ from `[[`. +- File tests, the `-n` and `-z` tests, and operators such as `-eq`. +- Pattern matching with `==`, and regular expression matching with `=~`. +- The `!=` operator, `&&` within `[[ ... ]]`, `>` for strings, and negation with `!`. +- Using an ordinary command, or a list of commands, as the condition. +- Using `&&` and `||` between commands as a replacement for `if`. +- Using a number by itself as a `(( ... ))` condition. +- Using `(( ... ))` in loops, or as a standalone command to modify variables. + +## Concepts + +The Concepts this exercise unlocks are: + +- `conditionals` + +## Prerequisites + +- `quoting` diff --git a/exercises/concept/vehicle-purchase/.meta/exemplar.sh b/exercises/concept/vehicle-purchase/.meta/exemplar.sh new file mode 100644 index 00000000..2ebf2b1d --- /dev/null +++ b/exercises/concept/vehicle-purchase/.meta/exemplar.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +kind=$1 +option1=$2 +option2=$3 +original_price=$4 +age=$5 + +# Task 1 +if [[ $kind == "car" || $kind == "truck" ]]; then + license_needed="yes" +else + license_needed="no" +fi +echo "License needed for ${kind}: ${license_needed}" + +# Task 2 +if [[ $option1 < $option2 ]]; then + choice=$option1 +else + choice=$option2 +fi +echo "${choice} is clearly the better choice." + +# Task 3 +if (( age < 3 )); then + percentage=80 +elif (( age > 10 )); then + percentage=50 +else + percentage=70 +fi +price=$(( original_price * percentage / 100 )) +echo "Estimated price: ${price}" diff --git a/exercises/concept/vehicle-purchase/bats-extra.bash b/exercises/concept/vehicle-purchase/bats-extra.bash new file mode 100644 index 00000000..7cef63ea --- /dev/null +++ b/exercises/concept/vehicle-purchase/bats-extra.bash @@ -0,0 +1,642 @@ +# This is the source code for bats-support and bats-assert, concatenated +# * https://github.com/bats-core/bats-support +# * https://github.com/bats-core/bats-assert +# +# Comments have been removed to save space. See the git repos for full source code. + +############################################################ +# +# bats-support - Supporting library for Bats test helpers +# +# Written in 2016 by Zoltan Tombol +# +# To the extent possible under law, the author(s) have dedicated all +# copyright and related and neighboring rights to this software to the +# public domain worldwide. This software is distributed without any +# warranty. +# +# You should have received a copy of the CC0 Public Domain Dedication +# along with this software. If not, see +# . +# + +# shellcheck disable=SC2120 # (warning): fail references arguments, but none are ever passed. +fail() { + # shellcheck disable=2015 # (info): Note that A && B || C is not if-then-else. C may run when A is true. + (( $# == 0 )) && batslib_err || batslib_err "$@" + return 1 +} + +batslib_is_caller() { + local -i is_mode_direct=1 + + # Handle options. + while (( $# > 0 )); do + case "$1" in + -i|--indirect) is_mode_direct=0; shift ;; + --) shift; break ;; + *) break ;; + esac + done + + # Arguments. + local -r func="$1" + + # Check call stack. + if (( is_mode_direct )); then + [[ $func == "${FUNCNAME[2]}" ]] && return 0 + else + local -i depth + for (( depth=2; depth<${#FUNCNAME[@]}; ++depth )); do + [[ $func == "${FUNCNAME[$depth]}" ]] && return 0 + done + fi + + return 1 +} + +batslib_err() { + { if (( $# > 0 )); then + echo "$@" + else + cat - + fi + } >&2 +} + +batslib_count_lines() { + local -i n_lines=0 + local line + while IFS='' read -r line || [[ -n $line ]]; do + (( ++n_lines )) + done < <(printf '%s' "$1") + echo "$n_lines" +} + +batslib_is_single_line() { + for string in "$@"; do + (( $(batslib_count_lines "$string") > 1 )) && return 1 + done + return 0 +} + +batslib_get_max_single_line_key_width() { + local -i max_len=-1 + while (( $# != 0 )); do + local -i key_len="${#1}" + batslib_is_single_line "$2" && (( key_len > max_len )) && max_len="$key_len" + shift 2 + done + echo "$max_len" +} + +batslib_print_kv_single() { + local -ir col_width="$1"; shift + while (( $# != 0 )); do + printf '%-*s : %s\n' "$col_width" "$1" "$2" + shift 2 + done +} + +batslib_print_kv_multi() { + while (( $# != 0 )); do + printf '%s (%d lines):\n' "$1" "$( batslib_count_lines "$2" )" + printf '%s\n' "$2" + shift 2 + done +} + +batslib_print_kv_single_or_multi() { + local -ir width="$1"; shift + local -a pairs=( "$@" ) + + local -a values=() + local -i i + for (( i=1; i < ${#pairs[@]}; i+=2 )); do + values+=( "${pairs[$i]}" ) + done + + if batslib_is_single_line "${values[@]}"; then + batslib_print_kv_single "$width" "${pairs[@]}" + else + local -i i + for (( i=1; i < ${#pairs[@]}; i+=2 )); do + pairs[i]="$( batslib_prefix < <(printf '%s' "${pairs[$i]}") )" + done + batslib_print_kv_multi "${pairs[@]}" + fi +} + +batslib_prefix() { + local -r prefix="${1:- }" + local line + while IFS='' read -r line || [[ -n $line ]]; do + printf '%s%s\n' "$prefix" "$line" + done +} + +batslib_mark() { + local -r symbol="$1"; shift + # Sort line numbers. + # shellcheck disable=SC2046 # (warning): Quote this to prevent word splitting. + set -- $( sort -nu <<< "$( printf '%d\n' "$@" )" ) + + local line + local -i idx=0 + while IFS='' read -r line || [[ -n $line ]]; do + if (( ${1:--1} == idx )); then + printf '%s\n' "${symbol}${line:${#symbol}}" + shift + else + printf '%s\n' "$line" + fi + (( ++idx )) + done +} + +batslib_decorate() { + echo + echo "-- $1 --" + cat - + echo '--' + echo +} + +############################################################ + +assert() { + if ! "$@"; then + batslib_print_kv_single 10 'expression' "$*" \ + | batslib_decorate 'assertion failed' \ + | fail + fi +} + +assert_equal() { + if [[ $1 != "$2" ]]; then + batslib_print_kv_single_or_multi 8 \ + 'expected' "$2" \ + 'actual' "$1" \ + | batslib_decorate 'values do not equal' \ + | fail + fi +} + +assert_failure() { + : "${output?}" + : "${status?}" + + (( $# > 0 )) && local -r expected="$1" + if (( status == 0 )); then + batslib_print_kv_single_or_multi 6 'output' "$output" \ + | batslib_decorate 'command succeeded, but it was expected to fail' \ + | fail + elif (( $# > 0 )) && (( status != expected )); then + { local -ir width=8 + batslib_print_kv_single "$width" \ + 'expected' "$expected" \ + 'actual' "$status" + batslib_print_kv_single_or_multi "$width" \ + 'output' "$output" + } \ + | batslib_decorate 'command failed as expected, but status differs' \ + | fail + fi +} + +assert_line() { + local -i is_match_line=0 + local -i is_mode_partial=0 + local -i is_mode_regexp=0 + : "${lines?}" + + # Handle options. + while (( $# > 0 )); do + case "$1" in + -n|--index) + if (( $# < 2 )) || ! [[ $2 =~ ^([0-9]|[1-9][0-9]+)$ ]]; then + echo "\`--index' requires an integer argument: \`$2'" \ + | batslib_decorate 'ERROR: assert_line' \ + | fail + return $? + fi + is_match_line=1 + local -ri idx="$2" + shift 2 + ;; + -p|--partial) is_mode_partial=1; shift ;; + -e|--regexp) is_mode_regexp=1; shift ;; + --) shift; break ;; + *) break ;; + esac + done + + if (( is_mode_partial )) && (( is_mode_regexp )); then + echo "\`--partial' and \`--regexp' are mutually exclusive" \ + | batslib_decorate 'ERROR: assert_line' \ + | fail + return $? + fi + + # Arguments. + local -r expected="$1" + + if (( is_mode_regexp == 1 )) && [[ '' =~ $expected ]] || (( $? == 2 )); then + echo "Invalid extended regular expression: \`$expected'" \ + | batslib_decorate 'ERROR: assert_line' \ + | fail + return $? + fi + + # Matching. + if (( is_match_line )); then + # Specific line. + if (( is_mode_regexp )); then + if ! [[ ${lines[$idx]} =~ $expected ]]; then + batslib_print_kv_single 6 \ + 'index' "$idx" \ + 'regexp' "$expected" \ + 'line' "${lines[$idx]}" \ + | batslib_decorate 'regular expression does not match line' \ + | fail + fi + elif (( is_mode_partial )); then + if [[ ${lines[$idx]} != *"$expected"* ]]; then + batslib_print_kv_single 9 \ + 'index' "$idx" \ + 'substring' "$expected" \ + 'line' "${lines[$idx]}" \ + | batslib_decorate 'line does not contain substring' \ + | fail + fi + else + if [[ ${lines[$idx]} != "$expected" ]]; then + batslib_print_kv_single 8 \ + 'index' "$idx" \ + 'expected' "$expected" \ + 'actual' "${lines[$idx]}" \ + | batslib_decorate 'line differs' \ + | fail + fi + fi + else + # Contained in output. + if (( is_mode_regexp )); then + local -i idx + for (( idx = 0; idx < ${#lines[@]}; ++idx )); do + [[ ${lines[$idx]} =~ $expected ]] && return 0 + done + { local -ar single=( 'regexp' "$expected" ) + local -ar may_be_multi=( 'output' "$output" ) + local -ir width="$( batslib_get_max_single_line_key_width "${single[@]}" "${may_be_multi[@]}" )" + batslib_print_kv_single "$width" "${single[@]}" + batslib_print_kv_single_or_multi "$width" "${may_be_multi[@]}" + } \ + | batslib_decorate 'no output line matches regular expression' \ + | fail + elif (( is_mode_partial )); then + local -i idx + for (( idx = 0; idx < ${#lines[@]}; ++idx )); do + [[ ${lines[$idx]} == *"$expected"* ]] && return 0 + done + { local -ar single=( 'substring' "$expected" ) + local -ar may_be_multi=( 'output' "$output" ) + local -ir width="$( batslib_get_max_single_line_key_width "${single[@]}" "${may_be_multi[@]}" )" + batslib_print_kv_single "$width" "${single[@]}" + batslib_print_kv_single_or_multi "$width" "${may_be_multi[@]}" + } \ + | batslib_decorate 'no output line contains substring' \ + | fail + else + local -i idx + for (( idx = 0; idx < ${#lines[@]}; ++idx )); do + [[ ${lines[$idx]} == "$expected" ]] && return 0 + done + { local -ar single=( 'line' "$expected" ) + local -ar may_be_multi=( 'output' "$output" ) + local -ir width="$( batslib_get_max_single_line_key_width "${single[@]}" "${may_be_multi[@]}" )" + batslib_print_kv_single "$width" "${single[@]}" + batslib_print_kv_single_or_multi "$width" "${may_be_multi[@]}" + } \ + | batslib_decorate 'output does not contain line' \ + | fail + fi + fi +} + +assert_output() { + local -i is_mode_partial=0 + local -i is_mode_regexp=0 + local -i is_mode_nonempty=0 + local -i use_stdin=0 + : "${output?}" + + # Handle options. + if (( $# == 0 )); then + is_mode_nonempty=1 + fi + + while (( $# > 0 )); do + case "$1" in + -p|--partial) is_mode_partial=1; shift ;; + -e|--regexp) is_mode_regexp=1; shift ;; + -|--stdin) use_stdin=1; shift ;; + --) shift; break ;; + *) break ;; + esac + done + + if (( is_mode_partial )) && (( is_mode_regexp )); then + echo "\`--partial' and \`--regexp' are mutually exclusive" \ + | batslib_decorate 'ERROR: assert_output' \ + | fail + return $? + fi + + # Arguments. + local expected + if (( use_stdin )); then + expected="$(cat -)" + else + expected="${1-}" + fi + + # Matching. + if (( is_mode_nonempty )); then + if [ -z "$output" ]; then + echo 'expected non-empty output, but output was empty' \ + | batslib_decorate 'no output' \ + | fail + fi + elif (( is_mode_regexp )); then + # shellcheck disable=SC2319 # (warning) `$?` in a conditional expression: https://github.com/koalaman/shellcheck/wiki/SC2319 + if [[ '' =~ $expected ]] || (( $? == 2 )); then + # the regex matches an empty string or it is syntactically incorrect. + echo "Invalid extended regular expression: \`$expected'" \ + | batslib_decorate 'ERROR: assert_output' \ + | fail + elif ! [[ $output =~ $expected ]]; then + batslib_print_kv_single_or_multi 6 \ + 'regexp' "$expected" \ + 'output' "$output" \ + | batslib_decorate 'regular expression does not match output' \ + | fail + fi + elif (( is_mode_partial )); then + if [[ $output != *"$expected"* ]]; then + batslib_print_kv_single_or_multi 9 \ + 'substring' "$expected" \ + 'output' "$output" \ + | batslib_decorate 'output does not contain substring' \ + | fail + fi + else + if [[ $output != "$expected" ]]; then + batslib_print_kv_single_or_multi 8 \ + 'expected' "$expected" \ + 'actual' "$output" \ + | batslib_decorate 'output differs' \ + | fail + fi + fi +} + +assert_success() { + : "${output?}" + : "${status?}" + + if (( status != 0 )); then + { local -ir width=6 + batslib_print_kv_single "$width" 'status' "$status" + batslib_print_kv_single_or_multi "$width" 'output' "$output" + } \ + | batslib_decorate 'command failed' \ + | fail + fi +} + +refute() { + if "$@"; then + batslib_print_kv_single 10 'expression' "$*" \ + | batslib_decorate 'assertion succeeded, but it was expected to fail' \ + | fail + fi +} + +refute_line() { + local -i is_match_line=0 + local -i is_mode_partial=0 + local -i is_mode_regexp=0 + : "${lines?}" + + # Handle options. + while (( $# > 0 )); do + case "$1" in + -n|--index) + if (( $# < 2 )) || ! [[ $2 =~ ^([0-9]|[1-9][0-9]+)$ ]]; then + echo "\`--index' requires an integer argument: \`$2'" \ + | batslib_decorate 'ERROR: refute_line' \ + | fail + return $? + fi + is_match_line=1 + local -ri idx="$2" + shift 2 + ;; + -p|--partial) is_mode_partial=1; shift ;; + -e|--regexp) is_mode_regexp=1; shift ;; + --) shift; break ;; + *) break ;; + esac + done + + if (( is_mode_partial )) && (( is_mode_regexp )); then + echo "\`--partial' and \`--regexp' are mutually exclusive" \ + | batslib_decorate 'ERROR: refute_line' \ + | fail + return $? + fi + + # Arguments. + local -r unexpected="$1" + + if (( is_mode_regexp == 1 )) && [[ '' =~ $unexpected ]] || (( $? == 2 )); then + echo "Invalid extended regular expression: \`$unexpected'" \ + | batslib_decorate 'ERROR: refute_line' \ + | fail + return $? + fi + + # Matching. + if (( is_match_line )); then + # Specific line. + if (( is_mode_regexp )); then + if [[ ${lines[$idx]} =~ $unexpected ]]; then + batslib_print_kv_single 6 \ + 'index' "$idx" \ + 'regexp' "$unexpected" \ + 'line' "${lines[$idx]}" \ + | batslib_decorate 'regular expression should not match line' \ + | fail + fi + elif (( is_mode_partial )); then + if [[ ${lines[$idx]} == *"$unexpected"* ]]; then + batslib_print_kv_single 9 \ + 'index' "$idx" \ + 'substring' "$unexpected" \ + 'line' "${lines[$idx]}" \ + | batslib_decorate 'line should not contain substring' \ + | fail + fi + else + if [[ ${lines[$idx]} == "$unexpected" ]]; then + batslib_print_kv_single 5 \ + 'index' "$idx" \ + 'line' "${lines[$idx]}" \ + | batslib_decorate 'line should differ' \ + | fail + fi + fi + else + # Line contained in output. + if (( is_mode_regexp )); then + local -i idx + for (( idx = 0; idx < ${#lines[@]}; ++idx )); do + if [[ ${lines[$idx]} =~ $unexpected ]]; then + { local -ar single=( 'regexp' "$unexpected" 'index' "$idx" ) + local -a may_be_multi=( 'output' "$output" ) + local -ir width="$( batslib_get_max_single_line_key_width "${single[@]}" "${may_be_multi[@]}" )" + batslib_print_kv_single "$width" "${single[@]}" + if batslib_is_single_line "${may_be_multi[1]}"; then + batslib_print_kv_single "$width" "${may_be_multi[@]}" + else + may_be_multi[1]="$( printf '%s' "${may_be_multi[1]}" | batslib_prefix | batslib_mark '>' "$idx" )" + batslib_print_kv_multi "${may_be_multi[@]}" + fi + } \ + | batslib_decorate 'no line should match the regular expression' \ + | fail + return $? + fi + done + elif (( is_mode_partial )); then + local -i idx + for (( idx = 0; idx < ${#lines[@]}; ++idx )); do + if [[ ${lines[$idx]} == *"$unexpected"* ]]; then + { local -ar single=( 'substring' "$unexpected" 'index' "$idx" ) + local -a may_be_multi=( 'output' "$output" ) + local -ir width="$( batslib_get_max_single_line_key_width "${single[@]}" "${may_be_multi[@]}" )" + batslib_print_kv_single "$width" "${single[@]}" + if batslib_is_single_line "${may_be_multi[1]}"; then + batslib_print_kv_single "$width" "${may_be_multi[@]}" + else + may_be_multi[1]="$( printf '%s' "${may_be_multi[1]}" | batslib_prefix | batslib_mark '>' "$idx" )" + batslib_print_kv_multi "${may_be_multi[@]}" + fi + } \ + | batslib_decorate 'no line should contain substring' \ + | fail + return $? + fi + done + else + local -i idx + for (( idx = 0; idx < ${#lines[@]}; ++idx )); do + if [[ ${lines[$idx]} == "$unexpected" ]]; then + { local -ar single=( 'line' "$unexpected" 'index' "$idx" ) + local -a may_be_multi=( 'output' "$output" ) + local -ir width="$( batslib_get_max_single_line_key_width "${single[@]}" "${may_be_multi[@]}" )" + batslib_print_kv_single "$width" "${single[@]}" + if batslib_is_single_line "${may_be_multi[1]}"; then + batslib_print_kv_single "$width" "${may_be_multi[@]}" + else + may_be_multi[1]="$( printf '%s' "${may_be_multi[1]}" | batslib_prefix | batslib_mark '>' "$idx" )" + batslib_print_kv_multi "${may_be_multi[@]}" + fi + } \ + | batslib_decorate 'line should not be in output' \ + | fail + return $? + fi + done + fi + fi +} + +refute_output() { + local -i is_mode_partial=0 + local -i is_mode_regexp=0 + local -i is_mode_empty=0 + local -i use_stdin=0 + : "${output?}" + + # Handle options. + if (( $# == 0 )); then + is_mode_empty=1 + fi + + while (( $# > 0 )); do + case "$1" in + -p|--partial) is_mode_partial=1; shift ;; + -e|--regexp) is_mode_regexp=1; shift ;; + -|--stdin) use_stdin=1; shift ;; + --) shift; break ;; + *) break ;; + esac + done + + if (( is_mode_partial )) && (( is_mode_regexp )); then + echo "\`--partial' and \`--regexp' are mutually exclusive" \ + | batslib_decorate 'ERROR: refute_output' \ + | fail + return $? + fi + + # Arguments. + local unexpected + if (( use_stdin )); then + unexpected="$(cat -)" + else + unexpected="${1-}" + fi + + if (( is_mode_regexp == 1 )) && [[ '' =~ $unexpected ]] || (( $? == 2 )); then + echo "Invalid extended regular expression: \`$unexpected'" \ + | batslib_decorate 'ERROR: refute_output' \ + | fail + return $? + fi + + # Matching. + if (( is_mode_empty )); then + if [ -n "$output" ]; then + batslib_print_kv_single_or_multi 6 \ + 'output' "$output" \ + | batslib_decorate 'output non-empty, but expected no output' \ + | fail + fi + elif (( is_mode_regexp )); then + if [[ $output =~ $unexpected ]]; then + batslib_print_kv_single_or_multi 6 \ + 'regexp' "$unexpected" \ + 'output' "$output" \ + | batslib_decorate 'regular expression should not match output' \ + | fail + fi + elif (( is_mode_partial )); then + if [[ $output == *"$unexpected"* ]]; then + batslib_print_kv_single_or_multi 9 \ + 'substring' "$unexpected" \ + 'output' "$output" \ + | batslib_decorate 'output should not contain substring' \ + | fail + fi + else + if [[ $output == "$unexpected" ]]; then + batslib_print_kv_single_or_multi 6 \ + 'output' "$output" \ + | batslib_decorate 'output equals, but it was expected to differ' \ + | fail + fi + fi +} diff --git a/exercises/concept/vehicle-purchase/vehicle_purchase.bats b/exercises/concept/vehicle-purchase/vehicle_purchase.bats new file mode 100644 index 00000000..ef3c41f3 --- /dev/null +++ b/exercises/concept/vehicle-purchase/vehicle_purchase.bats @@ -0,0 +1,154 @@ +#!/usr/bin/env bats +load bats-extra + +# bats test_tags=task:1 +test_car_needs_a_license() { # @test + run bash vehicle_purchase.sh car 'Toyota Corolla' 'Wuling Hongguang' 1000 1 + + assert_success + assert_line --index 0 'License needed for car: yes' +} + +# bats test_tags=task:1 +test_truck_needs_a_license() { # @test + run bash vehicle_purchase.sh truck 'Toyota Corolla' 'Wuling Hongguang' 1000 1 + + assert_success + assert_line --index 0 'License needed for truck: yes' +} + +# bats test_tags=task:1 +test_bike_does_not_need_a_license() { # @test + run bash vehicle_purchase.sh bike 'Toyota Corolla' 'Wuling Hongguang' 1000 1 + + assert_success + assert_line --index 0 'License needed for bike: no' +} + +# bats test_tags=task:1 +test_stroller_does_not_need_a_license() { # @test + run bash vehicle_purchase.sh stroller 'Toyota Corolla' 'Wuling Hongguang' 1000 1 + + assert_success + assert_line --index 0 'License needed for stroller: no' +} + +# bats test_tags=task:1 +test_e_scooter_does_not_need_a_license() { # @test + run bash vehicle_purchase.sh e-scooter 'Toyota Corolla' 'Wuling Hongguang' 1000 1 + + assert_success + assert_line --index 0 'License needed for e-scooter: no' +} + +# bats test_tags=task:1 +test_cart_does_not_need_a_license() { # @test + run bash vehicle_purchase.sh cart 'Toyota Corolla' 'Wuling Hongguang' 1000 1 + + assert_success + assert_line --index 0 'License needed for cart: no' +} + +# bats test_tags=task:1 +test_empty_kind_does_not_need_a_license() { # @test + run bash vehicle_purchase.sh '' 'Toyota Corolla' 'Wuling Hongguang' 1000 1 + + assert_success + assert_line --index 0 'License needed for : no' +} + +# bats test_tags=task:2 +test_recommends_the_first_option() { # @test + run bash vehicle_purchase.sh car 'Bugatti Veyron' 'Ford Pinto' 1000 1 + + assert_success + assert_line --index 1 'Bugatti Veyron is clearly the better choice.' +} + +# bats test_tags=task:2 +test_recommends_the_second_option() { # @test + run bash vehicle_purchase.sh car 'Ford Pinto' 'Bugatti Veyron' 1000 1 + + assert_success + assert_line --index 1 'Bugatti Veyron is clearly the better choice.' +} + +# bats test_tags=task:2 +test_recommends_the_shorter_name_first() { # @test + run bash vehicle_purchase.sh car 'Volkswagen Golf' 'Volkswagen' 1000 1 + + assert_success + assert_line --index 1 'Volkswagen is clearly the better choice.' +} + +# bats test_tags=task:2 +test_recommends_by_digits_before_letters() { # @test + run bash vehicle_purchase.sh bike 'Bergamont City' '2020 Gazelle Medeo' 1000 1 + + assert_success + assert_line --index 1 '2020 Gazelle Medeo is clearly the better choice.' +} + +# bats test_tags=task:2 +test_choice_does_not_expand_glob_characters() { # @test + run bash vehicle_purchase.sh car '*' 'Ford Pinto' 1000 1 + + assert_success + assert_line --index 1 '* is clearly the better choice.' +} + +# bats test_tags=task:3 +test_price_for_a_new_vehicle() { # @test + run bash vehicle_purchase.sh car 'Ford Pinto' 'Bugatti Veyron' 1000 1 + + assert_success + assert_line --index 2 'Estimated price: 800' +} + +# bats test_tags=task:3 +test_price_for_a_vehicle_just_under_three_years() { # @test + run bash vehicle_purchase.sh car 'Ford Pinto' 'Bugatti Veyron' 25000 2 + + assert_success + assert_line --index 2 'Estimated price: 20000' +} + +# bats test_tags=task:3 +test_price_for_a_three_year_old_vehicle() { # @test + run bash vehicle_purchase.sh car 'Ford Pinto' 'Bugatti Veyron' 1000 3 + + assert_success + assert_line --index 2 'Estimated price: 700' +} + +# bats test_tags=task:3 +test_price_for_a_ten_year_old_vehicle() { # @test + run bash vehicle_purchase.sh car 'Ford Pinto' 'Bugatti Veyron' 25000 10 + + assert_success + assert_line --index 2 'Estimated price: 17500' +} + +# bats test_tags=task:3 +test_price_for_an_eleven_year_old_vehicle() { # @test + run bash vehicle_purchase.sh car 'Ford Pinto' 'Bugatti Veyron' 1000 11 + + assert_success + assert_line --index 2 'Estimated price: 500' +} + +# bats test_tags=task:3 +test_price_for_an_old_vehicle_other_price() { # @test + run bash vehicle_purchase.sh car 'Ford Pinto' 'Bugatti Veyron' 25000 30 + + assert_success + assert_line --index 2 'Estimated price: 12500' +} + +# bats test_tags=task:3 +test_prints_exactly_three_lines() { # @test + run bash vehicle_purchase.sh car 'Ford Pinto' 'Bugatti Veyron' 1000 5 + + assert_success + assert_equal "${#lines[@]}" 3 +} diff --git a/exercises/concept/vehicle-purchase/vehicle_purchase.sh b/exercises/concept/vehicle-purchase/vehicle_purchase.sh new file mode 100644 index 00000000..d5170002 --- /dev/null +++ b/exercises/concept/vehicle-purchase/vehicle_purchase.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +# This script helps you prepare to buy a vehicle. +# +# Arguments: +# $1 the kind of vehicle, such as "car" or "bike". +# $2 the first vehicle you are considering. +# $3 the second vehicle you are considering. +# $4 the original price of the vehicle, as a whole number. +# $5 the age of the vehicle in years, as a whole number. +# +# Add your code below each task comment. +# Each task builds on the previous tasks, so keep the earlier output unchanged. +# Quote your variable expansions when you use `[` or `test`. + +# Task 1: print whether a license is needed for this kind of vehicle. + +# Task 2: print which of the two vehicles to choose. + +# Task 3: print the estimated price of the used vehicle. From 6cf39307f1996d603df9180c15894154d79b44b7 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Mon, 30 Nov 2026 17:04:34 -0500 Subject: [PATCH 2/2] Address review comments on the vehicle-purchase pull request - List the common arithmetic operators in the arithmetic concept. - Format command names in the conditionals headings as code, and note there that `[[` is preferred over `[` and `test`. - Write "bash" in lowercase within sentences and headings. - Show the command with its output in the task examples. Co-Authored-By: Isaac Good Co-Authored-By: Claude Sonnet 5.5 --- concepts/arithmetic/about.md | 21 +++++++++++++++---- concepts/arithmetic/introduction.md | 21 +++++++++++++++---- concepts/conditionals/about.md | 21 ++++++++++++------- concepts/conditionals/introduction.md | 21 ++++++++++++------- .../concept/vehicle-purchase/.docs/hints.md | 2 +- .../vehicle-purchase/.docs/instructions.md | 6 ++++++ .../vehicle-purchase/.docs/introduction.md | 21 ++++++++++++------- .../concept/vehicle-purchase/.meta/design.md | 2 +- 8 files changed, 81 insertions(+), 34 deletions(-) diff --git a/concepts/arithmetic/about.md b/concepts/arithmetic/about.md index 7dab52fb..a6b9918f 100644 --- a/concepts/arithmetic/about.md +++ b/concepts/arithmetic/about.md @@ -1,11 +1,24 @@ -# About Arithmetic in the Bash Shell +# About Arithmetic in the bash Shell Bash can do arithmetic but _only with integers_. If you need to do floating-point math, you'll need to call out to some external program (like [awk][awk] or [bc][bc]). The arithmetic operators are listed (in order of precedence) in the manual: [Shell Arithmetic][arithmetic]. -Additionally, Bash only performs arithmetic in certain places (known as arithmetic "contexts"). +These are the common ones, from highest to lowest precedence: + +| Operators | Meaning | +| --- | --- | +| `**` | exponentiation | +| `*` `/` `%` | multiplication, division, remainder | +| `+` `-` | addition, subtraction | +| `<=` `>=` `<` `>` | comparison | +| `==` `!=` | equality, inequality | +| `&&` | logical AND | +| `\|\|` | logical OR | +| `=` `+=` `-=` `*=` `/=` `%=` | assignment | + +Additionally, bash only performs arithmetic in certain places (known as arithmetic "contexts"). ## Arithmetic Expansion @@ -28,7 +41,7 @@ echo "new value for x is $x" ~~~~exercism/caution Be careful using arithmetic this way combined with `set -e`. -If the expression evaluates to zero, the exit status will be non-zero and Bash will terminate. +If the expression evaluates to zero, the exit status will be non-zero and bash will terminate. ```bash x=0 @@ -125,7 +138,7 @@ s="hello world" len=$(( ${#s} * 2 )) ``` -And positional paremeters have to as well. +Positional parameters also need the `$`. If the first two positional parameters represent width and height: ```bash diff --git a/concepts/arithmetic/introduction.md b/concepts/arithmetic/introduction.md index 9bead108..29161906 100644 --- a/concepts/arithmetic/introduction.md +++ b/concepts/arithmetic/introduction.md @@ -1,11 +1,24 @@ -# Introduction to Arithmetic in the Bash Shell +# Introduction to Arithmetic in the bash Shell Bash can do arithmetic but _only with integers_. If you need to do floating-point math, you'll need to call out to some external program (like [awk][awk] or [bc][bc]). The arithmetic operators are listed (in order of precedence) in the manual: [Shell Arithmetic][arithmetic]. -Additionally, Bash only performs arithmetic in certain places (known as arithmetic "contexts"). +These are the common ones, from highest to lowest precedence: + +| Operators | Meaning | +| --- | --- | +| `**` | exponentiation | +| `*` `/` `%` | multiplication, division, remainder | +| `+` `-` | addition, subtraction | +| `<=` `>=` `<` `>` | comparison | +| `==` `!=` | equality, inequality | +| `&&` | logical AND | +| `\|\|` | logical OR | +| `=` `+=` `-=` `*=` `/=` `%=` | assignment | + +Additionally, bash only performs arithmetic in certain places (known as arithmetic "contexts"). ## Arithmetic Expansion @@ -28,7 +41,7 @@ echo "new value for x is $x" ~~~~exercism/caution Be careful using arithmetic this way combined with `set -e`. -If the expression evaluates to zero, the exit status will be non-zero and Bash will terminate. +If the expression evaluates to zero, the exit status will be non-zero and bash will terminate. ```bash x=0 @@ -125,7 +138,7 @@ s="hello world" len=$(( ${#s} * 2 )) ``` -And positional paremeters have to as well. +Positional parameters also need the `$`. If the first two positional parameters represent width and height: ```bash diff --git a/concepts/conditionals/about.md b/concepts/conditionals/about.md index 8206894d..98b1bdce 100644 --- a/concepts/conditionals/about.md +++ b/concepts/conditionals/about.md @@ -1,8 +1,8 @@ # About Conditionals -## Truthiness in Bash +## Truthiness in bash -Bash does not have the concept of boolean values. +Bash does not have the concept of Boolean values. There are strings and numbers, and arrays of strings or numbers. So how do conditional commands deal with true and false? @@ -15,7 +15,7 @@ Any other exit status represents _failure_. This applies to all commands, including bash builtin commands, keywords, and functions. -## The "if" Command +## The `if` Command The basic syntax of the `if` command is @@ -65,7 +65,12 @@ if grep -q "my pattern" my_file; then fi ``` -## "[" and "test" Commands +## The `[` and `test` Commands + +~~~~exercism/note +Prefer `[[`, described in the next section, when you write conditional expressions in bash. +You still need to recognize `[` and `test`, because you will see them in other scripts. +~~~~ There is no special syntax around the CONDITIONAL_COMMANDS. You may be used to seeing if statements that look like this: @@ -122,7 +127,7 @@ if [ -n $str ]; then echo "not empty"; else echo "empty"; fi [test]: https://www.gnu.org/software/bash/manual/bash.html#index-test ~~~~ -## "[[" Keyword +## The `[[` Keyword The [`[[...]]` conditional construct][cond-construct] is not a _command_, it is a **keyword**. This means that, although it is handled like any other command, it has special parsing rules. @@ -164,8 +169,8 @@ However it produces an _exit status_. * The exit status is 1 (failure) if the expression's result is **zero**. ~~~~exercism/note -Inside `((` Bash uses the same sense of true/false as the C language: zero is false, non-zero is true. -**This is the opposite of how Bash evaluates exit statuses!** +Inside `((` bash uses the same sense of true/false as the C language: zero is false, non-zero is true. +**This is the opposite of how bash evaluates exit statuses!** ~~~~ This syntax is typically used as the COMMAND part of an `if` statement or a [`while` loop][looping-while]. @@ -175,7 +180,7 @@ if (( x == y - 1 )); then ... while (( y > 0 )); do ... ``` -## The "case" Command +## The `case` Command [`case`][case] is another control flow command. It is like a "switch" statement in other languages. diff --git a/concepts/conditionals/introduction.md b/concepts/conditionals/introduction.md index c31e38ff..4e415de5 100644 --- a/concepts/conditionals/introduction.md +++ b/concepts/conditionals/introduction.md @@ -1,8 +1,8 @@ # Introduction to Conditionals -## Truthiness in Bash +## Truthiness in bash -Bash does not have the concept of boolean values. +Bash does not have the concept of Boolean values. There are strings and numbers, and arrays of strings or numbers. So how do conditional commands deal with true and false? @@ -15,7 +15,7 @@ Any other exit status represents _failure_. This applies to all commands, including bash builtin commands, keywords, and functions. -## The "if" Command +## The `if` Command The basic syntax of the `if` command is @@ -65,7 +65,12 @@ if grep -q "my pattern" my_file; then fi ``` -## "[" and "test" Commands +## The `[` and `test` Commands + +~~~~exercism/note +Prefer `[[`, described in the next section, when you write conditional expressions in bash. +You still need to recognize `[` and `test`, because you will see them in other scripts. +~~~~ There is no special syntax around the CONDITIONAL_COMMANDS. You may be used to seeing if statements that look like this: @@ -122,7 +127,7 @@ if [ -n $str ]; then echo "not empty"; else echo "empty"; fi [test]: https://www.gnu.org/software/bash/manual/bash.html#index-test ~~~~ -## "[[" Keyword +## The `[[` Keyword The [`[[...]]` conditional construct][cond-construct] is not a _command_, it is a **keyword**. This means that, although it is handled like any other command, it has special parsing rules. @@ -164,8 +169,8 @@ However it produces an _exit status_. * The exit status is 1 (failure) if the expression's result is **zero**. ~~~~exercism/note -Inside `((` Bash uses the same sense of true/false as the C language: zero is false, non-zero is true. -**This is the opposite of how Bash evaluates exit statuses!** +Inside `((` bash uses the same sense of true/false as the C language: zero is false, non-zero is true. +**This is the opposite of how bash evaluates exit statuses!** ~~~~ This syntax is typically used as the COMMAND part of an `if` statement or a [`while` loop][looping-while]. @@ -175,7 +180,7 @@ if (( x == y - 1 )); then ... while (( y > 0 )); do ... ``` -## The "case" Command +## The `case` Command [`case`][case] is another control flow command. It is like a "switch" statement in other languages. diff --git a/exercises/concept/vehicle-purchase/.docs/hints.md b/exercises/concept/vehicle-purchase/.docs/hints.md index 23d9fa47..8ba3449f 100644 --- a/exercises/concept/vehicle-purchase/.docs/hints.md +++ b/exercises/concept/vehicle-purchase/.docs/hints.md @@ -4,7 +4,7 @@ - Quote your variable expansions in conditional expressions when you use `[` or `test`. Inside `[[ ... ]]`, variables are not split or glob-expanded, but quoting the right-hand side of `==` makes it match literally. -- Read about [conditional constructs][conditional-constructs] in the Bash manual, or the track's conditionals concept page. +- Read about [conditional constructs][conditional-constructs] in the bash manual, or the track's conditionals concept page. ## 1. Determine if you will need a license diff --git a/exercises/concept/vehicle-purchase/.docs/instructions.md b/exercises/concept/vehicle-purchase/.docs/instructions.md index ff3051d0..80a70050 100644 --- a/exercises/concept/vehicle-purchase/.docs/instructions.md +++ b/exercises/concept/vehicle-purchase/.docs/instructions.md @@ -29,6 +29,7 @@ The kind of vehicle is given as the first argument. Print `License needed for KIND: yes` or `License needed for KIND: no`, with `KIND` replaced by that argument. ```text +$ bash vehicle_purchase.sh car 'Wuling Hongguang' 'Toyota Corolla' 1000 5 License needed for car: yes ``` @@ -42,6 +43,8 @@ Print the decision as a sentence. For the arguments `'Wuling Hongguang' 'Toyota Corolla'`: ```text +$ bash vehicle_purchase.sh car 'Wuling Hongguang' 'Toyota Corolla' 1000 5 +License needed for car: yes Toyota Corolla is clearly the better choice. ``` @@ -66,5 +69,8 @@ Print the estimated price. For an original price of `1000` and an age of `5`: ```text +$ bash vehicle_purchase.sh car 'Wuling Hongguang' 'Toyota Corolla' 1000 5 +License needed for car: yes +Toyota Corolla is clearly the better choice. Estimated price: 700 ``` diff --git a/exercises/concept/vehicle-purchase/.docs/introduction.md b/exercises/concept/vehicle-purchase/.docs/introduction.md index 6fd0bbdf..8af69471 100644 --- a/exercises/concept/vehicle-purchase/.docs/introduction.md +++ b/exercises/concept/vehicle-purchase/.docs/introduction.md @@ -2,9 +2,9 @@ ## Conditionals -### Truthiness in Bash +### Truthiness in bash -Bash does not have the concept of boolean values. +Bash does not have the concept of Boolean values. There are strings and numbers, and arrays of strings or numbers. So how do conditional commands deal with true and false? @@ -17,7 +17,7 @@ Any other exit status represents _failure_. This applies to all commands, including bash builtin commands, keywords, and functions. -### The "if" Command +### The `if` Command The basic syntax of the `if` command is @@ -67,7 +67,12 @@ if grep -q "my pattern" my_file; then fi ``` -### "[" and "test" Commands +### The `[` and `test` Commands + +~~~~exercism/note +Prefer `[[`, described in the next section, when you write conditional expressions in bash. +You still need to recognize `[` and `test`, because you will see them in other scripts. +~~~~ There is no special syntax around the CONDITIONAL_COMMANDS. You may be used to seeing if statements that look like this: @@ -124,7 +129,7 @@ if [ -n $str ]; then echo "not empty"; else echo "empty"; fi [test]: https://www.gnu.org/software/bash/manual/bash.html#index-test ~~~~ -### "[[" Keyword +### The `[[` Keyword The [`[[...]]` conditional construct][cond-construct] is not a _command_, it is a **keyword**. This means that, although it is handled like any other command, it has special parsing rules. @@ -166,8 +171,8 @@ However it produces an _exit status_. * The exit status is 1 (failure) if the expression's result is **zero**. ~~~~exercism/note -Inside `((` Bash uses the same sense of true/false as the C language: zero is false, non-zero is true. -**This is the opposite of how Bash evaluates exit statuses!** +Inside `((` bash uses the same sense of true/false as the C language: zero is false, non-zero is true. +**This is the opposite of how bash evaluates exit statuses!** ~~~~ This syntax is typically used as the COMMAND part of an `if` statement or a [`while` loop][looping-while]. @@ -177,7 +182,7 @@ if (( x == y - 1 )); then ... while (( y > 0 )); do ... ``` -### The "case" Command +### The `case` Command [`case`][case] is another control flow command. It is like a "switch" statement in other languages. diff --git a/exercises/concept/vehicle-purchase/.meta/design.md b/exercises/concept/vehicle-purchase/.meta/design.md index dc7ba903..dd6f3aa5 100644 --- a/exercises/concept/vehicle-purchase/.meta/design.md +++ b/exercises/concept/vehicle-purchase/.meta/design.md @@ -2,7 +2,7 @@ ## Goal -The goal of this exercise is to introduce conditionals in Bash scripts. +The goal of this exercise is to introduce conditionals in bash scripts. ## Learning objectives