Skip to content

Commit

Permalink
review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
glennj committed Dec 2, 2024
1 parent 9064028 commit c1f3512
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
3 changes: 2 additions & 1 deletion concepts/pipelines/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"glennj"
],
"contributors": [
"IsaacG"
"IsaacG",
"kotp"
],
"blurb": "Compose more complex bash commands with pipelines and command lists"
}
27 changes: 13 additions & 14 deletions concepts/pipelines/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
We have seen how to write simple commands, where a command is followed by arguments.
Now we will see how to make more complex commands by composing simple commands.

## I/O
## Input and Output

Before we start, a quick intro to input/output.
Before we start, a quick introduction to input/output.

Processes have "standard I/O channels".

Expand All @@ -14,9 +14,9 @@ Processes have "standard I/O channels".
* A process can emit _error output_ on "stderr".

The `tr` command is a very pure example of this.
All it does is read text from its stdin, perform character transliterations, and print the resulting text to stdout.
All it does is read text from its input, perform character transliterations, and print the resulting text to its output.

We will see more about manipulating stdio channels later in the syllabus.
We will see more about manipulating "stdio" channels later in the syllabus.

## Pipelines

Expand All @@ -36,8 +36,6 @@ The pipe symbol (`|`) connects the output of one command to the input of another
* By default, each command in a pipeline runs in a separate subshell.
(A subshell is a child process that is a copy of the currently running shell.)
* All the commands in a subshell execute in parallel.
* There is a performance cost to running pipelines.
If you find yourself with long pipelines of similar commands, consider combining them in a single command.
For example, pipelines using multiple instances of `grep`, `cut`, `sed`, `awk`, and `tr` can generally be combined into a single `awk` command for efficiency.
Expand Down Expand Up @@ -91,11 +89,12 @@ They differ in when C is executed.

### Uses of Command Lists

Here are a couple of examples where command lists can simplify bash code.
Here are a couple of examples where command lists can simplify the code.

#### Reading blocks of lines from a file

Suppose you have a data file that represents points of a triangle as the length of the three sides but each on a separate line.
Suppose you have a data file containing data about triangles,
and a triangle is represented by three separate lines holding the sides of the triangle.

```bash
$ cat triangle.dat
Expand Down Expand Up @@ -131,19 +130,19 @@ Assuming `is_pythagorean` is a command that determines if the three sides satisf
Many programming languages have a form of assertion where an exception is thrown if some condition fails

```
assert(x == 5, "x must be 5");
assert(user == "Administrator", "wrong user");
```

We can use an OR operator in bash to simulate that function:
We can use an OR operator in Bash to simulate that function:

```bash
die () {
die () { # a little convenience function
echo "$*" >&2
exit 1
}

[[ $x -eq 5 ]] || die "x must be equal to 5"
[[ $y -gt 5 ]] || die "y must be greater than 5"
[[ $user == "Administrator" ]] || die "wrong user"
[[ $password == "secret!" ]] || die "incorrect password"
```

## Style Considerations
Expand Down Expand Up @@ -176,7 +175,7 @@ Using a _line continuation_ means you can put the operator first, which makes it

~~~~exercism/note
A _line continuation_ is the two character sequence "backslash" and "newline" (`\` + `\n`).
When bash sees that sequence, it is simply removed from the code, thereby _continuing_ the current line with the next line.
When Bash sees that sequence, it is simply removed from the code, thereby _continuing_ the current line with the next line.
Take care to not allow any spaces between the backslash and the newline.
~~~~

Expand Down
27 changes: 13 additions & 14 deletions concepts/pipelines/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
We have seen how to write simple commands, where a command is followed by arguments.
Now we will see how to make more complex commands by composing simple commands.

## I/O
## Input and Output

Before we start, a quick intro to input/output.
Before we start, a quick introduction to input/output.

Processes have "standard I/O channels".

Expand All @@ -14,9 +14,9 @@ Processes have "standard I/O channels".
* A process can emit _error output_ on "stderr".

The `tr` command is a very pure example of this.
All it does is read text from its stdin, perform character transliterations, and print the resulting text to stdout.
All it does is read text from its input, perform character transliterations, and print the resulting text to its output.

We will see more about manipulating stdio channels later in the syllabus.
We will see more about manipulating "stdio" channels later in the syllabus.

## Pipelines

Expand All @@ -36,8 +36,6 @@ The pipe symbol (`|`) connects the output of one command to the input of another
* By default, each command in a pipeline runs in a separate subshell.
(A subshell is a child process that is a copy of the currently running shell.)
* All the commands in a subshell execute in parallel.
* There is a performance cost to running pipelines.
If you find yourself with long pipelines of similar commands, consider combining them in a single command.
For example, pipelines using multiple instances of `grep`, `cut`, `sed`, `awk`, and `tr` can generally be combined into a single `awk` command for efficiency.
Expand Down Expand Up @@ -91,11 +89,12 @@ They differ in when C is executed.

### Uses of Command Lists

Here are a couple of examples where command lists can simplify bash code.
Here are a couple of examples where command lists can simplify the code.

#### Reading blocks of lines from a file

Suppose you have a data file that represents points of a triangle as the length of the three sides but each on a separate line.
Suppose you have a data file containing data about triangles,
and a triangle is represented by three separate lines holding the sides of the triangle.

```bash
$ cat triangle.dat
Expand Down Expand Up @@ -131,19 +130,19 @@ Assuming `is_pythagorean` is a command that determines if the three sides satisf
Many programming languages have a form of assertion where an exception is thrown if some condition fails

```
assert(x == 5, "x must be 5");
assert(user == "Administrator", "wrong user");
```

We can use an OR operator in bash to simulate that function:
We can use an OR operator in Bash to simulate that function:

```bash
die () {
die () { # a little convenience function
echo "$*" >&2
exit 1
}

[[ $x -eq 5 ]] || die "x must be equal to 5"
[[ $y -gt 5 ]] || die "y must be greater than 5"
[[ $user == "Administrator" ]] || die "wrong user"
[[ $password == "secret!" ]] || die "incorrect password"
```

## Style Considerations
Expand Down Expand Up @@ -176,7 +175,7 @@ Using a _line continuation_ means you can put the operator first, which makes it

~~~~exercism/note
A _line continuation_ is the two character sequence "backslash" and "newline" (`\` + `\n`).
When bash sees that sequence, it is simply removed from the code, thereby _continuing_ the current line with the next line.
When Bash sees that sequence, it is simply removed from the code, thereby _continuing_ the current line with the next line.
Take care to not allow any spaces between the backslash and the newline.
~~~~

Expand Down

0 comments on commit c1f3512

Please sign in to comment.