shquote()breaks system()命令而不是逃脱空间

发布于 2025-01-20 08:58:09 字数 848 浏览 1 评论 0原文

我正在运行R脚本中的GCMS软件AMDI,以分析GCMS数据。基本语法是:

system("AMDIS_PATH GCMS_FILE_PATH /S /GC /GE /E"

使用“ /s /gc /ge /e”,指AMDIS程序中的功能。

我想制作代码为“万无一失”,因此我允许使用“/”而不是“ \\”的路径。而且我还允许路径中的空间,用shquote(...,type =“ cmd”)逃脱它们,

这意味着完整的代码看起来

system(paste(shQuote(gsub("/", "\\", AMDIS_PATH, fixed = T), type = 'cmd'), shQuote(gsub("/", "\\", GCMS_FILE_PATH, fixed = T), type = 'cmd'), "/S /GD /GS /E"))

此代码不起作用(output = 65535) ,但是有一个问题:

如果我从gcms_file_path中删除shquotes。然后,一切都应该像它一样工作(只要File_path不包含任何空间)。 因此,执行这样的代码确实有效:

system(paste(shQuote(gsub("/", "\\", AMDIS_PATH, fixed = T), type = 'cmd'), gsub("/", "\\", GCMS_FILE_PATH, fixed = T), "/S /GD /GS /E"))

任何想法Shquote为AMDIS_PATH的工作正常,而对于GCMS_FILE_PATH不适合?以及如何运行代码并仍然在GCMS_FILE_PATH中逃脱空格?

I'm running the GCMS software AMDIS inside an R script to analyze GCMS data. The basic syntax is:

system("AMDIS_PATH GCMS_FILE_PATH /S /GC /GE /E"

With "/S /GC /GE /E" referring to functionalities within the AMDIS program.

I want to make the code 'fool-proof', so I'm allowing paths with "/" instead of "\\". And I'm also allowing spaces in the paths, escaping them with shQuote(..., type = "cmd")

Which means the full code looks like

system(paste(shQuote(gsub("/", "\\", AMDIS_PATH, fixed = T), type = 'cmd'), shQuote(gsub("/", "\\", GCMS_FILE_PATH, fixed = T), type = 'cmd'), "/S /GD /GS /E"))

This code does not work (output = 65535), but there's a catch:

If I remove the shQuotes from my GCMS_FILE_PATH. Then everything works like it should (so long as the FILE_PATH does not contain any spaces).
So executing the code like this does work:

system(paste(shQuote(gsub("/", "\\", AMDIS_PATH, fixed = T), type = 'cmd'), gsub("/", "\\", GCMS_FILE_PATH, fixed = T), "/S /GD /GS /E"))

Any idea why shQuote works fine for the AMDIS_PATH, but not for the GCMS_FILE_PATH? And how I can run my code and still escape spaces in the GCMS_FILE_PATH?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤寂小茶 2025-01-27 08:58:09

没有一个可重复的示例很难说发生了什么在这里,请注意,?系统说:

在Windows上,系统不使用外壳,并且有一个单独的函数外壳将命令行传递给外壳。

因此,使用type ='cmd' 可能完全不合适,请考虑使用system2而不是。

您的问题可能与您期望的System要做的事情或期望shquote要做的事情。您可以通过SystemCAT来轻松地解开两者,以调试。

Without a reproducible example it's hard to say what is going on here, but note that ?system says:

On Windows, system does not use a shell and there is a separate function shell which passes command lines to a shell.

So escaping with type = 'cmd' might not be appropriate at all, consider using system2 instead.

Your issue might either be with what you expect system to do or what you expect shQuote to do. You can easily disentangle the the two by replacing system with cat for debugging.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文