0

In tcsh, I can run commands like:

 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.


Currently, I've tried making a separate bash function for each subcommand, like so:

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.

However, java -version still shows the same older version and module_list does not show any new modules. Everything works great in tcsh (where I use the module add alias instead). I have tried different versions of module command. The latest version tested is 3.2.10. The result is the same on RHEL6 and RHEL7

Any ideas how to solve this?

EDIT

Based on some clever comments I tried to the exact same command for 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?

So I guess the question is rather about how the modulescmd differ from the tcsh alias module add

BR Patrik

patrik
  • 4,506
  • 6
  • 24
  • 48
  • 2
    I don't see how any of the problems you are seeing relate to `bash`. The only shell problem would be the failure to quote`$1`, but given the arguments you are passing to `module_add`, that isn't the source of your problem. – chepner Jul 05 '18 at 13:50
  • @chepner You have a point, i just noticed that the exact same command works the same way for tcsh. Any idea why `/app/modules/0/bin/modulecmd tcsh add jdk/1.8.0_162` and `module add jdk/1.8.0_162` works differently in tcsh? – patrik Jul 05 '18 at 14:40
  • @CharlesDuffy I would say this is about the tsch alias `module add`. I add the tag tcsh, since this now seems to be a question about how tcsh works. – patrik Jul 05 '18 at 14:58
  • It looks to me like the alias just runs modulecmd, and modulecmd is responsible for doing the work. Or, rather, emitting a script on output that does the work. – Charles Duffy Jul 05 '18 at 15:00
  • When you run the bash version, does it emit something that looks like shell commands on stdout? – Charles Duffy Jul 05 '18 at 15:01
  • ...anyhow, if you show a working tcsh implementation of the aliases you want (instead of just asserting that there *is* a tcsh alias, show it!), I'd be more able to provide bash alternatives (assuming that modulecmd's generation of equivalent bash scripts as output does in fact work correctly). – Charles Duffy Jul 05 '18 at 15:06
  • @CharlesDuffy yes it does. It output something that looks as if it is setting some environment variables. Btw, got it now, Looks quite obvious when I look at it now... alias module eval `/app/modules/0/bin/modulecmd tcsh !*` – patrik Jul 05 '18 at 15:09
  • 1
    Good -- that's what I assumed it was in writing my answer. – Charles Duffy Jul 05 '18 at 15:12

1 Answers1

2

modulecmd emits shell scripts on its stdout (thus the argument telling it which shell to generate a script for).

A shell needs to actually execute those commands for them to take effect; this is what eval does. (Don't ever use eval unless you trust the people who wrote the program that generated the output you're evaling to be rigorous about corner cases -- it lends itself to security bugs).

Thus, if your existing tcsh alias for the module command is:

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

...the bash equivalent would be:

module() { eval "$(/app/modules/0/bin/modulecmd bash "$@")"; }
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441