Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在nodejs代码中执行多个终端命令_Node.js_Shell - Fatal编程技术网

Node.js 在nodejs代码中执行多个终端命令

Node.js 在nodejs代码中执行多个终端命令,node.js,shell,Node.js,Shell,我试图通过节点脚本执行多个终端命令 var command = ' cd ~/Desktop' command +=' find -type f -printf %T+\\t%p\\n | sort -n' exec(command, function (error, stdout, stderr) { }); 我有一个类似smaple.sh的shell脚本,它工作得很好 cd ~/Desktop find -type f -printf '%T+\t%

我试图通过节点脚本执行多个终端命令

        var command = ' cd ~/Desktop'
        command +=' find -type f -printf %T+\\t%p\\n | sort -n' 

 exec(command, function (error, stdout, stderr) {

});
我有一个类似smaple.sh的shell脚本,它工作得很好

cd ~/Desktop
find -type f -printf '%T+\t%p\n' | sort -n
我试图在节点脚本中执行上述终端命令

        var command = ' cd ~/Desktop'
        command +=' find -type f -printf %T+\\t%p\\n | sort -n' 

 exec(command, function (error, stdout, stderr) {

});
执行上述代码时,我什么也得不到。 首先我需要更改目录,然后执行第二个命令

find -type f -printf %T+\\t%p\\n | sort -n

使用当前代码,node.js正在尝试执行以下操作:

cd~/Desktop find-type f-printf%T+\\T%p\\n | sort-n

如果您尝试在节点外部运行,您将得到相同的结果

您需要使用
&&
清除命令,如下所示:

var command = ' cd ~/Desktop &&'
command +=' find -type f -printf %T+\\t%p\\n | sort -n' 

exec(command, function (error, stdout, stderr) {

});
也许更优雅的方法是:

var commands = [];
commands.push('cd ~/Desktop');
commands.push('find -type f -printf %T+\\t%p\\n | sort -n');

var command = commands.join(' && ');

exec(command, function (error, stdout, stderr) {

});

我得到的是
exec错误:错误:命令失败:execvp():没有这样的文件或目录
,但我可能有这样的目录在终端上执行你的代码时,它工作正常。但是使用exec我得到了上述错误