0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-31 09:14:20 -04:00
denoland-deno/docs/examples/unix_cat.md

25 lines
712 B
Markdown
Raw Normal View History

2020-05-06 18:21:13 -04:00
## An implementation of the unix "cat" program
In this program each command-line argument is assumed to be a filename, the file
is opened, and printed to stdout.
```ts
for (let i = 0; i < Deno.args.length; i++) {
2020-06-08 18:52:23 -04:00
const filename = Deno.args[i];
const file = await Deno.open(filename);
2020-05-06 18:21:13 -04:00
await Deno.copy(file, Deno.stdout);
file.close();
}
```
The `copy()` function here actually makes no more than the necessary kernel ->
userspace -> kernel copies. That is, the same memory from which data is read
from the file, is written to stdout. This illustrates a general design goal for
I/O streams in Deno.
Try the program:
```shell
deno run --allow-read https://deno.land/std/examples/cat.ts /etc/passwd
2020-05-06 18:21:13 -04:00
```