bash - Which UNIX utility do I use? -


i working on variety of projects need specific compilers. fortunately, on machine i'm working on, supplied command module, makes swapping compilers easy e.g.

module unload prgenv-intel module load prgenv-cray 

or,

module swap prgenv-intel prgenv-cray 

there's command, module list, lists loaded modules. output

currently loaded modules: 1) module_foo/v2.3 2) module_bar/1.1.8 ... 17) prgenv-pgi/5.2.40 

or effect. (the compiler isn't listed @ position 17; it's somewhere on list.)

my problem: there multiple possibilities compiler loaded @ given time. want write script in each project's directory takes output of module list, finds part of list beginning prgenv (e.g. prgenv-xxx xxx compiler, whichever 1 loaded), executes command module swap prgenv-xxx prgenv-cray project requires cray compiler.

the point here can't load cray compiler (say) unless unload whichever compiler loaded. can access information examining results of module list, i'd rather write script automates of this.

is insane overkill? perhaps, i'm trying learn how solve more generic problems unix commands , figure learning experience.

#!/bin/bash  [ $# = 1 ] || { echo "usage: $0 prgenv-new" >&2; exit 1; }  new_module="${1:?}"  cur_module=$(module list | sed -n '/^[0-9]*) *\(prgenv-[a-za-z0-9_]*\)\/.*/ s//\1/p')  if [ -z "$cur_module" ] module load "$new_module" else if [ "$cur_module" != "$new_module" ] module swap "$cur_module" "$new_module" else echo "current module $cur_module; requested module" fi 

that uses standard (prehistoric) sed notations , should work version. -n suppresses normal 'echo' operation. pattern looks digits followed ) , spaces , prgenv- , identifier followed / , junk. replaces prgenv- , identifier , prints resulting line. could, perhaps should, quit after (change p { p; q; }).

the following code tests whether there module loaded. if not, loads new one. if new module different current one, swaps them. if new module same current one, says so.

the first line checks script called 1 argument, printing usage message if wasn't. second captures value in argument, checking isn't empty (so didn't fancy , invoke switch_env '', assuming script called switch_env).


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -