|
1 | | -In this stage, you'll implement support for redirecting the output of a command to a file. |
| 1 | +In this stage, you'll implement support for redirecting a command's output to a file. |
2 | 2 |
|
3 | | -The `1>` operator is used to redirect the output of a command to a file. |
4 | | -But, as a special case, if the file descriptor is not specified before the operator `>`, the output is redirected to the standard output by default, so `>` is equivalent to `1>`. |
| 3 | +### The `>` Operator |
5 | 4 |
|
6 | | -Learn more about [Redirecting Output](https://www.gnu.org/software/bash/manual/bash.html#Redirecting-Output). |
| 5 | +The `>` operator redirects the standard output of a command to a file. When you use `>`, the output that would normally appear on the terminal is instead written to the specified file. |
| 6 | + |
| 7 | +If the file doesn't exist, it is created. If the file already exists, it is overwritten (replacing its old contents). |
| 8 | + |
| 9 | +For example: |
| 10 | + |
| 11 | +```bash |
| 12 | +$ echo hello > output.txt |
| 13 | +$ cat output.txt |
| 14 | +hello |
| 15 | +``` |
| 16 | + |
| 17 | +### File Descriptor Notation |
| 18 | + |
| 19 | +In Unix, standard output is identified by the number `1`. You can explicitly write `1>` to redirect standard output: |
| 20 | + |
| 21 | +```bash |
| 22 | +$ echo hello 1> file.txt |
| 23 | +``` |
| 24 | + |
| 25 | +However, since redirecting standard output is so common, the shell allows you to omit the `1` and just write `>`: |
| 26 | + |
| 27 | +```bash |
| 28 | +$ echo hello > file.txt |
| 29 | +``` |
| 30 | + |
| 31 | +Both commands do exactly the same thing. |
| 32 | + |
| 33 | +### Error Output Is Not Redirected |
| 34 | + |
| 35 | +The `>` operator only redirects standard output, not standard error. If a command produces error messages, they still appear on the terminal: |
| 36 | + |
| 37 | +```bash |
| 38 | +$ cat nonexistent > output.txt |
| 39 | +cat: nonexistent: No such file or directory |
| 40 | +$ cat output.txt |
| 41 | +$ |
| 42 | +``` |
| 43 | + |
| 44 | +The error message appears on the terminal, and `output.txt` is empty (or contains only the non-error output if there was any). |
7 | 45 |
|
8 | 46 | ### Tests |
9 | 47 |
|
10 | 48 | The tester will execute your program like this: |
11 | | - |
12 | 49 | ```bash |
13 | | -./your_program.sh |
| 50 | +$ ./your_program.sh |
14 | 51 | ``` |
15 | 52 |
|
16 | | -It'll then send a series of commands to your shell, executing commands and redirecting their output to a file: |
| 53 | +It will then send a series of commands to your shell and redirect their output to a file: |
17 | 54 |
|
18 | 55 | ```bash |
19 | 56 | $ ls /tmp/baz > /tmp/foo/baz.md |
20 | 57 | $ cat /tmp/foo/baz.md |
21 | 58 | apple |
22 | 59 | blueberry |
| 60 | + |
23 | 61 | $ echo 'Hello James' 1> /tmp/foo/foo.md |
24 | 62 | $ cat /tmp/foo/foo.md |
25 | 63 | Hello James |
| 64 | + |
26 | 65 | $ cat /tmp/baz/blueberry nonexistent 1> /tmp/foo/quz.md |
27 | 66 | cat: nonexistent: No such file or directory |
28 | 67 | $ cat /tmp/foo/quz.md |
29 | 68 | blueberry |
30 | 69 | ``` |
31 | 70 |
|
32 | | -The tester will check if the commands correctly execute commands and redirect their output to a file as specified. |
33 | | -The file contents will also be checked for correctness. |
| 71 | +The tester will verify that: |
| 72 | +- Commands with `>` redirect their standard output to the specified file. |
| 73 | +- Commands with `1>` work identically to `>`. |
| 74 | +- Files are created if they don't exist. |
| 75 | +- Error messages still appear on the terminal (not redirected). |
| 76 | +- Only standard output is written to the file. |
| 77 | + |
0 commit comments