简体   繁体   English

在节点js中使用rm和扩展的globing

[英]Using rm with extended globbing in node js

I need to do daily cleanup of a folder for my app, so I ve made a bash script (with some help of superuser) to do so. 我需要每天为我的应用程序清理文件夹,因此我已经制作了一个bash脚本(在超级用户的帮助下)。

It work well from the console, but it is not interpreted correctly from node. 它可以从控制台正常运行,但是不能从节点正确解释。

Here the working script: 这是工作脚本:

rm ./app/download/!("test1"|"test4");

Which I ve thought would work like this in node.js: 我认为在node.js中可以这样工作:

 var spawn = require('child_process').spawn,
     PATH = process.argv[1].substr(0, process.argv[1].lastIndexOf('/') + 1), //Get the full path to the directory of the app
     arg = [PATH + 'download/!(\"', 'test1', '\"|\"', 'test4', ('\")'],
     child = spawn ('rm', arg);

 child.stderr.on('data', function(data) {
      console.log('stderr:' + data);
 });

But I get rm interpret them by being different files: 但是我让rm通过不同的文件来解释它们:

 stderr:rm: cannot remove '/home/user/app/download("' : No such file or directory
 rm cannot remove 'test1' : No such file or directory
 ...

So I ve tried by changing arg to this: 因此,我尝试通过将arg更改为此:

 arg = [PATH + 'download/!("' + 'test1' + '"|"' + 'test4' + '")']

But I get 但是我明白了

 stderr:rm: cannot remove '/home/user/app/download/!("test1"|"test2")' : No such file or directory

I m trying with exec, but I don t think this is the problem since it seems the spawned rm don t interpret the extended glob thing, which is active by default... 我正在尝试使用exec,但我不认为这是问题所在,因为似乎rm生成的rm不能解释扩展的glob事物,默认情况下它是活动的...

EDIT: As a workaround, I m trying to do a sh script which would start this command with as many parameter I start it with for the file to keep. 编辑:作为一种解决方法,我试图做一个sh脚本,它将使用与启动该命令一样多的参数来启动此命令,以保留该文件。 It s not as I wanted it, but for now I just need something which would work. 这不是我想要的,但是现在我只需要一些可以使用的东西。

Try to change your lines to: 尝试将行更改为:

 arg = ['-c', "shopt -s extglob\nshopt -s nullglob\nrm " + PATH + 'download/!("test1"|"test4")'],
 child = spawn ('bash', arg);

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

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