Skip to content

Commit 4a91f8b

Browse files
authored
add comments on pipes (#1502)
1 parent 08171cc commit 4a91f8b

File tree

1 file changed

+36
-11
lines changed
  • common-content/en/module/tools/individual-shell-tools

1 file changed

+36
-11
lines changed

common-content/en/module/tools/individual-shell-tools/index.md

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,34 @@ Through this course, we are going to write lots of programs.
1515

1616
But a lot of programs have already been written by other people. It's often quicker and easier to use an existing program than to write a new one.
1717

18-
We are going to learn about some commonly used programs. We will learn some common patterns that make them easier to learn them and to combine them.
18+
Many programs that were written for Unix (the predecessor of Linux) are still used today. Why are these programs still useful today? In part because of the [Unix philosophy](https://en.wikipedia.org/wiki/Unix_philosophy), the way these programs were written: simple, modular and flexible.
1919

20-
We run these programs from a terminal. We can either call the programs directly from a terminal, or put instructions to call the programs in a script and run the script.
20+
- **Simple**: Each tool solves a specific problem, making it easier to understand and learn.
21+
- **Modular**: We can reuse tools in different ways by combining them.
22+
- **Flexible**: Pipelines let us build solutions quickly without coding from scratch.
23+
24+
We are going to learn about some commonly used programs. We will learn common patterns that make them easier to learn and combine.
25+
26+
We run these programs from a terminal. We can either call the programs directly from a terminal, or write the instructions to call the programs in a script and run the script.
27+
28+
### Key Concepts
29+
30+
**Pipelines**: We can connect programs together so the output of one becomes the input of another using the `|` (pipe) symbol.
31+
32+
For example, to find lines containing "error" in all .log files and show how many unique files have them:
33+
34+
```console
35+
% grep -l "error" *.log | wc -l
36+
3
37+
```
38+
39+
Here, `grep -l "error" *.log` finds all .log files containing "error" and outputs just the filenames, then `wc -l` counts the lines (filenames) to give us the total count.
40+
41+
**PATH**: The system searches for programs in directories listed in the PATH environment variable. When you type a command, the shell looks for the program in these directories.
42+
43+
**Important**: The current directory (`.`) is not in PATH by default for security reasons. To run a program in the current directory, you must use `./program-name` instead of just `program-name`.
44+
45+
**Finding programs**: Use `which <program-name>` to see where a program is located.
2146

2247
### Conventions
2348

@@ -31,24 +56,24 @@ file1 file2
3156
Here we are saying: At a terminal prompt (signified by the `%`), run the command `ls`.\
3257
Below the command is the output (`file1 file2`) you should expect to see after you run the command. Sometimes this is the exact output you should expect. Sometimes _example_ outputs are given: these could vary according to your filesystem, username, or other variables.
3358

34-
To call a program, we type its name and press enter. Open a terminal and run:
59+
To call a program, we type its name and press enter. Open a terminal and run:
3560

3661
```console
3762
% ls
3863
file1 file2
3964
```
4065

41-
Most of the programs which take a file as an input take it as an argument to the command.\
42-
We list arguments after the command name, separated with a space, e.g. we can pass `/tmp` as an argument to `ls`:
66+
Most programs that take a file as input accept it as an argument to the command.\
67+
We list arguments after the command name, separated with a space. For example, we can pass `/tmp` as an argument to `ls`:
4368

4469
```console
4570
% ls /tmp
4671
some-temporary-file some-other-temporary file
4772
```
4873

49-
#### Flags and arguments
74+
#### Flags and Arguments
5075

51-
Some programs take extra arguments. These may be optional or required. Extra arguments generally start with a `-` or `--` then their name. If these extra arguments take a value, expect the value to come _after_ the name.
76+
Some programs take extra arguments. These may be optional or required. Extra arguments generally start with a `-` or `--` followed by their name. If these extra arguments take a value, expect the value to come _after_ the name.
5277

5378
```console
5479
% grep -r -B 1 "hello" .
@@ -59,10 +84,10 @@ Some programs take extra arguments. These may be optional or required. Extra arg
5984
./errors/index.md:const result = console.log("hello world");
6085
```
6186

62-
Here we passed the optional argument `-r` and the optional argument `-B`. `-r` takes no value. We gave `-B` the value 1.
87+
Here we passed the optional argument `-r` and the optional argument `-B`. `-r` takes no value. We gave `-B` the value 1.
6388

64-
We call arguments that start with a `-` or `--` flags. We could give flags in any order - `grep -B 1 -r "hello" .` works the same as `grep -r -B 1 "hello" .`.
89+
We call arguments that start with a `-` or `--` **flags**. We could give flags in any order - `grep -B 1 -r "hello" .` works the same as `grep -r -B 1 "hello" .`.
6590

66-
Flags that start with a `-` not a `--` can also be squashed together. `grep -rc "Hello" .` works the same as `grep -r -c "Hello" .`. If you are looking up a flag you don't know, like `-rc`, remember it may actaully be two flags (`-r` and `-c`). You can also specify a value for the _last_ flag in a squashed together list: `grep -rcB 1 "hello" .`.
91+
Flags that start with a `-` (not `--`) can also be combined together. `grep -rc "Hello" .` works the same as `grep -r -c "Hello" .`. If you are looking up a flag you don't know, like `-rc`, remember it may actually be two flags (`-r` and `-c`). You can also specify a value for the _last_ flag in a combined list: `grep -rcB 1 "hello" .`.
6792

68-
`"hello"` and `.` are called positional arguments because they don't have a named flag before them. The program decides how to interpret them based on their position (order) in the command line, rather than a named flag coming before them. Their order matters.
93+
`"hello"` and `.` are called **positional arguments** because they don't have a named flag before them. The program decides how to interpret them based on their position (order) in the command line, rather than a named flag coming before them. Their order matters.

0 commit comments

Comments
 (0)