Using rm with extended globbing in node js

自闭症网瘾萝莉.ら 提交于 2019-12-11 07:44:42

问题


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.

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:

 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:

 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 = [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...

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. It s not as I wanted it, but for now I just need something which would work.


回答1:


Try to change your lines to:

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


来源:https://stackoverflow.com/questions/18890904/using-rm-with-extended-globbing-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!