Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

terminal exercises

Views: 2497
1
2
At any time when a program is running it has the option of reading from an input buffer called stdin.
3
How does anything come to be in this buffer? It could be because the user types it.
4
That happens in the example stdin_example.py given here.
5
To stop typing in input, use ctrl-D.
6
7
Try running stdin_example.py right now. Also look at the code.
8
9
Another way information can come to be in the stdin buffer is if it is redirected there. Here is an example of that:
10
11
$ ./stdin_example.py < what_are_stdout_stdin.txt
12
13
You should see this very file dumped to the screen.
14
15
Still a third way for information to be in stdin is to "pipe" it there using the Unix pipe operator |.
16
17
Here is an example:
18
19
$ echo "yellow cups make purple papers" | ./stdin_example.py
20
21
By combining these methods you can write powerful scripts that use only simple executables as parts.
22
23
24
What is stdout? This is the buffer to which output is written by programs by default. Usually it is printed to the screen.
25
26
But it too can be redirected. For example you can redirect it to a file:
27
28
$ echo "mice...eating...eating" > mouse_file
29
30
Now check the contents of mouse_file.
31
32
This applies to scripts you created as well. And you can even use input redirection and output redirection at the same time:
33
34
$ ./stdin_example.py < what_are_stdout_stdin.txt > another_copy.txt
35
36
Now check the contents of another_copy.txt. What just happened?
37
38
39
You can also "pipe" your stdout so that it becomes the input to another program.
40
41
42
43