nodeJS exec 不适用于“cd”shell cmd
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15629923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nodeJS exec does not work for "cd " shell cmd
提问by user1447121
var sys = require('sys'),
exec = require('child_process').exec;
exec("cd /home/ubuntu/distro", function(err, stdout, stderr) {
console.log("cd: " + err + " : " + stdout);
exec("pwd", function(err, stdout, stderr) {
console.log("pwd: " + err + " : " + stdout);
exec("git status", function(err, stdout, stderr) {
console.log("git status returned " ); console.log(err);
})
})
})
cd: null :
pwd: null : /
git status returned
{ [Error: Command failed: fatal: Not a git repository (or any of the parent directories): .git ] killed: false, code: 128, signal: null }
nodeJS exec does not work for "cd " shell cmd. as you see below, pwd works, git status is trying to work but fails because it is not executed in a git directory, but cd cmd fails stopping further successful execution of other cmds. Tried in nodeJS shell as well as nodeJS+ExpressJS webserver.
nodeJS exec 不适用于“cd”shell cmd。正如你在下面看到的,pwd 工作,git status 正在尝试工作但失败,因为它没有在 git 目录中执行,但 cd cmd 无法阻止其他 cmd 的进一步成功执行。在 nodeJS shell 以及 nodeJS+ExpressJS webserver 中尝试过。
回答by icktoofay
Each command is executed in a separate shell, so the first cdonly affects that shell process which then terminates. If you want to run gitin a particular directory, just have Node set the path for you:
每个命令都在一个单独的 shell 中执行,所以第一个命令cd只会影响那个 shell 进程,然后终止。如果您想git在特定目录中运行,只需让 Node 为您设置路径:
exec('git status', {cwd: '/home/ubuntu/distro'}, /* ... */);
cwd(current working directory) is one of many options available for exec.
cwd(当前工作目录)是一个可供多种选择exec。
回答by Frambot
It is working. But then it is throwing the shell away. Node creates a new shell for each exec.
这是工作。但后来它扔掉了外壳。Node 为每个exec.
Here are options that can help: http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
以下是可以提供帮助的选项:http: //nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
回答by Cameron
Rather than call exec() multiple times. Call exec() once for multiple commands
而不是多次调用 exec() 。为多个命令调用 exec() 一次
Your shell IS executing cdbut it's just that each shell throws away it's working directory after it's finished. Hence you're back at square one.
您的外壳正在执行,cd但只是每个外壳在完成后都会丢弃它的工作目录。因此,您又回到了第一站。
In your case, you don't need to call exec() more than once. You can make sure your cmdvariable contains multiple instructions instead of 1. CD willwork in this case.
在您的情况下,您不需要多次调用 exec() 。您可以确保您的cmd变量包含多个指令而不是 1 个。CD将在这种情况下工作。
var cmd = `ls
cd foo
ls`
var exec = require('child_process').exec;
exec(cmd, function(err, stdout, stderr) {
console.log(stdout);
})
Note: This code should work on Linux but not Windows. See here
注意:此代码应适用于Linux,但不适用于 Windows。看这里

