Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to source a csh script into a modulefile? #399

Closed
rdauria opened this issue May 27, 2021 · 5 comments
Closed

How to source a csh script into a modulefile? #399

rdauria opened this issue May 27, 2021 · 5 comments
Labels

Comments

@rdauria
Copy link

rdauria commented May 27, 2021

Hello,

Sorry for posting here, but I am not sure whether this is a question or a feature request. I have been using the sourcing of scripts to create environmental modules as described here:

https://modules.readthedocs.io/en/latest/cookbook/source-script-in-modulefile.html

in particular:

#%Module4.6
source-sh bash example/source-script-in-modulefile/foo-1.2/foo-setup.sh arg1
EOF

Is there a way to source csh/tcsh scripts? I could not find mention in the documentation (https://modules.readthedocs.io/en/latest/cookbook/source-script-in-modulefile.html) or a man page that would describe the syntax. Should one use something line:

#%Module4.6
source-sh csh example/source-script-in-modulefile/foo-1.2/foo-setup.csh arg1
EOF

Notice that the use case is to create a modulefile for anaconda that will prevent the users from running:

conda init

which (quite ignorantly - I should say) writes in the user initialization files. The idea is to use instead in the modulefile for anaconda something along the lines of:

set myshell $::env(SHELL)

if {  $myshell == "/bin/bash" || $myshell == "/bin/dash" || $myshell == "/bin/ksh" || $myshell == "/bin/sh"  || $myshell == "/bin/zsh" } then {
        source-sh bash /path/to/anaconda3/etc/profile.d/conda.sh
} elseif {  $myshell == "/bin/csh" || $myshell == "/bin/tcsh" } then {
        source-sh csh /path/to/anaconda3/etc/profile.d/conda.csh
}

which would support both sh-based as well as csh-based shells.

The problem is that:

source-sh csh /path/to/anaconda3/etc/profile.d/conda.csh

does not appear to work.

Do you have any suggestions? If csh script can be sources could you update the documentation an upload and example?

Thanks,

RD

Please use preferably the Modules mailing-list to post question:

https://sourceforge.net/projects/modules/lists/modules-interest

@xdelaruelle
Copy link
Member

Hello,

source-sh supports sh-kind, csh-kind and fish shells. I will explicit that in the cookbook recipe.

When working with simple script you can even use the sh script through source-sh to initialize a csh session (as module produce csh shell code in this end for a csh session).

But here, the scripts are a bit more complex : they initialize a function for sh-shell or an alias for csh-shell.

For the dispatch, I suggest to rely on the module-info shelltype modulefile command:

#%Module
switch -- [module-info shelltype] {
    sh {
        source-sh sh /path/to/anaconda3/etc/profile.d/conda.sh
    }
    csh {
        source-sh csh /path/to/anaconda3/etc/profile.d/conda.csh
    }
}

Now to understand why it does not work on your setup. I suspect that the conda initialization script was already sourced prior loading the module that rely on source-sh. source-sh proceed this way when it is evaluated:

  1. dump current environment
  2. run designated script with designated shell
  3. dump resulting environment
  4. diff the 2 environment dumps

So if the conda.csh was already run prior using module, the initial environment already contains the conda definitions, thus it is equal to the resulting environment so the diff between both is empty.

Regards,
Xavier

@rdauria
Copy link
Author

rdauria commented May 27, 2021

Hello Xavier,

Super!!! Your suggestion works like a charm! It also has the advantage that it looks for the type of the calling shell independently of what is the login $SHELL. Thanks a million!!!

I am sure that if you were to document it in the cookbook many users would be thankful for it (anaconda can finally be loaded as a module without having to source a script!).

As I was working with tcsh I noticed that when using modules (version 4.7.0) in a tcsh shell the history gets garbled, I am not sure if I should open a new ticket (please let me know). This is what I mean:

[rd@testbox ~]$ tcsh
[rd@testbox ~]$ module av
------------------------------------ /MODULES/modulefiles -------------------------------------
R/4.0.5 <L>  

--------------------------------- /MODULES/4.7.0/modulefiles ----------------------------------
dot  module-git  module-info  modules  null  use.own  

Key:
<module-tag>  <L>=loaded  
[rd@testbox ~]$ module load R/4.0.5
[rd@testbox ~]$ module li
Currently Loaded Modulefiles:
 1) R/4.0.5  
[rd@testbox ~]$ history | tail -n 5
    11  9:16    module load R/4.0.5
-50000000       9:16    load
    12  9:16    module li
-50000000       9:16    
    13  9:16    history | tail -n 5

this behavior appears only on tcsh shell and if I exit the shell and reenter it the history shows up OK:

[rd@testbox ~]$ exit
[rd@testbox ~]$ tcsh
[rd@testbox ~]$ history | tail -n 5
    12	9:18	less test
    13	9:21	history
    14	9:21	module load R/4.0.5
    15	9:21	module av
    16	9:21	history | tail -n 5

for your reference I am on a CentOS 7.9.2009 box.

Any idea of what is going on and how to get around it?

Thanks,

RD

@xdelaruelle
Copy link
Member

Perfect, I will improve the recipe as suggested.

Regarding the tcsh issue, this is something that were spotted 2 years ago. I have reported the issue upstream with code to reproduce it, but it has not been solved yet.

There is a workaround available with the wa_277 module configuration or MODULES_WA_277 environment variable.

See https://modules.readthedocs.io/en/stable/MIGRATING.html?highlight=MODULES_WA_277#other-new-sub-commands-command-line-switches-and-environment-variables

This configuration should be defined prior module command initialization as it changes the module command alias on tcsh.

The counterpart of this workaround is that it weakens shell evaluation of the code produced by modulefiles. Characters with special meaning for Tcsh shell (like { and }) may not be used anymore in shell alias definition otherwise the evaluation of the code produced by modulefiles will return a syntax error.

@xdelaruelle
Copy link
Member

I have just updated the cookbook recipe to provide additional examples to use source-sh similar to what we discussed here.

@jeevan4
Copy link

jeevan4 commented Jul 12, 2024

I am using module framework on windows and I have downloaded windows package as well as install tclsh on my machine..

Is there a way to source a bat file or shell script on windows command prompt ?

source-sh bash /path/to/bash/script.sh 
or
source-sh bat /path/to/bat/script.bat
or
../../../modulecommand/modulecmd.tcl python sh-to-mod bash /path/to/bash/script.sh 
or
../../../modulecommand/modulecmd.tcl python sh-to-mod bat /path/to/bat/script.bat

Appreciate your inputs as I need to create a single modulefile for both linux and windows such that this is possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants