gpt4 book ai didi

linux - 源代码在 shell 脚本中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 12:05:59 26 4
gpt4 key购买 nike

我们在 Linux 服务器上运行,我需要在 anaconda 中运行 R 脚本。

如何通过 shell 脚本进入 anaconda 环境。

通过命令行:

$  source anaconda 
$ Rscript <file.R>

工作得很好。

如果 anaconda 已经启动,则以下 .sh 可以正常工作:

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

是否有以下版本:

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

这会打开 anconda 并运行脚本吗?

我的 anaconda 位于 ./anaconda 中。我试过将其设置为我的初始路径。

谢谢!

最佳答案

我们缺少的是 anaconda 文件的内容。我认为它将 Rscript 命令创建为 alias,因为默认情况下 shell 不会扩展别名,除非 shell 是交互式的。例如:

互动,万事大吉

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

但是在脚本中,我可以重现你的错误

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

强制 bash 交互:

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

试试这个:

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

关于linux - 源代码在 shell 脚本中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50703081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com