JavaScript uses Node. js to write Shell scripts.

Source: Internet
Author: User
Tags eol

Access Parameters
You can use process. argv to access the command line parameters. It is an array containing the following content:

[NodeBinary, script, arg0, arg1,...]

That is to say, the first parameter starts from process. argv [2]. You can traverse all parameters as follows:Copy codeThe Code is as follows: process. argv. slice (2). forEach (function (fileName ){
...
});

If you want to perform more complex processing on parameters, you can take a look at the Node. js module nomnom and optimist. Below, we will use the File System Module Multiple times:Copy codeThe Code is as follows: var fs = require ('fs ');

Read a text file
If your file is not large, you can read the entire file into the memory and put it in a string:Copy codeThe Code is as follows: var text = fs. readFileSync (fileName, "utf8 ");

Then, you can split the text and process it in one row.Copy codeThe Code is as follows: text. split (/\ r? \ N/). forEach (function (line ){
//...
});

For large files, you can use a stream to traverse all rows. mtomis provides a solution on Stack Overflow.

Write a text file
You can write the complete content into a file using a string.

Fs. writeFileSync (fileName, str, 'utf8 ');
Alternatively, you can incrementally write strings to the stream.Copy codeThe Code is as follows: var out = fs. createWriteStream (fileName, {encoding: "utf8 "});
Out. write (str );
Out. end (); // It is the same as destroy () and destroySoon ().

Cross-platform considerations

Determines the line terminator.
Solution 1: read an existing file to the string and search for "\ r \ n". If not found, the line terminator is "\ n ".Copy codeThe Code is as follows: var EOL = fileContents. indexOf ("\ r \ n")> = 0? "\ R \ n": "\ n ";

Solution 2: Detection System Platform. "win32" is returned for all Windows platforms, and for 64-bit systems.Copy codeThe Code is as follows: var EOL = (process. platform = 'win32 '? '\ R \ N':' \ n ')

Process paths

You can use the path module when processing the file system PATH. This ensures that the correct path separator is used ("/" on Unix and "\" on Windows "\").Copy codeThe Code is as follows: var path = require ('path ');
Path. join (mydir, "foo ");

Run scripts
If your shell script is named myscript. js, you can run it like this:

Node myscript. js arg1 arg2...

On Unix, you can add a code in the first line of the script to tell the operating system what program to use to explain the script:

#! /Usr/bin/env node

You must also grant the executable permission to the script:

Chmod u + x myscript. js
Now the script can run independently:

./Myscript. js arg1 arg2...

Other topics

  • Output to standard output (stdout ): Console. logSame as in a browser.Console is a global objectIt is not a module and does not need to be used.Require () Import.
  • Read standard input (stdin ): Process. stdinIs a readable stream.ProcessIs a global object.
  • Run the shell command:Child_process.exec ().

Related Articles

  1. Tip: load source from a file in the Node. js shell
  2. Execute code each time the Node. js REPL starts
Related Article

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.