jlconlin
jlconlin

Reputation: 15084

tcl: How to determine shell for module command

I am programming in tcl (for use with the modules command). I need to find out what shell is being used by the user. Since I've never programmed in tcl before, I don't know what is wrong with my simple code. Can someone please advise?

set shell [module-info shell]
if { $shell=="bash" } {
    puts "running bash"
}

The error I'm getting (sorry I didn't include it originally) is:

intel64(32):ERROR:102: Tcl command execution failed: if { $shell == "bash" } {
    puts "running bash
}

Note that intel64 is the file where this code is found and line 34 is the last line.

Upvotes: 2

Views: 5985

Answers (4)

Terry McClurg
Terry McClurg

Reputation: 1

set shell $::env(SHELL)
switch -glob $shell {
  "/bin/tcsh" -
  "/bin/csh" {
  setenv foamDotFile $env(FOAM_INST_DIR)/OpenFOAM-2.3.0/etc/cshrc
  }
  "/bin/ksh" -
  "/bin/bash" {
  setenv foamDotFile $env(FOAM_INST_DIR)/OpenFOAM-2.3.0/etc/bashrc
  }
}

Upvotes: 0

estani
estani

Reputation: 26547

The problem is that you should not use puts when using the modules software.

modules is just a tcl program that modifies somehow your shell environment in a shell-independent manner. This is done via a simple trick, check your environment:

set | grep -A 10 module
...
module () 
{ 
    eval `/modules-3.2.9/Modules/$MODULE_VERSION/bin/modulecmd bash $*`
}

So the trick is creating a proper string that is then evaluated by the current shell. That string comes from stdout so what should have happened after it worked is that your shell should have complaint about trying to evaluate "running bash"

I'm puzzled as to why it's supposed to have worked after you added the missing quote.

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 247162

Single quotes have no special meaning for Tcl. In Tcl, double quotes are analogous to shell double quotes, and braces ({}) are analogous to shell single quotes. You would want to write

if { $shell == "bash" } ...

See the entire syntax of Tcl documented here: http://www.tcl.tk/man/tcl8.5/TclCmd/Tcl.htm

That doesn't explain the error message though. Tcl itself does not have a system command. I can't say how this "modules command" extends Tcl. Try

puts "running bash"

or if you really really want to use echo, then

exec echo running bash

Upvotes: 3

TrojanName
TrojanName

Reputation: 5375

2 comments:

  1. use puts to display the value of $shell, so you can see exactly what's going on.
  2. your else needs to be on the same line as the right curly bracket before it.
  3. You say that the value of $shell is 'bash'. If so then you will need to change your if statement and put double quotes around it e.g. "'bash'".

Upvotes: 1

Related Questions