system() 返回错误但 shell() 有效 [英] system() returns error but shell() works

查看:31
本文介绍了system() 返回错误但 shell() 有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近完成了 (Windows) 系统更新,现在我无法再使用 R 运行系统命令.我尝试重新安装 R 和所有使用过的软件包,但这并不能解决我的问题.重新启动也没有解决问题.

I've recently done a (Windows) system update and now I cannot run system commands anymore using R. I've tried to reinstall R and all the used packages but this doesn't resolve my problem. Rebooting also didn't solve the problem.

我刚刚发现虽然 system() 和 system2() 命令不再起作用,但 shell() 命令确实起作用.

I just discovered that while the system() and system2() commands don't work anymore, the shell() command does work.

> system('ECHO "test"')
Warning message:
running command 'ECHO "test"' had status 127
> system2('ECHO "test"')
Warning message:
running command 'ECHO \"test\"' had status 127
> shell('ECHO "test"')
"test"

文档描述了 shell() 应该是一个简单的用户友好的 system() 包装器.因此我不明白为什么 shell() 有效,但系统却没有.

The documentation describes that shell() should be a simple user-friendly wrapper around system(). Therefore I don't understand why shell() works, but system doesn't.

在另一台 system() 函数仍然可以工作的计算机上,我检查了差异.Windows 版本、R 版本和包版本是相等的(至少根据干净会话中的 sessionInfo()).与我可以使用的 system() 相比,Shell() 指定了一些额外的参数:

With another computer on which the system() functions still work, I've checked for differences. The Windows version, the R version and the package versions are equal (at least according to sessionInfo() in a clean session). Shell() specifies a few additional arguments compared to system() with which I could play:

  • shell 指定要使用的 shell.详细信息"部分告诉我这将检查两个环境变量:R_SHELL 和 COMSPEC.R_SHELL 未在两个系统(全局和本地)上定义.COMSPEC 已定义并在两个系统上都指向 cmd.exe(在同一文件夹中).我已经通过这个可执行文件检查并运行了纯命令.
  • flag 不知道这是做什么的,但它与 system() 命令有所不同.据我了解,更改它的唯一原因是使用不同的 shell/OS(例如 Linux 上的 Bash).
  • intern 将 shell() 和 system() 设置为 FALSE.
  • wait 将 shell() 和 system() 设置为 TRUE.
  • translate 设置为 TRUE,但系统调用不包含/"或\".正如预期的那样,使用这个布尔值不会改变结果.
  • mustWork 与出现错误时的输出相关.将返回错误或警告.更改此布尔值不会改变行为.
  • shell specifies the shell to be used. The 'Details' section tells me that this will check two environment variables: R_SHELL and COMSPEC. R_SHELL is not defined on both systems (global and local). COMSPEC is defined and refers on both systems to cmd.exe (in the same folder). I've checked and running the pure command via this executable works.
  • flag no idea what this does, but it is a difference with the system() command. As far as I understand it, the only reason to change it would be when a different shell/OS is used (e.g. Bash on Linux).
  • intern set to FALSE for both shell() and system().
  • wait set to TRUE for both shell() and system().
  • translate set to TRUE, but the system call doesn't contain '/' nor '\'. As expected, playing with this Boolean doesn't change the results.
  • mustWork related to the output in case of an error. Will return an error or a warning. Changing this Boolean doesn't change the behavior.

因此,我只能得出结论,shell() 在 system() 之上做了一些以某种方式改变其行为的事情.此行为未在文档中指定,也未通过任何参数访问.

Therefore, I can only conclude that shell() does something on top of system() that somehow changes its behavior. This behavior is not specified in the documentation, nor accessible via any of the arguments.

我可以在哪里寻找两者之间的其他差异以找到此错误的来源?

Where could I look for additional differences between both in order to find the source of this bug?

推荐答案

首先我尝试了是否可以重现此问题(在 Win10 上):

First I tried if I can reproduce this (on Win10):

system('ECHO "test"')
#Warning message:
#running command 'ECHO "test"' had status 127

shell('ECHO "test"')
#"test"

然后我查看了 shell 的源代码,发现它在将命令传递给 system 之前更改了命令.在我的系统上:

Then I looked at shell's source code and discovered that it changes the command before passing it to system. On my system:

system('C:\\WINDOWS\\system32\\cmd.exe /c ECHO "test"')
#"test"

另见 Sys.getenv("COMSPEC") 如果 Sys.getenv("R_SHELL") 为空,它会给出 shell.请注意,文档指出 POSIX 标准要求在 shell 下运行命令.help("system") 解释了在类似 Unix 的操作系统上 system 启动一个 shell 并推荐 shell(在 Windows 上).

See also Sys.getenv("COMSPEC") which gives the shell if Sys.getenv("R_SHELL") is empty. Note that the documentation states that the POSIX standards require running commands under a shell. help("system") explains that on a Unix-alike OS system launches a shell and recommends shell (on Windows).

这篇关于system() 返回错误但 shell() 有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆