Skip to content
This repository has been archived by the owner on Sep 21, 2023. It is now read-only.

Latest commit

 

History

History
34 lines (27 loc) · 1.09 KB

unix_cat.md

File metadata and controls

34 lines (27 loc) · 1.09 KB

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.
  • Deno.stdout.writable is used to get a writable stream to the console standard output.
  • Deno.FsFile.readable is used to get a readable stream from the file. (This readable stream closes the file when it is finished reading, so it is not necessary to close the file explicitly.)
  • 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).

/**
 * cat.ts
 */
for (const filename of Deno.args) {
  const file = await Deno.open(filename);
  await file.readable.pipeTo(Deno.stdout.writable, { preventClose: true });
}

To run the program:

deno run --allow-read https://deno.land/[email protected]/examples/cat.ts /etc/passwd