Skip to content

Commit 03ea3ab

Browse files
authored
Merge pull request #190 from codecrafters-io/TropicolX-patch-27
Revise "Redirect stderr #vz4"
2 parents 5661456 + 306cec2 commit 03ea3ab

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed
Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,64 @@
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.
22

3-
The `2>` operator is used to redirect the standard error of a command to a file.
3+
### The `2>` Operator
44

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.
66

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.
88

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:
1022

1123
```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+
$
1333
```
1434

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+
```
1643

44+
It will then send a series of commands to your shell and attempt to redirect its standard error to a file:
1745
```bash
1846
$ ls nonexistent 2> /tmp/quz/baz.md
1947
$ cat /tmp/quz/baz.md
2048
ls: nonexistent: No such file or directory
49+
2150
$ echo 'Maria file cannot be found' 2> /tmp/quz/foo.md
2251
Maria file cannot be found
52+
$
53+
2354
$ cat /tmp/bar/pear nonexistent 2> /tmp/quz/quz.md
2455
pear
2556
$ cat /tmp/quz/quz.md
2657
cat: nonexistent: No such file or directory
2758
```
2859

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

Comments
 (0)