Skip to content

Latest commit

 

History

History
35 lines (28 loc) · 1.04 KB

unix_cat.md

File metadata and controls

35 lines (28 loc) · 1.04 KB

An implementation of the unix "cat" program {#an-implementation-of-the-unix-cat-program}

Concepts {#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.
  • Deno.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 {#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).

/**
 * cat.ts
 */
for (const filename of Deno.args) {
  const file = await Deno.open(filename);
  await Deno.copy(file, Deno.stdout);
  file.close();
}

To run the program:

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