When Shell encounters Node. js

Source: Internet
Author: User

Okay, I admit that this title is a bit ambiguous, but I hope the following content can help engineers who are unfamiliar with or do not like Shell or Windows platforms for batch processing.

BKJIA recommended topics: Node. js Zone

First of all, I admit that the Shell script configuration sed, awk, and so on are already quite powerful. If you are familiar with them, just smile at the following content, but if you like NODE, let's continue our journey:

Well, first make a simple Shell to get the time difference code for the cold dish diff. sh:

 
 
  1. #! /Bin/bashSTART = $ (date + % s)
  2. # Prepare for dish washing and other operations
  3. Du-m/home>/tmp/output
  4. # Finished
  5. END = $ (date + % s)
  6. DIFF = $ ($ END-$ START ))
  7. Echo "converted to $ DIFF seconds"
  8.  
  9. Chmod + x diff. sh
  10. Sh diff. sh

The execution result is as follows:

It takes 0 seconds to complete.

~ O, can you eat such a block? Is it a BUG? Well, the speed is too fast, but the script uses a range of seconds. It can only be performed in nanoseconds, and perform Division operations in Shell. Let's try vim diff. js again using NODE:

 
 
  1. #!/usr/bin/env node  
  2. var util  = require('util'),  
  3.     spawn = require('child_process').spawn,  
  4.     ls    = spawn('du', ['-m','/home/']);  
  5. var start = +new Date();  
  6. ls.stdout.on('data', function (data) {  
  7. //console.log('stdout: ' + data);  
  8. });  
  9.  
  10. ls.stderr.on('data', function (data) {  
  11.   console.log('stderr: ' + data);  
  12. });  
  13.  
  14. ls.on('exit', function (code) {  
  15.   var end = + new Date();  
  16.   console.log(end-start);  
  17. }); 

Run./diff. js:

1113

The time is displayed in milliseconds. Does it feel more comfortable? Well, I admit that this comparison does not mean it is necessary to make a decision, it's just a tool that front-end captors like to cook. It depends on the powerful functions of Shell. Of course, you can also use Python for captors who like different languages, ruby and other languages.

Okay. If you think it's interesting, move on with me. Otherwise, just make a brick, haha.

The preceding example shows that it is convenient and flexible to use Shell scripts and the built-in functions of NodeJs to make some common dishes. The powerful function of Shell scripts is stream redirection processing, is it necessary to use a script or directly use an ultra-complex command to handle it? Well, it may be difficult for our non-system administrator, let's take out the powerful dish knife procstreams and see how it makes dishes:

 
 
  1. #!/usr/bin/env node  
  2. var p = require('procstreams');  
  3. p('cat app.log').pipe('wc -l')  
  4.   .data(function(stdout, stderr) {  
  5.       console.log(stdout);  
  6.   }); 

The above JS Code implements app statistics. the number of log lines, which is equivalent to the cat app of Shell. log | wc-l function. How does it feel? Will it be more flexible to implement complex or interactive functions. Next dish

 
 
  1. #!/usr/bin/env node  
  2. var p = require('procstreams');  
  3.     p('mkdir foo')  
  4.        .and('rm file.txt')  
  5.           .on('exit', function() {    
  6.                 console.log('done');    
  7.            }); 

Is it familiar? What should I do if I still need to handle complicated parameter problems when executing the script? It doesn't matter. What about node-optimist and isaacs's nopt jobs that have long been waiting for them? The last dish:

 
 
  1. #!/usr/bin/env node  
  2. var util  = require('util'),  
  3.   spawn = require('child_process').spawn;  
  4. var argv = require('optimist').argv;  
  5. var cmd =  argv.cmd;  
  6. var args = argv.args  
  7. var option = argv.opt  
  8. console.log(cmd + '  ' + args + '  ' + option);  
  9. var ls    = spawn(cmd , [args ,option]);  
  10. ls.stdout.on('data', function (data) {  
  11.   if (!data || !!data)  console.log(' i believe it');  
  12. });  
  13.  
  14. ls.stderr.on('data', function (data) {  
  15.   console.log('It\'s a miracle!');  
  16. });  
  17.  
  18. ls.on('exit', function (code) {  
  19.    console.log('it.justHappened();');  
  20. }); 

~ Dear, thank you. At the end of the article, I was crazy when I wrote some Shell scripts at work because of a space or format, will it be more calm to use it with NODEJS in the future? Please express your own opinions. Thank you.

Original article: http://club.cnodejs.org/topic/4f2a2f80aa8e490b1106909b

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.