cea-hpc / modules

Environment Modules: provides dynamic modification of a user's environment
http://modules.sourceforge.net/
GNU General Public License v2.0
668 stars 102 forks source link

eval a command #519

Closed antoine-morvan closed 6 months ago

antoine-morvan commented 6 months ago

Hello,

I am setting up a module file for the JUBE tool. This tools comes with a command to add shell completion: https://apps.fz-juelich.de/jsc/jube/jube2/docu/commandline.html#complete

The recommanded usage is : eval "$(jube complete)"

However, the source-sh command in a module file is looking for a script file, and cannot execute a plain command. Similarly, the system command does not know of any eval binary; as it is a builtin function of the shell.

How would you call this eval command to properly setup shell completion in a module file ?

Thanks.

xdelaruelle commented 6 months ago

I would suggest to create a temporary shell script that does the eval.

Here is a small shell script example that outputs shell code on stdout like jube complete does:

$ cat myscript
echo "export FOO=value"

A modulefile can be crafted to generate a temporary script that evaluates the above script to generate the corresponding environment change orders:

$ cat foo
#%Module
set tmpfile /tmp/source-myscript-[module-info username]
set fid [open $tmpfile w]

puts $fid {#!/bin/bash
eval $(./myscript)}

close $fid

source-sh bash $tmpfile
file delete $tmpfile
$ module show ./foo
-------------------------------------------------------------------
/path/to/foo:

setenv          FOO value
-------------------------------------------------------------------
$ module load ./foo
$ echo $FOO
value
$ module unload ./foo
$ echo $FOO

$
antoine-morvan commented 6 months ago

Works like a charm, thanks.

xdelaruelle commented 5 months ago

I am adding a bash-eval shell mode to the sh-to-mod sub-command and source-sh modulefile command to make such shell code evaluation easier to achieve.

source-sh bash-eval jube complete