R: Alias ​​command invoked using system ()

In my ~/.bash_profile file, I put this alias command:

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

It works fine with the bash shell, but when I call it from R using system , I get a command not found:

 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 

Other environment variables in bash_profile are correctly recognized by R / system (). Any idea on how / if it can be fixed?

This is the session information 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 
+7
source share
3 answers

Your shell is probably not a login shell: http://linux.die.net/man/1/bash

When bash is invoked as an interactive login shell or as a non-interactive shell with the --login option, it first reads and executes commands from the / etc / profile file, if that file exists. After reading this file, it searches for ~ / .bash_profile, ~ / .bash_login and ~ / .profile in that order and reads and executes commands from the first one that exists and is readable. The -noprofile option can be used when the shell begins to block this behavior.

- the login may work, but it may be better to put your alias in .bashrc, which will be executed even if it is not an input shell

+3
source

Obviously, aliases are not disclosed in non-interactive shell sessions.

You will need to either run

 shopt -s expand_aliases 

Inside your system call, for example

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

Or define the BASH_ENV environment and add it to this file.

0
source

You can solve this problem by using system2 and using bash interactively, i.e. -i By adding -i or as a login shell ( -l ), depending on where you add the aliases.

Example when your aliases are assigned in your ~/.bashrc :

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

from man bash we learn that

When bash is invoked as an interactive login shell or as a non-interactive shell -i with the --login [ -l ] option, it first reads and executes commands from the /etc/profile file, if that file exists. After reading this file, it searches for ~/.bash_profile , ~/.bash_login and ~/.profile in the specified order, and also reads and executes commands from the first one that exists and is readable. The --noprofile option can be used when the shell starts to prohibit this behavior.

[...]

When an interactive shell is started that is not a login shell [ -i ], bash reads and executes the commands from /etc/bash.bashrc and ~/.bashrc if these files exist. This can be disabled with the --norc option. The file option --rcfile will force bash to read and execute commands from the file instead of /etc/bash.bashrc and ~/.bashrc .

0
source

Source: https://habr.com/ru/post/987423/


All Articles