Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modules (Unix Env) with menus , default values specified by time

Status
Not open for further replies.

pratzen

Technical User
Aug 14, 2009
1
0
0
DE
Hi

I am rather new with tcl, we use module to modify the env. but the module avail is getting longer, making it almost unusable so I add menus , for interactive work well , for batch it will never work .

The idea is to add a menu if after some time (5 sec) a item is not chosen then load default

the other better idea would to be able to act in bacht mode

something like

"
% module load pakcage -v 0.1
"
and version 0.1 will be loaded


my dummy script is like :

"

if { [ module-info mode display ] || [module-info mode load ] } {

puts stderr "Choose the Option to load the version you want to load"
puts stderr "1 ) Ver 0.1"
puts stderr "2 ) Ver 0.2"
puts stderr "x ) Exit"

set num [gets stdin OPTION ]


if { $OPTION == "1" } {
puts stderr "Loading ...0.1"

} elseif { $OPTION == "2" } {
puts stderr "Loading ... 0.2"

} else {
puts stderr "Exiting"
}

}

"

As I said this works interactively but not in bacth mode

Thanks


 
Hi pratzen,

Here is a little example, how to do it:

interactive_and_batch.tcl
Code:
[COLOR=#804040][b]package[/b][/color] require cmdline

[COLOR=#804040][b]proc[/b][/color] execute_ver {ver} {
  [COLOR=#804040][b]puts[/b][/color] stderr [COLOR=#ff00ff]"Loading the version $ver ..."[/color]
}

[COLOR=#804040][b]proc[/b][/color] batch_mode {ver} {
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Executing in batch:"[/color]
  execute_ver [COLOR=#008080]$ver[/color]
}

[COLOR=#804040][b]proc[/b][/color] interactive_mode {} {
  [COLOR=#804040][b]set[/b][/color] key_ok [COLOR=#ff00ff]0[/color]
  [COLOR=#804040][b]while[/b][/color] { [COLOR=#008080]$key_ok[/color] == [COLOR=#ff00ff]0[/color] } {
    [COLOR=#804040][b]puts[/b][/color] stderr [COLOR=#ff00ff]"Choose the Option to load the version you want to load"[/color]
    [COLOR=#804040][b]puts[/b][/color] stderr [COLOR=#ff00ff]"1 ) Version 1"[/color]
    [COLOR=#804040][b]puts[/b][/color] stderr [COLOR=#ff00ff]"2 ) Version 2"[/color]
    [COLOR=#804040][b]puts[/b][/color] stderr [COLOR=#ff00ff]"x ) Exit"[/color]

    [COLOR=#804040][b]set[/b][/color] num [[COLOR=#804040][b]gets[/b][/color] stdin OPTION ]

    [COLOR=#804040][b]if[/b][/color] { [COLOR=#008080]$OPTION[/color] == [COLOR=#ff00ff]"1"[/color] || [COLOR=#008080]$OPTION[/color] == [COLOR=#ff00ff]"2"[/color]} {
      [COLOR=#804040][b]set[/b][/color] key_ok [COLOR=#ff00ff]1[/color]
      execute_ver [COLOR=#008080]$OPTION[/color]
    } [COLOR=#804040][b]elseif[/b][/color] { [COLOR=#008080]$OPTION[/color] == [COLOR=#ff00ff]"x"[/color] } {
      [COLOR=#804040][b]set[/b][/color] key_ok [COLOR=#ff00ff]1[/color]
      [COLOR=#804040][b]puts[/b][/color]  stderr [COLOR=#ff00ff]"Nothing done - exit ..."[/color]
    } [COLOR=#804040][b]else[/b][/color] {
      [COLOR=#804040][b]set[/b][/color] key_ok [COLOR=#ff00ff]0[/color]
    }
  }
}

[COLOR=#0000ff]#********** main program ***********[/color]
[COLOR=#0000ff]#puts "\$argv = '$argv'"[/color]
[COLOR=#804040][b]set[/b][/color] options [[COLOR=#804040][b]list[/b][/color] {v.arg [COLOR=#ff00ff]"nr"[/color] [COLOR=#ff00ff]"run in batch the version"[/color]}]
[COLOR=#804040][b]if[/b][/color] {[[COLOR=#804040][b]catch[/b][/color] {[COLOR=#804040][b]array[/b][/color] [COLOR=#804040][b]set[/b][/color] params [cmdline::getoptions argv [COLOR=#008080]$options[/color]]} res]} { 
  [COLOR=#804040][b]puts[/b][/color] stderr [COLOR=#ff00ff]"$res"[/color] 
  [COLOR=#804040][b]exit[/b][/color] 
}

[COLOR=#0000ff]#puts "param list: '[array get params]'"[/color]
[COLOR=#0000ff]#puts "\$params(v) = '$params(v)'"[/color]
[COLOR=#804040][b]if[/b][/color] {[COLOR=#008080]$params[/color](v) eq [COLOR=#ff00ff]"nr"[/color]} {
[COLOR=#0000ff]  # if no value for -v given[/color]
  interactive_mode 
} [COLOR=#804040][b]else[/b][/color] {
[COLOR=#0000ff]  # when parameter value -v given execute in batch mod[/color]
  [COLOR=#804040][b]if[/b][/color] {[COLOR=#008080]$params[/color](v) == [COLOR=#ff00ff]1[/color] || [COLOR=#008080]$params[/color](v) == [COLOR=#ff00ff]2[/color]} {
    [COLOR=#804040][b]set[/b][/color] ver [COLOR=#008080]$params[/color](v)
    [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]ver = $ver"[/color]    
    batch_mode [COLOR=#008080]$ver[/color]
  } [COLOR=#804040][b]else[/b][/color] {
    [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Error Invalid parameter -v $params(v) !!!"[/color]
  }
}
Usage:
Code:
D:\Work>tclsh85 interactive_and_batch.tcl -?
interactive_and_batch options:
 -v value             run in batch the version <nr>
 -help                Print this message
 -?                   Print this message


D:\Work>tclsh85 interactive_and_batch.tcl -v 1
$ver = 1
Executing in batch:
Loading the version 1 ...

D:\Work>tclsh85 interactive_and_batch.tcl -v 2
$ver = 2
Executing in batch:
Loading the version 2 ...

D:\Work>tclsh85 interactive_and_batch.tcl -v something
Error Invalid parameter -v something !!!

D:\Work>tclsh85 interactive_and_batch.tcl
Choose the Option to load the version you want to load
1 ) Version 1
2 ) Version 2
x ) Exit
1
Loading the version 1 ...

D:\Work>tclsh85 interactive_and_batch.tcl
Choose the Option to load the version you want to load
1 ) Version 1
2 ) Version 2
x ) Exit
2
Loading the version 2 ...

D:\Work>tclsh85 interactive_and_batch.tcl
Choose the Option to load the version you want to load
1 ) Version 1
2 ) Version 2
x ) Exit
5
Choose the Option to load the version you want to load
1 ) Version 1
2 ) Version 2
x ) Exit
x
Nothing done - exit ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top