Skip to content

Commit 5661456

Browse files
authored
Merge pull request #189 from codecrafters-io/TropicolX-patch-26
Revise "Redirect stdout #jv1"
2 parents 00a293d + c36f331 commit 5661456

File tree

1 file changed

+53
-9
lines changed

1 file changed

+53
-9
lines changed
Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,77 @@
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.
22

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
54

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).
745

846
### Tests
947

1048
The tester will execute your program like this:
11-
1249
```bash
13-
./your_program.sh
50+
$ ./your_program.sh
1451
```
1552

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

1855
```bash
1956
$ ls /tmp/baz > /tmp/foo/baz.md
2057
$ cat /tmp/foo/baz.md
2158
apple
2259
blueberry
60+
2361
$ echo 'Hello James' 1> /tmp/foo/foo.md
2462
$ cat /tmp/foo/foo.md
2563
Hello James
64+
2665
$ cat /tmp/baz/blueberry nonexistent 1> /tmp/foo/quz.md
2766
cat: nonexistent: No such file or directory
2867
$ cat /tmp/foo/quz.md
2968
blueberry
3069
```
3170

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

Comments
 (0)