简体   繁体   English

源代码无法在Shell脚本中运行

[英]Source not working within shell script

We operate on a Linux server and I need to run an R script within anaconda. 我们在Linux服务器上运行,我需要在anaconda中运行R脚本。

How do I engage the anaconda environment via a shell script. 如何通过shell脚本参与anaconda环境。

Via the command line: 通过命令行:

$  source anaconda 
$  Rscript <file.R>

works just fine. 效果很好。

If anaconda is already engaged the following .sh works fine: 如果anaconda已经使用,则以下.sh可以正常工作:

#!/bin/bash
PATH=$PATH:/usr/local/bin/anaconda
echo Rscript ./name.R

Is there a version of: 是否有以下版本:

#!/bin/bash
PATH=$PATH:/usr/local/bin
echo source anaconda; Rscript ./name.R

That would open anconda and run the script? 那会打开anconda并运行脚本吗?

My anaconda is located within ./anaconda. 我的蟒蛇位于./anaconda中。 I've tried setting that as my initial path. 我尝试将其设置为我的初始路径。

Thanks! 谢谢!

What we're missing is the contents of the anaconda file. 我们缺少的是anaconda文件的内容。 I'm thinking it creates the Rscript command as an alias , since the shell by default does not expand aliases unless the shell is interactive. 我认为它Rscript命令创建为别名 ,因为默认情况下,除非shell是交互式的,否则shell不会扩展别名。 For example: 例如:

Interactive, all is well 互动,一切都很好

$ cat > anaconda
alias Rscript='echo this is Rscript:'
$ source ./anaconda
$ Rscript foo bar
this is Rscript: foo bar

But in a script, I can reproduce your error 但是在脚本中,我可以重现您的错误

$ cat > test.sh
#!/bin/bash
source ./anaconda
Rscript foo bar
$ bash test.sh
test.sh: line 3: Rscript: command not found

Forcing bash to be interactive: 强制bash进行交互:

$ bash -i test.sh
this is Rscript: foo bar

Try this: 尝试这个:

#!/bin/bash
shopt -s expand_aliases
source /path/to/anaconda
Rscript ./name.R

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM