|
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 |
| 3 | +### The `>` Operator |
4 | 4 |
|
5 | | -The `1>` operator is used to redirect the output of a command to a file. |
6 | | -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>`. |
| 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. |
7 | 6 |
|
8 | | -Learn more about [Redirecting Output](https://www.gnu.org/software/bash/manual/bash.html#Redirecting-Output). |
| 7 | +If the file doesn't exist, it is created. If the file already exists, it is overwritten (replacing its old contents). |
9 | 8 |
|
10 | | -### Tests |
| 9 | +For example: |
| 10 | + |
| 11 | +```bash |
| 12 | +$ echo hello > output.txt |
| 13 | +$ cat output.txt |
| 14 | +hello |
| 15 | +``` |
11 | 16 |
|
12 | | -The tester will execute your program like this: |
| 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: |
13 | 20 |
|
14 | 21 | ```bash |
15 | | -./your_program.sh |
| 22 | +$ echo hello 1> file.txt |
16 | 23 | ``` |
17 | 24 |
|
18 | | -It will then send a series of commands to your shell, executing commands, and redirecting their output to a file: |
| 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. The `>` is shorthand for `1>`. |
| 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). |
| 45 | + |
| 46 | +### Tests |
| 47 | + |
| 48 | +The tester will execute your program: |
| 49 | +```bash |
| 50 | +$ ./your_program.sh |
| 51 | +``` |
| 52 | + |
| 53 | +It will then send a series of commands to your shell and redirect their output to a file: |
19 | 54 |
|
20 | 55 | ```bash |
21 | 56 | $ ls /tmp/baz > /tmp/foo/baz.md |
22 | 57 | $ cat /tmp/foo/baz.md |
23 | 58 | apple |
24 | 59 | blueberry |
| 60 | + |
25 | 61 | $ echo 'Hello James' 1> /tmp/foo/foo.md |
26 | 62 | $ cat /tmp/foo/foo.md |
27 | 63 | Hello James |
| 64 | + |
28 | 65 | $ cat /tmp/baz/blueberry nonexistent 1> /tmp/foo/quz.md |
29 | 66 | cat: nonexistent: No such file or directory |
30 | 67 | $ cat /tmp/foo/quz.md |
31 | 68 | blueberry |
32 | 69 | ``` |
33 | 70 |
|
34 | | -The tester will check if the commands are executed correctly and redirect their output to a file as specified. 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