【问题标题】:R: Alias command called with system()R:使用 system() 调用的别名命令
【发布时间】:2015-07-30 02:54:30
【问题描述】:

在我的~/.bash_profile 文件中,我输入了这个alias 命令:

which mvsync
alias mvsync='rsync --remove-source-files -arvuP'
    /usr/bin/rsync

它在 bash shell 中运行良好,但是当我在 R 中使用 system 调用它时,我得到一个找不到命令:

R
system('mvsync --help')
sh: mvsync: command not found

## Or
system('mvsync --help', intern=TRUE)
sh: mvsync: command not found
Error in system("mvsync --help", intern = TRUE) : 
  error in running command

## Or
system("bash -i -c mvsync")
bash: mvsync: command not found

[4]+  Stopped                 R

bash_profile 中的其他环境变量可以被 R/system() 正确识别。 知道如何/是否可以修复吗?

这是 R 会话信息:

sessionInfo()
R version 3.1.3 (2015-03-09)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: CentOS release 6.6 (Final)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] graphics  grDevices utils     datasets  stats     methods   base     

other attached packages:
[1] ggplot2_1.0.0    data.table_1.9.4

loaded via a namespace (and not attached):
 [1] chron_2.3-45     colorspace_1.2-4 digest_0.6.8     grid_3.1.3       gtable_0.1.2     MASS_7.3-39      munsell_0.4.2    plyr_1.8.1       proto_0.3-10     Rcpp_0.11.3      reshape2_1.4     scales_0.2.4     stringr_0.6.2   

【问题讨论】:

  • 你试过加system('mvsync --help', intern=TRUE)
  • @pacomet 恐怕不走运(见编辑后的帖子),但谢谢。
  • 别名是 bash 主义,而不是 shell 主义(我知道这听起来很矛盾)。试试system("bash -i -c mvsync")-i 对于让 bash 访问别名等功能很重要)。
  • @r2evans 谢谢,但仍然无法正常工作(请参阅编辑后的帖子)。顺便说一句,我看到 /bin/sh 是 bash 的符号链接。
  • @dariober,好吧,我的意思是在系统调用中使用 bash 与交互式使用 bash 不同,因此需要-i。不过很好奇,因为我发布的内容适用于我的 ubuntu 和 windows 机器。尝试-l(登录)以及(参见similar question),假设这是您定义别名的where的问题。

标签: r bash .bash-profile


【解决方案1】:

您的 shell 可能不是登录 shell:http://linux.die.net/man/1/bash

当 bash 作为交互式登录 shell 或作为带有 --login 选项的非交互式 shell 调用时,它首先从文件 /etc/profile 中读取并执行命令(如果该文件存在)。读取该文件后,它会按顺序查找 ~/.bash_profile、~/.bash_login 和 ~/.profile,并从第一个存在且可读的文件中读取并执行命令。启动 shell 时可以使用 --noprofile 选项来禁止这种行为。

--login 可能有效,但最好将别名放在 .bashrc 中,即使它不是登录 shell 也会执行

【讨论】:

    【解决方案2】:

    您可以通过使用 system2 并在交互模式下使用 bash 来解决此问题,即添加 -i 或作为登录 shell (-l),具体取决于您添加别名的位置。

    ~/.bashrc 中分配别名时的示例:

    system2('/bin/bash', args = c('-ic', shQuote('mvsync --help')))
    

    man bash 我们了解到

    当 bash 作为交互式登录 shell 或作为具有 --login [-l] 选项的非交互式 shell 调用时,它首先从文件 /etc/profile 中读取并执行命令,如果该文件存在的话。读取该文件后,它会按顺序查找~/.bash_profile~/.bash_login~/.profile,并从第一个存在且可读的文件中读取并执行命令。当 shell 启动时可以使用--noprofile 选项来禁止这种行为。

    [...]

    当启动不是登录 shell 的交互式 shell [-i] 时,如果存在这些文件,bash 会读取并执行来自 /etc/bash.bashrc~/.bashrc 的命令。这可以通过使用 --norc 选项来禁止。 --rcfile 文件选项将强制 bash 从文件而不是 /etc/bash.bashrc~/.bashrc 读取和执行命令。

    【讨论】:

      【解决方案3】:

      显然,非交互式 shell 会话中不会扩展别名。

      您需要运行

      shopt -s expand_aliases
      

      在你的系统调用中,例如

      system('bash -l',input=c("shopt -s expand_aliases","mvsync --help"))
      

      或定义BASH_ENV 环境变量并将其添加到该文件中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多