我需要每天为我的应用程序清理一个文件夹,所以我做了一个bash脚本(在超级用户的帮助下)这样做。
它在控制台上运行良好,但无法从节点正确解释。
这是工作脚本:
rm ./app/download/!("test1"|"test4");
我认为在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);
});
但是我会通过不同的文件来解释它们:
stderr:rm: cannot remove '/home/user/app/download("' : No such file or directory
rm cannot remove 'test1' : No such file or directory
...
所以我尝试将arg改为:
arg = [PATH + 'download/!("' + 'test1' + '"|"' + 'test4' + '")']
但是我得到了
stderr:rm: cannot remove '/home/user/app/download/!("test1"|"test2")' : No such file or directory
我正在尝试使用exec,但我不认为这是问题,因为它似乎产生的rm不能解释扩展的glob事件,默认情况下是活动的......
编辑:作为一种解决方法,我试图做一个sh脚本,它将启动这个命令与尽可能多的参数我启动它保持文件。这不是我想要的,但是现在我只需要一些可行的东西。答案 0 :(得分:1)
尝试将您的行更改为:
arg = ['-c', "shopt -s extglob\nshopt -s nullglob\nrm " + PATH + 'download/!("test1"|"test4")'],
child = spawn ('bash', arg);