Do you understand JavaScript? Do you need to write a Shell script? You should try Node. js. It is easy to install and is suitable for learning its access parameters by writing Shell scripts.
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:
The 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:
The 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:
The Code is as follows:
Var text = fs. readFileSync (fileName, "utf8 ");
Then, you can split the text and process it in one row.
The 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.
The 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 ".
The 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.
The 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 "\").
The 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
- Tip: load source from a file in the Node. js shell
- Execute code each time the Node. js REPL starts