node.js - grep via child_process.spawn works on Git Bash for Windows, or real bash on Linux - but not both - Stack Overflow

The following code works on Windows but not on Linux (Amazon Linux 2023):const regex = "((fa[lrsd

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.

Share Improve this question edited Nov 19, 2024 at 21:59 tvanc asked Nov 18, 2024 at 19:41 tvanctvanc 4,3544 gold badges26 silver badges45 bronze badges 4
  • 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
Add a comment  | 

1 Answer 1

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'] ...

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745598314a4635250.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信