SoFunction
Updated on 2025-03-10

Detailed explanation of using a simple command line tool

This article introduces the use of writing a simple command line tool and share it with you. The details are as follows:

The operating system needs to be Linux

1. Goal

  1. Enter the command you wrote on the command line to complete the target task
  2. The command line requires global validity
  3. The command line requires that it can be deleted
  4. The command line functions to generate a file to display the current date

2. Code section

  1. Create a new file and name it sherryFile
  2. Contents of file sherryFile

Introduction: Generate a file with the current date and creator

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

command start
writeFile success!!!!
command end

In this file directory, a new file will be generated, with the following content

2/28/2018
    create By karuru

Modify the command to be valid globally

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 to everyone's study and I hope everyone will support me more.