1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 07:44:48 -05:00
denoland-deno/docs/examples/unix_cat.md
Luca Casonato 45f9b32ef0
Docs for deno test + minor other changes (#5185)
* Added fs events example.
* Added docs for `deno test`.
* Renamed file server example.
* Unified markdown code types.
* Removed plugin topics from TOC.
* Fixed links.
2020-05-10 03:09:42 +02:00

708 B

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.

for (let i = 0; i < Deno.args.length; i++) {
  let filename = Deno.args[i];
  let file = await Deno.open(filename);
  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:

deno run --allow-read https://deno.land/std/examples/cat.ts /etc/passwd