简体   繁体   English

使用Node.js执行简单的Unix终端命令

[英]Executing Simple Unix Terminal Commands using Node.js

I am trying to execute simple terminal commands through Node.js but so far have only gotten 'ls' to work.. What if I want to change directory etc? 我正在尝试通过Node.js执行简单的终端命令,但到目前为止,只有“ ls”可以工作。如果我想更改目录怎么办? Anyone know how to do that? 有人知道该怎么做吗? Here is code that just does the 'ls' command via Node.js. 这是只通过Node.js执行“ ls”命令的代码。

 var exec = require('child_process').exec;
 var cmd = 'ls';
 exec(cmd,function(error,stdout,stderr){

      console.log(stdout);
      console.log(stderr);
      if(error!=null){
            console.log(error);
        }
   });

Well I'm using a command abtrations for doing thing like that.. You can practically use the cd like in your terminal whit this: 好吧,我正在使用命令标记来做类似的事情。.实际上,您可以在终端中使用cd例如:

https://www.npmjs.com/package/shelljs https://www.npmjs.com/package/shelljs

how to use a cd : 如何使用cd

var shell = require('shelljs');
shell.cd('../');

and the good thing It's that is an abstraction to the command so It should work on any OS.. 好处是这是命令的抽象,因此它可以在任何OS上工作。

First : Try to get the input data from your console Second : Try to execute it in the node code Third : Try to get the result and show it in your console 第一:尝试从控制台获取输入数据;第二:尝试在节点代码中执行输入数据;第三:尝试获取结果并将其显示在控制台中

All those steps, you can get the answer from google or docs of nodejs. 所有这些步骤,您都可以从Google或nodejs的文档中获取答案。 If you can't find the answer, try to search it in npm. 如果找不到答案,请尝试在npm中进行搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM