From 4326096153d51a1a162873843f0a20b050242353 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 19 Mar 2010 20:02:05 +0100 Subject: [PATCH] STYLE: cleanup some shell script syntax --- bin/foamEndJob | 106 ++++++++++++------- bin/foamJob | 265 +++++++++++++++++++++++++----------------------- bin/mpirunDebug | 151 +++++++++++++++------------ 3 files changed, 289 insertions(+), 233 deletions(-) diff --git a/bin/foamEndJob b/bin/foamEndJob index f82f7409ce..ba213c86aa 100755 --- a/bin/foamEndJob +++ b/bin/foamEndJob @@ -85,7 +85,8 @@ setRawEntry() { oldLine=`echo "$oldNumLine" | sed -e 's/^[^:]*://'` oldKey=`getKey "$oldLine"` oldVal=`getRawEntry $1 "$2"` - if [ ! "$oldKey" -o ! "$oldVal" -o ! "$oldLine" ]; then + if [ ! "$oldKey" -o ! "$oldVal" -o ! "$oldLine" ] + then echo "setRawStringEntry: entry $2 not found in $1" echo "oldKey=$oldKey" echo "lineNo=$lineNo" @@ -134,9 +135,12 @@ getBoolEntry() } # newerFile file1 file2 -newerFile() { +# ... could also use if [ $file1 -nt $file2 ] ... +newerFile() +{ latest=`ls -1 -t $1 $2 2> /dev/null | head -1` - if [ "$latest" = $1 ]; then + if [ "$latest" = $1 ] + then return 0 else return 1 @@ -149,8 +153,8 @@ processExists() { ps -u $LOGNAME -o 'pid' | fgrep $1 >/dev/null } -printUsage() { -cat << USAGELABEL +usage() { +cat << USAGE Usage: $PROGNAME [-n] or $PROGNAME -c @@ -165,7 +169,8 @@ finish. Restores original controlDict if The -c option clears any outstanding $PROGNAME for the case. -USAGELABEL +USAGE + exit 1 } @@ -174,7 +179,8 @@ restoreDict() { trap 2 3 15 echo "$PROGNAME : Restoring controlDict from controlDict_bak." - if [ -r ${controlDict}_bak ]; then + if [ -r ${controlDict}_bak ] + then cp ${controlDict}_bak $controlDict fi @@ -208,61 +214,69 @@ esac # # Initial checks # -if [ $# -lt 3 ]; then - printUsage - exit 1 +if [ $# -lt 3 ] +then + usage fi + STOPNOW='' -if [ $1 = '-n' ]; then +if [ $1 = '-n' ] +then STOPNOW='yes' shift fi CLEAR='' -if [ $1 = '-c' ]; then +if [ $1 = '-c' ] +then CLEAR='yes' shift - if [ $# -ne 2 ]; then - printUsage - exit 1 + if [ $# -ne 2 ] + then + usage fi ROOT=$1 CASE=$2 else - if [ $# -ne 3 ]; then - printUsage - exit 1 + if [ $# -ne 3 ] + then + usage fi ROOT=$1 CASE=$2 PID=$3 fi -CASE=`echo $CASE | sed -e 's!/.*!!'` #strip of processorXXX ending +CASE=`echo $CASE | sed -e 's!/.*!!'` # strip of processorXXX ending #- Pid actually running -if [ ! "$CLEAR" ]; then +if [ ! "$CLEAR" ] +then processExists $PID - if [ $? -ne 0 ] ;then + if [ $? -ne 0 ] + then echo "$PROGNAME : process $PID not running." exit 1 fi fi #- case directory writeable -if [ ! -w $ROOT/$CASE ]; then +if [ ! -w $ROOT/$CASE ] +then echo "$PROGNAME : $ROOT/$CASE is not writeable." exit 1 fi #- Controldict writeable controlDict=$ROOT/$CASE/system/controlDict -if [ ! -w $controlDict ]; then +if [ ! -w $controlDict ] +then echo "$PROGNAME : $controlDict is not writeable." exit 1 fi #- runTimeModifiable getBoolEntry $controlDict 'runTimeModifiable' -if [ $? -ne 0 ]; then +if [ $? -ne 0 ] +then echo "$PROGNAME : runTimeModifiable not true in dictionary $controlDict." exit 1 fi @@ -270,12 +284,14 @@ fi # #- Check if another foamEndJob running # -if [ "$CLEAR" ]; then +if [ "$CLEAR" ] +then pidFiles=`ls $ROOT/$CASE/.foamEndJob* 2>/dev/null` for pidFile in $pidFiles do pid=`cat $pidFile` - if [ "$pid" ]; then + if [ "$pid" ] + then echo "$PROGNAME : found $PROGNAME (pid $pid) for Foam process" echo " root: $ROOT" echo " case: $CASE" @@ -288,11 +304,14 @@ if [ "$CLEAR" ]; then fi pidFile=$ROOT/$CASE/.foamEndJob${PID} -if [ -f $pidFile ]; then +if [ -f $pidFile ] +then pid=`cat $pidFile` - if [ "$pid" ]; then + if [ "$pid" ] + then processExists $pid - if [ $? -eq 0 ] ;then + if [ $? -eq 0 ] + then echo "$PROGNAME : found running $PROGNAME (pid $pid) for Foam process" echo " root: $ROOT" echo " case: $CASE" @@ -314,28 +333,32 @@ echo $$ > $pidFile #- startTime startTime=`getEntry $controlDict 'startTime'` -if [ ! "$startTime" ]; then +if [ ! "$startTime" ] +then echo "$PROGNAME : startTime not set in dictionary $controlDict." exit 1 fi #- Write interval writeInterval=`getEntry $controlDict 'writeInterval'` -if [ ! "$writeInterval" ]; then +if [ ! "$writeInterval" ] +then echo "$PROGNAME : writeInterval not set in dictionary $controlDict." exit 1 fi #- stopAt stopAt=`getEntry $controlDict 'stopAt'` -if [ ! "$stopAt" ]; then +if [ ! "$stopAt" ] +then echo "$PROGNAME : stopAt not set in dictionary $controlDict." exit 1 fi #- endTime endTime=`getEntry $controlDict 'endTime'` -if [ ! "$endTime" ]; then +if [ ! "$endTime" ] +then echo "$PROGNAME : endTime not set in dictionary $controlDict." exit 1 fi @@ -353,7 +376,8 @@ cp $controlDict ${controlDict}_bak #- Set up handler to restore controlDict trap restoreDict 2 3 15 -if [ "$STOPNOW" ]; then +if [ "$STOPNOW" ] +then setRawEntry $controlDict 'stopAt' 'nextWrite' setRawEntry $controlDict 'writeInterval' '1' @@ -385,7 +409,8 @@ if newerFile ${controlDict} ${controlDict}_bak; then iter=0 while newerFile ${controlDict} ${controlDict}_bak do - if [ $iter -ge 120 ]; then + if [ $iter -ge 120 ] + then #- 120*5 sec = 10 mins passed. Give up echo "$PROGNAME : File date not yet ok after 10 mins. Giving up." break @@ -412,22 +437,25 @@ while true do sleep 5 - if [ ! -r ${controlDict}_bak ]; then + if [ ! -r ${controlDict}_bak ] + then echo "$PROGNAME : ${controlDict}_bak dissappeared. Exiting without restore." exit 1 fi - if newerFile ${controlDict} ${controlDict}_bak; then + if newerFile ${controlDict} ${controlDict}_bak + then echo "$PROGNAME : ${controlDict} modified externally. Exiting without restore." exit 0 fi processExists $PID - if [ $? -ne 0 ] ;then + if [ $? -ne 0 ] + then #- Job finished break fi - #echo "Foam job $PID still running ..." + # echo "Foam job $PID still running ..." done #- Dictionary restore diff --git a/bin/foamJob b/bin/foamJob index ca51fb6074..612d003fbe 100755 --- a/bin/foamJob +++ b/bin/foamJob @@ -30,14 +30,14 @@ # #------------------------------------------------------------------------------ usage() { - while [ "$#" -ge 1 ]; do echo "$1"; shift; done - cat< ... options: -case specify alternative case directory, default is the cwd - -s also sends output to screen -p parallel run of processors + -s also sends output to screen -v specify OpenFOAM version -help print the usage @@ -45,56 +45,46 @@ options: Redirects the output to 'log' in the case directory USAGE - exit 1 + exit 1 } unset version # replacement for possibly buggy 'which' findExec() { - case "$1" in - */*) - if [ -x "$1" ] ; then - echo "$1" - return 0 - fi - ;; - esac + case "$1" in + */*) + if [ -x "$1" ] + then + echo "$1" + return 0 + fi + ;; + esac - oldIFS=$IFS - IFS=':' - for d in $PATH - do - # echo "testing: $d/$1" 1>&2 - if [ -x "$d/$1" -a ! -d "$d/$1" ] ; then - # echo "Found exec: $d/$1" 1>&2 - IFS=$oldIFS - echo "$d/$1" - return 0 - fi - done - IFS=$oldIFS - echo "" - return 1 -} - -# grep for $1 -getPID() { - ps -u $LOGNAME -o 'pid,args' | fgrep "$1 " | fgrep -v grep | head -1 | awk '{ print $1 }' + oldIFS=$IFS + IFS=':' + for d in $PATH + do + # echo "testing: $d/$1" 1>&2 + if [ -x "$d/$1" -a ! -d "$d/$1" ] + then + # echo "Found exec: $d/$1" 1>&2 + IFS=$oldIFS + echo "$d/$1" + return 0 + fi + done + IFS=$oldIFS + echo "" + return 1 } -consultGuide() { -cat</dev/null || usage "directory does not exist: '$caseDir'" ;; - -s) - SCREEN=yes + -p) + parallelOpt=true shift ;; - -p) - PARALLEL=yes + -s) + screenOpt=true shift ;; -v) @@ -136,100 +126,119 @@ do esac done -if [ "$#" -lt 1 ]; then - usage "No application specified" +if [ "$#" -lt 1 ] +then + usage "No application specified" fi # use foamExec for a specified version and for remote (parallel) runs -if [ -n "$version" -o "$PARALLEL" = yes ]; then - APPLICATION=`findExec foamExec` - if [ $? -ne 0 ]; then - usage "'foamExec' not found" - fi - if [ -n "$version" ]; then - APPLICATION="$APPLICATION -v $version" - fi +if [ -n "$version" -o "$parallelOpt" = true ] +then + APPLICATION=`findExec foamExec` + if [ $? -ne 0 ] + then + usage "'foamExec' not found" + fi + if [ -n "$version" ] + then + APPLICATION="$APPLICATION -v $version" + fi else - APPLICATION=`findExec $1` - if [ $? -ne 0 ]; then - usage "Application '$1' executable not found" - fi - echo "Application : $1" - shift + APPLICATION=`findExec $1` + if [ $? -ne 0 ] + then + usage "Application '$1' executable not found" + fi + echo "Application : $1" + shift fi -if [ "$PARALLEL" = no ]; then -# -# RUN ON SINGLE PROCESSOR -# - if [ "$SCREEN" = no ]; then - echo "Executing: $APPLICATION $@ > log 2>&1 &" - $APPLICATION $@ > log 2>&1 & + +if [ "$parallelOpt" = true ] +then + # parallel + # ~~~~~~~~ + + # + # is the case decomposed? + # + if [ -r "processor0" ] + then + NPROCS="`/bin/ls -1d processor* | wc -l`" else - echo "Executing: $APPLICATION $@ | tee log &" - $APPLICATION $@ | tee log & - wait $! + echo "Case is not currently decomposed" + if [ -r system/decomposeParDict ] + then + echo "system/decomposeParDict exists" + echo "Try decomposing with \"foamJob decomposePar\"" + exit 1 + else + echo "Cannot find system/decomposeParDict file required to decompose the case for parallel running." + echo "Please consult the User Guide for details of parallel running" + exit 1 + fi + fi + + # + # locate mpirun + # + mpirun=`findExec mpirun` + if [ $? -ne 0 ] + then + usage "'mpirun' not found" + fi + mpiopts="-np $NPROCS" + + # + # is the machine ready to run parallel? + # + echo "Parallel processing using $WM_MPLIB with $NPROCS processors" + case "$WM_MPLIB" in + OPENMPI) + # add hostfile info + for hostfile in \ + hostfile \ + machines \ + system/hostfile \ + system/machines \ + ; + do + if [ -r $hostfile ] + then + mpiopts="$mpiopts -hostfile $hostfile" + break + fi + done + ;; + esac + + # + # run (in parallel) + # + if [ "$screenOpt" = true ] + then + echo "Executing: mpirun $mpiopts $APPLICATION $@ -parallel | tee log" + $mpirun $mpiopts $APPLICATION $@ -parallel | tee log + else + echo "Executing: mpirun $mpiopts $APPLICATION $@ -parallel > log 2>&1" + $mpirun $mpiopts $APPLICATION $@ -parallel > log 2>&1 & + fi + +else + # + # run (on single processor) + # + if [ "$screenOpt" = true ] + then + echo "Executing: $APPLICATION $@ | tee log &" + $APPLICATION $@ | tee log & + wait $! + else + echo "Executing: $APPLICATION $@ > log 2>&1 &" + $APPLICATION $@ > log 2>&1 & fi else -# -# IS THE CASE DECOMPOSED? -# - if [ -r "processor0" ] ; then - NPROCS="`/bin/ls -1d processor* | wc -l`" - else - echo "Case is not currently decomposed" - if [ -r system/decomposeParDict ] ; then - echo "system/decomposeParDict exists" - echo "Try decomposing with \"foamJob decomposePar\"" - exit 1 - else - echo "Cannot find system/decomposeParDict file required to decompose the case for parallel running." - consultGuide - exit 1 - fi - fi -# -# LOCATE MPIRUN -# - mpirun=`findExec mpirun` - if [ $? -ne 0 ]; then - usage "'mpirun' not found" - fi - mpiopts="-np $NPROCS" - -# -# IS THE MACHINE READY TO RUN PARALLEL? -# - echo "Parallel processing using $WM_MPLIB with $NPROCS processors" - case "$WM_MPLIB" in - OPENMPI) - # add hostfile info - for hostfile in \ - hostfile \ - machines \ - system/hostfile \ - system/machines \ - ; - do - if [ -r $hostfile ]; then - mpiopts="$mpiopts -hostfile $hostfile" - break - fi - done - ;; - esac - -# -# RUN IN PARALLEL -# - if [ "$SCREEN" = no ] ; then - echo "Executing: mpirun $mpiopts $APPLICATION $@ -parallel > log 2>&1" - $mpirun $mpiopts $APPLICATION $@ -parallel > log 2>&1 & - else - echo "Executing: mpirun $mpiopts $APPLICATION $@ -parallel | tee log" - $mpirun $mpiopts $APPLICATION $@ -parallel | tee log - fi fi #------------------------------------------------------------------------------ diff --git a/bin/mpirunDebug b/bin/mpirunDebug index 6869e64e30..1588265866 100755 --- a/bin/mpirunDebug +++ b/bin/mpirunDebug @@ -40,34 +40,39 @@ else fi -printUsage() { - echo "" - echo "Usage: ${0##*/} -np
" - echo "" - echo "This will run like mpirun but with each process in an xterm" +usage() +{ + cat< + +* This will run like mpirun but with each process in an XTerm + +USAGE + exit 1 } -nProcs='' -exec='' -args='' +unset nProcs exec args -while [ "$1" != "" ]; do +while [ "$1" != "" ] +do echo "$1" case $1 in - -np) + -np) nProcs=$2 shift ;; - *) - if [ ! "$exec" ]; then + *) + if [ ! "$exec" ] + then exec=$1 - elif [ ! "$args" ]; then + elif [ ! "$args" ] + then args="\"$1\"" else args="$args \"$1\"" fi ;; - esac shift done @@ -76,27 +81,19 @@ echo "nProcs=$nProcs" echo "exec=$exec" echo "args=$args" -if [ ! "$nProcs" ]; then - printUsage - exit 1 -fi -if [ ! "$args" ]; then - printUsage - exit 1 -fi -if [ ! "$exec" ]; then - printUsage - exit 1 -fi +[ "$nProcs" ] || usage +[ "$args" ] || usage +[ "$exec" ] || usage exec=`which $exec` -if [ ! -x "$exec" ]; then +if [ ! -x "$exec" ] +then echo "Cannot find executable $exec or is not executable" - printUsage - exit 1 + usage fi -if [ ! "$PWD" ]; then +if [ ! "$PWD" ] +then PWD=`pwd` fi @@ -106,43 +103,51 @@ echo "Constructed gdb initialization file $PWD/gdbCommands" $ECHO "Choose running method: 0)normal 1)gdb+xterm 2)gdb 3)log 4)log+xterm 5)xterm+valgrind: \c" read method -if [ "$method" -ne 0 -a "$method" -ne 1 -a "$method" -ne 2 -a "$method" -ne 3 -a "$method" -ne 4 -a "$method" -ne 5 ]; then - printUsage - exit 1 -fi +case "$method" in +0 | 1 | 2 | 3 | 4 | 5 ) + # okay + ;; +*) + usage + ;; +esac $ECHO "Run all processes local or distributed? 1)local 2)remote: \c" read spawn -if [ "$spawn" -ne 1 -a "$spawn" -ne 2 ]; then - printUsage - exit 1 +if [ "$spawn" -ne 1 -a "$spawn" -ne 2 ] +then + usage fi + # check ~/.$WM_PROJECT/$WM_PROJECT_VERSION/ # check ~/.$WM_PROJECT/ # check /etc/ -if [ "$WM_PROJECT" ]; then +if [ "$WM_PROJECT" ] +then - for i in \ - $HOME/.WM_PROJECT/$WM_PROJECT_VERSION \ - $HOME/.WM_PROJECT \ - $WM_PROJECT_DIR/etc \ - ; - do - if [ -f "$i/bashrc" ]; then - sourceFoam="$i/bashrc" - break - fi - done + for i in \ + $HOME/.WM_PROJECT/$WM_PROJECT_VERSION \ + $HOME/.WM_PROJECT \ + $WM_PROJECT_DIR/etc \ + ; + do + if [ -f "$i/bashrc" ] + then + sourceFoam="$i/bashrc" + break + fi + done fi # Construct test string for remote execution. # Source OpenFOAM settings if OpenFOAM environment not set. # attempt to preserve the installation directory 'FOAM_INST_DIR' -if [ "$FOAM_INST_DIR" ]; then - sourceFoam='[ "$WM_PROJECT" ] || '"FOAM_INST_DIR=$FOAM_INST_DIR . $sourceFoam" +if [ "$FOAM_INST_DIR" ] +then + sourceFoam='[ "$WM_PROJECT" ] || '"FOAM_INST_DIR=$FOAM_INST_DIR . $sourceFoam" else - sourceFoam='[ "$WM_PROJECT" ] || '". $sourceFoam" + sourceFoam='[ "$WM_PROJECT" ] || '". $sourceFoam" fi echo "**sourceFoam:$sourceFoam" @@ -160,36 +165,45 @@ do geom="-geometry 120x20+$xpos+$ypos" node="" - if [ .$WM_MPLIB = .OPENMPI ]; then + if [ "$WM_MPLIB" = OPENMPI ] + then node="-np 1 " fi echo "#!/bin/sh" > $procCmdFile - if [ "$method" -eq 0 ]; then + case "$method" in + 0) echo "$sourceFoam; cd $PWD; $exec $args | tee $procLog" >> $procCmdFile echo "${node}$procCmdFile" >> $PWD/mpirun.schema - elif [ "$method" -eq 1 ]; then + ;; + 1) echo "$sourceFoam; cd $PWD; gdb -command $PWD/gdbCommands $exec 2>&1 | tee $procLog; read dummy" >> $procCmdFile #echo "$sourceFoam; cd $PWD; $exec $args; read dummy" >> $procCmdFile echo "${node}xterm -font fixed -title 'processor'$proc $geom -e $procCmdFile" >> $PWD/mpirun.schema - elif [ "$method" -eq 2 ]; then + ;; + 2) echo "$sourceFoam; cd $PWD; gdb -command $PWD/gdbCommands >& $procLog" >> $procCmdFile echo "${node}$procCmdFile" >> $PWD/mpirun.schema - elif [ "$method" -eq 3 ]; then + ;; + 3) echo "$sourceFoam; cd $PWD; $exec $args >& $procLog" >> $procCmdFile echo "${node}$procCmdFile" >> $PWD/mpirun.schema - elif [ "$method" -eq 4 ]; then + ;; + 4) echo "$sourceFoam; cd $PWD; $exec $args 2>&1 | tee $procLog; read dummy" >> $procCmdFile echo "${node}xterm -font fixed -title 'processor'$proc $geom -e $procCmdFile" >> $PWD/mpirun.schema - elif [ "$method" -eq 5 ]; then + ;; + 5) echo "$sourceFoam; cd $PWD; valgrind $exec $args; read dummy" >> $procCmdFile echo "${node}xterm -font fixed -title 'processor'$proc $geom -e $procCmdFile" >> $PWD/mpirun.schema - fi + ;; + esac chmod +x $procCmdFile let column=proc%6 - if [ $proc -ne 0 -a $column -eq 0 ]; then + if [ $proc -ne 0 -a $column -eq 0 ] + then ((xpos+=600)) ((ypos=0)) else @@ -203,10 +217,13 @@ do echo " tail -f $procLog" done -cmd="" -if [ .$WM_MPLIB = .OPENMPI ]; then +unset cmd + +case "$WM_MPLIB" in +OPENMPI) cmd="mpirun -app $PWD/mpirun.schema $procXtermCmdFile echo "$procCmd" >> $procXtermCmdFile chmod +x $procXtermCmdFile - if [ $proc -ne 0 ]; then + if [ $proc -ne 0 ] + then cmd="${cmd} :" fi cmd="${cmd} -n 1 ${procXtermCmdFile}" done < $PWD/mpirun.schema -fi + ;; +esac echo "Constructed $PWD/mpirun.schema file." echo ""