Detailed Description: use Node. js to write a simple command line tool, node. js command line

Source: Internet
Author: User

Detailed Description: use Node. js to write a simple command line tool, node. js command line

This article describes how to use Node. js to write a simple command line tool:

The operating system must be Linux

1. Objectives

  1. Enter a self-written command on the command line to complete the target task.
  2. The command line must be globally valid
  3. Command Line requirements can be deleted
  4. Command line function, generate a file, display the current date

2. Code Section

  1. Create a new file named sherryFile
  2. Content of file sherryFile

Introduction: generate a file whose content is the current date and creator

#! /usr/bin/env nodeconsole.log('command start');const fs = require('fs');let date = new Date().toLocaleDateString();let data = date + '\n\t' + '——create By karuru';fs.writeFile('./date.txt', data, 'utf8', (err) => {  if (err) {    console.log('sherryFile command wrong', err);    return false;  }  console.log('writeFile success!!!!');  console.log('command end');});
  1. Grant the execution permission to the file chmod 755 sherryFile
  2. Enter./sherryFile in the file path where the file is located.
  3. If the following content is output, the command is successfully executed.

Command start
WriteFile success !!!!
Command end

Under this directory, a new date.txt file will be generated. The content is as follows:

2/28/2018
Create By karuru

Change the command to globally valid

ln sherryFile /usr/local/bin/sherryFile

DELETE command

rm /usr/local/bin/sherryFile

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.