nodeJS exec不适用于“cd” shell cmd [英] nodeJS exec does not work for "cd " shell cmd

查看:896
本文介绍了nodeJS exec不适用于“cd” shell cmd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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不适用于cdshell cmd。如下所示,pwd工作,git状态正在尝试工作,但是失败,因为它不在git目录中执行,但cd cmd无法停止其他cmds的进一步执行。
尝试在nodeJS shell以及nodeJS + ExpressJS webserver中。


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.

推荐答案

每个命令在单独的shell中执行,所以第一个 cd 仅影响那个然后终止的shell进程。如果要在特定目录中运行 git ,只需要为您设置路径:

Each command is executed in a separate shell, so the first cd only affects that shell process which then terminates. If you want to run git in a particular directory, just have Node set the path for you:

exec('git status', {cwd: '/home/ubuntu/distro'}, /* ... */);

cwd (当前工作目录) 许多可用于 exec 的选项。

cwd (current working directory) is one of many options available for exec.

这篇关于nodeJS exec不适用于“cd” shell cmd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆