admin管理员组文章数量:1023738
The following code works on Windows but not on Linux (Amazon Linux 2023):
const regex = "((fa[lrsdb]\\s+|(fa-(?:solid|regular|light|thin|duotone|brands|sharp|sharp-duotone)\\s+)+)(fa-fw\\s+)*)(fa-[0-9]x\\s+)*fa(-\\w+)+"
const grep = spawn(
'grep',
['-whorP', `'${regex}'`, './{classes,inc,public,tpl}'],
{ env: {...process.env, LC_ALL: 'en_US.utf8'} }
)
On Linux I get the error:
grep: /home/tdev/rentecbo/{classes,inc,public,tpl}: No such file or directory
If I add shell: true
to the third argument the code works on Linux but fails on Windows with the error:
\s+)+)(fa-fw\s+)*)(fa-[0-9]x\s+)*fa(-\w+)+' was unexpected at this time.
Manually running the command via the CLI works in both environments:
grep -whorP '((fa[lrsdb]\s+|(fa-(?:solid|regular|light|thin|duotone|brands|sharp|sharp-duotone)\s+)+)(fa-fw\s+)*)(fa-[0-9]x\s+)*fa(-\w+)+' ./{classes,inc,public,tpl}
I got the script to work by setting shell: os.platform !== 'win32'
but it's a bit hacky.
The following code works on Windows but not on Linux (Amazon Linux 2023):
const regex = "((fa[lrsdb]\\s+|(fa-(?:solid|regular|light|thin|duotone|brands|sharp|sharp-duotone)\\s+)+)(fa-fw\\s+)*)(fa-[0-9]x\\s+)*fa(-\\w+)+"
const grep = spawn(
'grep',
['-whorP', `'${regex}'`, './{classes,inc,public,tpl}'],
{ env: {...process.env, LC_ALL: 'en_US.utf8'} }
)
On Linux I get the error:
grep: /home/tdev/rentecbo/{classes,inc,public,tpl}: No such file or directory
If I add shell: true
to the third argument the code works on Linux but fails on Windows with the error:
\s+)+)(fa-fw\s+)*)(fa-[0-9]x\s+)*fa(-\w+)+' was unexpected at this time.
Manually running the command via the CLI works in both environments:
grep -whorP '((fa[lrsdb]\s+|(fa-(?:solid|regular|light|thin|duotone|brands|sharp|sharp-duotone)\s+)+)(fa-fw\s+)*)(fa-[0-9]x\s+)*fa(-\w+)+' ./{classes,inc,public,tpl}
I got the script to work by setting shell: os.platform !== 'win32'
but it's a bit hacky.
1 Answer
Reset to default 1./{classes,inc,public,tpl}
is a bash syntax, which is why it doesn't work on linux without shell=true. If this is the real code, it would be easiest to just expand it manually ('./classes', './inc', './public', './tpl'
)
On Windows shell=true means cmd.exe, not bash by default, which breaks down because your regexp is not escaped enough for cmd.exe
If you insist on using bash on Windows, spawn it explicitly, i.e. spawn('bash', ['-c', 'your grep command as you'd type it'] ...
The following code works on Windows but not on Linux (Amazon Linux 2023):
const regex = "((fa[lrsdb]\\s+|(fa-(?:solid|regular|light|thin|duotone|brands|sharp|sharp-duotone)\\s+)+)(fa-fw\\s+)*)(fa-[0-9]x\\s+)*fa(-\\w+)+"
const grep = spawn(
'grep',
['-whorP', `'${regex}'`, './{classes,inc,public,tpl}'],
{ env: {...process.env, LC_ALL: 'en_US.utf8'} }
)
On Linux I get the error:
grep: /home/tdev/rentecbo/{classes,inc,public,tpl}: No such file or directory
If I add shell: true
to the third argument the code works on Linux but fails on Windows with the error:
\s+)+)(fa-fw\s+)*)(fa-[0-9]x\s+)*fa(-\w+)+' was unexpected at this time.
Manually running the command via the CLI works in both environments:
grep -whorP '((fa[lrsdb]\s+|(fa-(?:solid|regular|light|thin|duotone|brands|sharp|sharp-duotone)\s+)+)(fa-fw\s+)*)(fa-[0-9]x\s+)*fa(-\w+)+' ./{classes,inc,public,tpl}
I got the script to work by setting shell: os.platform !== 'win32'
but it's a bit hacky.
The following code works on Windows but not on Linux (Amazon Linux 2023):
const regex = "((fa[lrsdb]\\s+|(fa-(?:solid|regular|light|thin|duotone|brands|sharp|sharp-duotone)\\s+)+)(fa-fw\\s+)*)(fa-[0-9]x\\s+)*fa(-\\w+)+"
const grep = spawn(
'grep',
['-whorP', `'${regex}'`, './{classes,inc,public,tpl}'],
{ env: {...process.env, LC_ALL: 'en_US.utf8'} }
)
On Linux I get the error:
grep: /home/tdev/rentecbo/{classes,inc,public,tpl}: No such file or directory
If I add shell: true
to the third argument the code works on Linux but fails on Windows with the error:
\s+)+)(fa-fw\s+)*)(fa-[0-9]x\s+)*fa(-\w+)+' was unexpected at this time.
Manually running the command via the CLI works in both environments:
grep -whorP '((fa[lrsdb]\s+|(fa-(?:solid|regular|light|thin|duotone|brands|sharp|sharp-duotone)\s+)+)(fa-fw\s+)*)(fa-[0-9]x\s+)*fa(-\w+)+' ./{classes,inc,public,tpl}
I got the script to work by setting shell: os.platform !== 'win32'
but it's a bit hacky.
-
Manually running the command via the CLI works in both environments:
- This is not true. You are manually running it in CLI on Linux but run it on custom 3rd party software on Windows. If you were to run it on Window's real CLI it would fail. – slebetman Commented Nov 19, 2024 at 22:47 - @slebetman git bash for windows is a CLI – tvanc Commented Nov 20, 2024 at 19:45
- Git bash for windows is a 3rd party program that windows does not consider to be a CLI. Your bash commands you type into git bash is considered to be CLI by only git bash but not Windows. In fact. Git bash is itself a GUI program. Not a CLI program. This is needed because the COMSPEC shell (cmd.exe) cannot itself handle ANSI terminal escape sequences so the git bash developers had to create a separate GUI program to run bash in. – slebetman Commented Nov 21, 2024 at 2:08
- @slebetman ah interesting. Thank you for explaining that. I appreciate the work put into the Terminal window in JetBrains IDEs that smooths over those distinctions. – tvanc Commented Nov 22, 2024 at 23:42
1 Answer
Reset to default 1./{classes,inc,public,tpl}
is a bash syntax, which is why it doesn't work on linux without shell=true. If this is the real code, it would be easiest to just expand it manually ('./classes', './inc', './public', './tpl'
)
On Windows shell=true means cmd.exe, not bash by default, which breaks down because your regexp is not escaped enough for cmd.exe
If you insist on using bash on Windows, spawn it explicitly, i.e. spawn('bash', ['-c', 'your grep command as you'd type it'] ...
本文标签:
版权声明:本文标题:node.js - grep via child_process.spawn works on Git Bash for Windows, or real bash on Linux - but not both - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745598313a2158305.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Manually running the command via the CLI works in both environments:
- This is not true. You are manually running it in CLI on Linux but run it on custom 3rd party software on Windows. If you were to run it on Window's real CLI it would fail. – slebetman Commented Nov 19, 2024 at 22:47