How to execute shell command inside node. js to escape string

Source: Internet
Author: User

Today, when you execute a shell command with node. JS, you encounter a directory and file name with spaces.

According to common sense should escape, of course, you can write escape, but to deal with the complex escape, not so easy. So does the system provide it?

Google found that you can use

Child_process.execfile (file, [args], [options], [callback])

In the args array to help you escape.

On the code:

      var execfile = require (' child_process '). ExecFile;      var cmd = "Avconvert";      var args = ['-P ', ' presetapplem4a ', '-s ', oldname, '-O ', newname];      ExecFile (Cmd,args,        function (Error, stdout, stderr) {          console.log (' stdout: ' + stdout);          Console.log (' stderr: ' + stderr);          if (Error!== null) {            console.log (' EXEC (' +cmd+ ') Error: ' + error);          }        });


In the code snippet above, Oldname and newname are probably file names that contain spaces and punctuation.

By passing it to the ARG array, the system will help you escape.



How to execute shell command inside node. js to escape string

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.