简体   繁体   English

Node.js 运行命令行(child_process?)

[英]Node.js run command line (child_process?)

I have a Node.js program where I need to, on a button click, run 2 commands in the Windows command line.我有一个 Node.js 程序,我需要在其中单击按钮,在 Windows 命令行中运行 2 个命令。 For example, the process I'm trying to automate by the button click would be doable manually by going to cmd and entering the following commands:例如,我尝试通过单击按钮自动执行的过程可以通过转到 cmd 并输入以下命令手动完成:

pushd \\myserver.com\folder1\folder2         //Connect to remote server folder structure
mkdir NewFolder                              //Create new folder in the remote folder

I've found many resources pointing that I should use 'child_process', but I'm absolutely lost when it comes to shell scripting and am having a really hard time figuring out how to do this.我发现很多资源都指出我应该使用“child_process”,但是当涉及到 shell 脚本时,我完全迷路了,而且我真的很难弄清楚如何做到这一点。 Here's the code I have so far:这是我到目前为止的代码:

var cp = require('child_process');
cp.exec('pushd \\\\myserver.com\\folder1\\folder2\\', { shell: '/bin/bash' }, function(err, stdout, stderr){
    if(err){
        console.log(err);        
    }
});

But this above code just returns this error (which oddly removes the '\'s from the given dir):但是上面的代码只返回这个错误(奇怪的是从给定的目录中删除了'\'):

{ Error: Command failed: pushd \\myserver.com\folder1\folder2\
/bin/bash: line 1: pushd: \myserver.comfolder1folder2: No such file or directory
    at ChildProcess.exithandler (child_process.js:281:12)

  killed: false,
  code: 1,
  signal: null,
  cmd: 'pushd \\\\myserver.com\\folder1\\folder2\\' }
/bin/bash: line 1: pushd: \myserver.comfolder1folder2: No such file or directory

I'm really lost here and would appreciate any help.我真的在这里迷路了,非常感谢任何帮助。 Any alternative you have to child_process may also be very helpful.您对 child_process 的任何选择也可能非常有帮助。 Thank you!谢谢!

Usually all the escape sequences used for string uses '\\' for a single backslash.通常用于字符串的所有转义序列都使用 '\\' 作为单个反斜杠。 It is understandable you used it here for the directory path for windows.您在这里使用它作为 windows 的目录路径是可以理解的。

In JS particularly '\\' doesn't exactly work like that在 JS 中,特别是 '\\' 并不完全像那样工作

'abc\
def' == 'abcdef' // true

'\a' == 'a' // true

When a '\' is not followed by a character with any special meaning, it is considered to be a LineContinuation instead.当 '\' 后面没有任何特殊含义的字符时,它被认为是 LineContinuation。

As you can see from your error output using '\\\\myserver.com' considered '\myserver.com'.正如您从错误 output 中看到的,使用 '\\\\myserver.com' 被视为 '\myserver.com'。 Plain workaround is to use '\\\\' for single '\' or use '/' for path separation which I'm not pretty sure if shell will execute it.简单的解决方法是使用 '\\\\' 作为单个 '\' 或使用 '/' 进行路径分隔,我不太确定 shell 是否会执行它。 This is one of the blogs explains about it in details Link .这是详细解释它的博客之一链接

The shell is incorrect here:这里的 shell 不正确:

{ shell: '/bin/bash' }

It should be:它应该是:

{ shell: 'CMD.EXE' }

Because in the beginning of your post, you tell that you run 2 commands in the Windows command line , which indicates you are not using Bash, which is (usually) not installed on Windows.因为在帖子的开头,您告诉您在 Windows 命令行中运行 2 个命令,这表明您没有使用 Bash,它(通常)未安装在 Windows 上。

(I know my answer comes almost two years later than the question, but I wrote it in case anyone still reads this.) (我知道我的答案比问题晚了将近两年,但我还是写了它以防还有人读到它。)

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

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