[英]NodeJS Exec cp with brace expansion gives different results. Why?
I have a grunt-shell command which cp images files using brace expansion. 我有一个grunt-shell命令,该命令使用大括号扩展cp图像文件。
file: Gruntfile.js
cpImgTmp: {
command: 'cp ./app/images/{*.png,*.jpg,*.ico} tmp/images'
}
When I run this grunt-shell command on MacOS, it does what is expected but returns 'cannot cp...' error on ubuntu. 当我在MacOS上运行此grunt-shell命令时,它执行了预期的操作,但在ubuntu上返回了“ cannot cp ...”错误。
I've searched through grunt-shell lib and noticed it uses exec function. 我搜索了grunt-shell lib,发现它使用了exec函数。 I tested the command once more in a controlled environment
我在受控环境中再次测试了该命令
exec('cp ./app/images/{*.png,*.jpg,*.ico} tmp/images')
and verified exec function's spawn args are the same: 与已验证的exec函数的生成参数相同:
['/bin/sh', '-c', 'cp ./app/images/{*.png,*.jpg,*.ico} tmp/images']
I successfully executed the command inside ubuntu server terminal. 我在ubuntu服务器终端中成功执行了命令。 So my question is the problem with nodejs handling of brace expansion in different environments and what options can I pass to nodejs to make this command work cross unix OS?
所以我的问题是在不同环境中使用nodejs处理大括号扩展的问题,我可以将哪些选项传递给nodejs以使此命令在Unix操作系统上运行?
I figured this is less of a nodejs problem but a unix shell issue which nodejs could avoid. 我认为这不是一个nodejs问题,而是一个unix shell问题,nodejs可以避免。
The issue is sh
doesn't support brace expansions, bash
does. 问题是
sh
不支持大括号扩展, bash
支持。 On my Mac, sh
is symlink to bash
and ubuntu (18.04) sh
is symlink to dash
(by the results this obviously doesn't support brace expansion). 在我的Mac上,
sh
是bash
符号链接 ,ubuntu(18.04) sh
是dash
符号链接 (根据结果,这显然不支持花括号扩展)。
Solution is to pass {shell: '/bin/bash'
} for brace expansion to work in unix systems instead of the default '/bin/sh' shell. 解决方案是通过
{shell: '/bin/bash'
}进行括号扩展,以在unix系统中工作,而不是默认的'/ bin / sh'shell。
example using NodeJS exec
function: `exec('cp ./app/images/{ .png, .jpg,*.ico} tmp/images', {shell: '/bin/bash'}) 使用NodeJS
exec
函数的示例:exec('cp ./app/images/ { .png, .jpg,*。ico} tmp / images',{shell:'/ bin / bash'})
example using grunt-shell 使用grunt-shell的示例
cpImgTmp: {
command: 'cp ./app/images/{*.png,*.jpg,*.ico} tmp/images',
options: {
execOptions: {shell: '/bin/bash'}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.