|
1 | | -In this stage, you'll implement support for redirecting the standard error of a command to a file. |
| 1 | +In this stage, you'll implement support for redirecting a command's standard error to a file. |
2 | 2 |
|
3 | | -The `2>` operator is used to redirect the standard error of a command to a file. |
| 3 | +### The `2>` Operator |
4 | 4 |
|
5 | | -Learn more about [Redirecting Stderr](https://www.gnu.org/software/bash/manual/bash.html#Redirecting-Output). |
| 5 | +The `2>` operator redirects the standard error of a command to a file. The standard error is where programs write error messages and diagnostics. |
6 | 6 |
|
7 | | -### Tests |
| 7 | +In Unix, standard error is identified by the number `2`. When you write `2>`, you're explicitly redirecting file descriptor 2 (stderr) to a file. |
8 | 8 |
|
9 | | -The tester will execute your program like this: |
| 9 | +For example: |
| 10 | + |
| 11 | +```bash |
| 12 | +$ cat nonexistent 2> errors.txt |
| 13 | +$ cat errors.txt |
| 14 | +cat: nonexistent: No such file or directory |
| 15 | +``` |
| 16 | + |
| 17 | +Here, the error message is written to `errors.txt` rather than appearing in the terminal. |
| 18 | + |
| 19 | +### Standard Output Is Not Redirected |
| 20 | + |
| 21 | +The `2>` operator only redirects standard error, not standard output. Normal output still appears on the terminal: |
10 | 22 |
|
11 | 23 | ```bash |
12 | | -./your_program.sh |
| 24 | +$ cat existing_file nonexistent 2> errors.txt |
| 25 | +contents of existing file |
| 26 | +$ cat errors.txt |
| 27 | +cat: nonexistent: No such file or directory |
| 28 | + |
| 29 | +$ echo hello 2> errors.txt |
| 30 | +hello |
| 31 | +$ cat errors.txt |
| 32 | +$ |
13 | 33 | ``` |
14 | 34 |
|
15 | | -It'll then send a series of commands to your shell, executing commands and redirecting their output to a file: |
| 35 | +In the example above, the file contents appear in the terminal, while any error message goes to `errors.txt`. |
| 36 | + |
| 37 | +### Tests |
| 38 | + |
| 39 | +The tester will execute your program like this: |
| 40 | +```bash |
| 41 | +$ ./your_program.sh |
| 42 | +``` |
16 | 43 |
|
| 44 | +It will then send a series of commands to your shell and attempt to redirect its standard error to a file: |
17 | 45 | ```bash |
18 | 46 | $ ls nonexistent 2> /tmp/quz/baz.md |
19 | 47 | $ cat /tmp/quz/baz.md |
20 | 48 | ls: nonexistent: No such file or directory |
| 49 | + |
21 | 50 | $ echo 'Maria file cannot be found' 2> /tmp/quz/foo.md |
22 | 51 | Maria file cannot be found |
| 52 | +$ |
| 53 | + |
23 | 54 | $ cat /tmp/bar/pear nonexistent 2> /tmp/quz/quz.md |
24 | 55 | pear |
25 | 56 | $ cat /tmp/quz/quz.md |
26 | 57 | cat: nonexistent: No such file or directory |
27 | 58 | ``` |
28 | 59 |
|
29 | | -The tester will check if the commands correctly execute commands and redirect their error message to a file as specified. |
30 | | - |
31 | | -It will also check that the file is created (if it doesn’t already exist), and that its contents match the expected output. |
| 60 | +The tester will verify that: |
| 61 | +- Commands with `2>` redirect their standard error to the specified file. |
| 62 | +- The specified files are created if they don't exist. |
| 63 | +- Standard output still appears on the terminal (not redirected). |
| 64 | +- Standard errors are not displayed on the terminal. |
0 commit comments