简体   繁体   English

将tcsh的“模块”别名(调用modulecmd)修改为bash

[英]Adapting “module” alias (invoking modulecmd) from tcsh to bash

In tcsh, I can run commands like: 在tcsh中,我可以运行以下命令:

 module add jdk/1.8.0_162 

...using an alias defined as such: ...使用这样定义的别名:

alias module 'eval `/app/modules/0/bin/modulecmd tcsh \!*`'

I'm trying to get an equivalent to this for bash. 我正在尝试使bash与此等效。


Currently, I've tried making a separate bash function for each subcommand, like so: 当前,我尝试为每个子命令制作一个单独的bash函数,如下所示:

function module_add {
    /app/modules/0/bin/modulecmd bash add $1 2>>err.log
}

function module_rm {
    /app/modules/0/bin/modulecmd bash rm $1 2>>err.log
}

function module_list {
    /app/modules/0/bin/modulecmd bash list
}

java -version
module_list
module_rm 'j2re'
module_add 'jdk/1.8.0_162'
module_add 'gtk+/2.24.17'
module_list
java -version

I can be sure the program calls have been executed, since the unexisting module (added by purpose for testing) gtk+/2.24.17 creates an entry in err.log. 我可以确定程序调用已执行,因为不存在的模块(出于测试目的而添加)gtk + / 2.24.17在err.log中创建了一个条目。

However, java -version still shows the same older version and module_list does not show any new modules. 但是,java -version仍显示相同的旧版本,而module_list不显示任何新模块。 Everything works great in tcsh (where I use the module add alias instead). 一切在tcsh中都运行良好(我在其中使用模块添加别名)。 I have tried different versions of module command. 我尝试了不同版本的模块命令。 The latest version tested is 3.2.10. 测试的最新版本是3.2.10。 The result is the same on RHEL6 and RHEL7 结果在RHEL6和RHEL7上相同

Any ideas how to solve this? 任何想法如何解决这个问题?

EDIT 编辑

Based on some clever comments I tried to the exact same command for tcsh 基于一些聪明的评论,我尝试对tcsh使用完全相同的命令

/app/modules/0/bin/modulecmd tcsh add jdk/1.8.0_162

and it gave the same result. 它给出了相同的结果。 Anyone who knows the difference between that command and 知道该命令与

module add jdk/1.8.0_162 

in tcsh? 在tcsh中?

So I guess the question is rather about how the modulescmd differ from the tcsh alias module add 所以我想问题是关于modulescmd与tcsh别名module add有何不同

BR Patrik 帕特里克(BR Patrik)

modulecmd emits shell scripts on its stdout (thus the argument telling it which shell to generate a script for). modulecmd在其stdout上发出shell脚本(因此,该参数告诉它为哪个shell生成脚本)。

A shell needs to actually execute those commands for them to take effect; Shell需要实际执行这些命令才能使它们生效。 this is what eval does. 这是eval所做的。 (Don't ever use eval unless you trust the people who wrote the program that generated the output you're eval ing to be rigorous about corner cases -- it lends itself to security bugs ). (永远不要使用eval除非您信任编写要生成您要eval的输出的程序的人员对eval情况非常严格,否则会导致安全漏洞 )。

Thus, if your existing tcsh alias for the module command is: 因此,如果module命令的现有tcsh别名为:

alias module 'eval `/app/modules/0/bin/modulecmd tcsh \!*`'

...the bash equivalent would be: ... bash等效项为:

module() { eval "$(/app/modules/0/bin/modulecmd bash "$@")"; }

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

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