Question

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.

Was it helpful?

Solution

Try to change your lines to:

 arg = ['-c', "shopt -s extglob\nshopt -s nullglob\nrm " + PATH + 'download/!("test1"|"test4")'],
 child = spawn ('bash', arg);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top