An implementation of the unix “cat” program

Concepts

  • Use the Deno runtime API to output the contents of a file to the console.
  • Deno.args accesses the command line arguments.
  • Deno.open is used to get a handle to a file.
  • copy is used to transfer data from the file to the output stream.
  • Files should be closed when you are finished with them
  • Modules can be run directly from remote URLs.

Example

In this program each command-line argument is assumed to be a filename, the file is opened, and printed to stdout (e.g. the console).

  1. /**
  2. * cat.ts
  3. */
  4. import { copy } from "https://deno.land/std@$STD_VERSION/io/util.ts";
  5. for (const filename of Deno.args) {
  6. const file = await Deno.open(filename);
  7. await copy(file, Deno.stdout);
  8. file.close();
  9. }

To run the program:

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