JavaScript writes shell script with Node.js [translation]_javascript tips

Source: Internet
Author: User
Tags eol stdin win32
Access parameters
You can visit the command-line argument via PROCESS.ARGV, which is an array containing the following:

[Nodebinary, Script, arg0, arg1, ...]

That is, the first argument starts with Process.argv[2], and you can iterate through all the arguments as follows:
Copy Code code as follows:

Process.argv.slice (2). ForEach (function (fileName) {
...
});

If you want to do more complex processing of parameters, you can look at the Node.js module NomNom and optimist. Below, we will use the file system module several times:
Copy Code code as follows:

var fs = require (' FS ');

Read a text file
If your file is not very large, you can read the entire file into memory and place it in a string:
Copy Code code as follows:

var text = Fs.readfilesync (fileName, "UTF8");

Then, you can split the text, and the line of work.
Copy Code code as follows:

Text.split (/\r?\n/). ForEach (function (line) {
// ...
});

For large files, you can use a stream to iterate through all the rows. Mtomis a solution on the stack overflow.

Write a text file
You can write the entire content to a file through a string.

Fs.writefilesync (FileName, str, ' UTF8 ');
Or you can write the string in an incremental way to the stream.
Copy Code code as follows:

var out = Fs.createwritestream (FileName, {encoding: "UTF8"});
Out.write (str);
Out.end (); Currently, the same as Destroy () and Destroysoon ()

Cross-platform considerations

Determines the line terminator.
Resolution 1: Read an existing file into the string, search for "\ r \ n", and determine if the line terminator is "\ n" If it is not found.
Copy Code code as follows:

var EOL = Filecontents.indexof ("\ r \ n") >= 0? "\ r \ n": "\ n";

Solution 2: Test system platform. All Windows platforms return to "Win32", and 64-bit systems are also.
Copy Code code as follows:

var EOL = (Process.platform = = ' Win32 '? ' \ r \ n ': ' \ n ')

Handling paths

You can use the path module when working with file system paths. This ensures that the correct path separator is used (on UNIX, with "/" and "\" on Windows).
Copy Code code as follows:

var path = require (' path ');
Path.join (Mydir, "foo");

Run the script
If your shell's name is myscript.js, then you can run it like this:

Node Myscript.js arg1 arg2 ...

On UNIX, you can add a code to the first line of the script that tells the operating system what program to use to interpret the script:

#!/usr/bin/env node

You must also give the script executable permissions:

chmod u+x Myscript.js
Now the script can run independently:

./myscript.js arg1 Arg2 ...

Other Topics

    • output to standard output (stdout): Console.log is the same as in the browser. The console is a global object , not a module, and it does not need to be imported using require () .
    • read standard input (stdin): Process.stdin is a readable stream. A process is a global object.
    • To run the shell command: through 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.