cd命令 nodejs,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 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.

解决方案

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 (current working directory) is one of many options available for exec.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值