From a356adbfd87c9dbceaa75d1c83768e5bd0fc42e8 Mon Sep 17 00:00:00 2001 From: Chris Greenshields Date: Sun, 21 May 2017 19:58:36 -0400 Subject: [PATCH] Scripts in bin: improved script quality, option listing and exit status on -help --- bin/foamCleanPath | 15 +++-- bin/foamCleanPolyMesh | 24 ++++--- bin/foamCloneCase | 41 ++++++------ bin/foamCreateVideo | 98 ++++++++++++++--------------- bin/foamExec | 22 ++++--- bin/foamJob | 69 +++++++------------- bin/foamLog | 18 +++--- bin/foamMonitor | 135 ++++++++++++++++++++------------------- bin/foamNewBC | 143 +++++++++++++++++++++++------------------- bin/paraFoam | 35 ++++++----- bin/rmcore | 4 +- bin/rm~all | 4 +- 12 files changed, 312 insertions(+), 296 deletions(-) diff --git a/bin/foamCleanPath b/bin/foamCleanPath index cbae8578d..c38245f8c 100755 --- a/bin/foamCleanPath +++ b/bin/foamCleanPath @@ -3,7 +3,7 @@ # ========= | # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox # \\ / O peration | -# \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation +# \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation # \\/ M anipulation | #------------------------------------------------------------------------------- # License @@ -41,7 +41,7 @@ # by colons or whitespace #------------------------------------------------------------------------------ usage() { - cat <&2 + cat <&2 exit 1 } @@ -68,12 +72,15 @@ while [ "$#" -gt 0 ] do case "$1" in -h | -help) - usage + usage && exit 0 ;; -strip) strip=true shift ;; + -*) + error + ;; *) break ;; @@ -81,7 +88,7 @@ do done -[ "$#" -ge 1 ] || usage +[ "$#" -ge 1 ] || error dirList="$1" shift diff --git a/bin/foamCleanPolyMesh b/bin/foamCleanPolyMesh index e4f1eeec9..02e1df1ba 100755 --- a/bin/foamCleanPolyMesh +++ b/bin/foamCleanPolyMesh @@ -3,7 +3,7 @@ # ========= | # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox # \\ / O peration | -# \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation +# \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation # \\/ M anipulation | #------------------------------------------------------------------------------- # License @@ -31,8 +31,6 @@ # #------------------------------------------------------------------------------ usage() { - exec 1>&2 - while [ "$#" -ge 1 ]; do echo "$1"; shift; done cat <&2 + while [ "$#" -ge 1 ]; do echo "$1"; shift; done + usage + exit 1 } unset caseDir regionName @@ -55,21 +59,21 @@ while [ "$#" -gt 0 ] do case "$1" in -h | -help) - usage + usage && exit 0 ;; -case) - [ "$#" -ge 2 ] || usage "'$1' option requires an argument" - cd "$2" 2>/dev/null || usage "directory does not exist: '$2'" + [ "$#" -ge 2 ] || error "'$1' option requires an argument" + cd "$2" 2>/dev/null || error "directory does not exist: '$2'" caseDir=$2 shift 2 ;; -region) - [ "$#" -ge 2 ] || usage "'$1' option requires an argument" + [ "$#" -ge 2 ] || error "'$1' option requires an argument" regionName=$2 shift 2 ;; *) - usage "unknown option/argument: '$*'" + error "unknown option/argument: '$*'" ;; esac done @@ -133,7 +137,7 @@ for i in \ surfaceIndex \ ; do - rm -rf $meshDir/$i $meshDir/$i.gz + rm -rf ${meshDir:?}/$i ${meshDir:?}/$i.gz done #------------------------------------------------------------------------------ diff --git a/bin/foamCloneCase b/bin/foamCloneCase index d037cfa96..5ce4e9f0c 100755 --- a/bin/foamCloneCase +++ b/bin/foamCloneCase @@ -3,7 +3,7 @@ # ========= | # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox # \\ / O peration | -# \\ / A nd | Copyright (C) 2015 OpenFOAM Foundation +# \\ / A nd | Copyright (C) 2015-2017 OpenFOAM Foundation # \\/ M anipulation | #------------------------------------------------------------------------------- # License @@ -33,38 +33,42 @@ # #------------------------------------------------------------------------------ usage() { - exec 1>&2 - while [ "$#" -ge 1 ]; do echo "$1"; shift; done cat< +Usage: ${0##*/} [OPTION] options: - -l | -latestTime clone the latest time directory - -h | -help print the usage + -latestTime clone the latest time directory + -help print the usage Create a new case directory that includes time, system and constant directories of directory. The time directory is the first time directory by default. USAGE +} + +error () { + exec 1>&2 + while [ "$#" -ge 1 ]; do echo "$1"; shift; done + usage exit 1 } -TIME_OPTION="head -1" +time_option="head -1" # parse options while [ "$#" -gt 0 ] do case "$1" in -h | -help) - usage + usage && exit 0 ;; -l | -latestTime) - TIME_OPTION="tail -1" + time_option="tail -1" shift 1 ;; -*) - usage "unknown option: '$*'" + error "unknown option: '$*'" ;; *) break @@ -72,19 +76,18 @@ do esac done -[ $# -eq 2 ] || usage "Incorrect arguments specified" -if [ "$(foamListTimes -case $1 2>&1 >/dev/null | grep 'FOAM FATAL ERROR')" ] -then - usage "$1 is not does not a valid case directory" -fi -! [ -e $2 ] || usage "$2 file/directory already exists, delete and re-run" +[ $# -eq 2 ] || error "Incorrect arguments specified" +foamListTimes -case "$1" >/dev/null 2>&1 || \ + error "'$1' is not a valid case directory" + +! [ -e "$2" ] || error "'$2' file/directory already exists, delete and re-run" echo "Making $2 case directory" -mkdir $2 +mkdir "$2" -TIME_DIR="$(foamListTimes -withZero -case $1 | $TIME_OPTION)" +time_dir="$(foamListTimes -withZero -case "$1" | $time_option)" echo "Copying case directories from $1 to $2" -cp -r $1/system $1/constant $1/${TIME_DIR} $2 +cp -r "$1"/system "$1"/constant "$1"/"${time_dir}" "$2" #------------------------------------------------------------------------------ diff --git a/bin/foamCreateVideo b/bin/foamCreateVideo index d86f22576..439f0d15f 100755 --- a/bin/foamCreateVideo +++ b/bin/foamCreateVideo @@ -3,7 +3,7 @@ # ========= | # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox # \\ / O peration | -# \\ / A nd | Copyright (C) 2015-2016 OpenFOAM Foundation +# \\ / A nd | Copyright (C) 2015-2017 OpenFOAM Foundation # \\/ M anipulation | #------------------------------------------------------------------------------- # License @@ -32,19 +32,17 @@ #------------------------------------------------------------------------------ usage () { - exec 1>&2 - while [ "$#" -ge 1 ]; do echo "$1"; shift; done cat < directory containing png images (default local dir) - -f | -fps frames per second (default = 10) - -h | -help help - -i | -image name of image sequence (default = image) - -o | -out name of output video file (default = video) - -s | -start specify the start frame number for avconv - -w | -webm WebM output video file format + -dir | -d directory containing png images (default local dir) + -fps | -f frames per second (default = 10) + -help | -h help + -image | -i name of image sequence (default = image) + -out | -o name of output video file (default = video) + -start | -s specify the start frame number for avconv + -webm | -w WebM output video file format Creates a video file from a sequence of PNG images - A sequence named "image" will expect files image.0000.png, image.0001.png, etc @@ -55,56 +53,55 @@ Creates a video file from a sequence of PNG images Requires avconv or mencoder for MPEG-4 output, avconv for WebM output USAGE +} + +error () { + exec 1>&2 + while [ "$#" -ge 1 ]; do echo "$1"; shift; done + usage exit 1 } - # Default settings -DIR='.' -IMAGE='image' -VIDEO='video' -FPS=10 -FMT='mp4' -START_NUMBER='' +dir=. +image=image +video=video +fps=10 +fmt=mp4 while [ "$#" -gt 0 ] do case "$1" in -h | -help) - usage + usage && exit 0 ;; - -d | -directory) - [ "$#" -ge 2 ] || usage "'$1' option requires an argument" - DIR=$2 + -d | -dir) + [ "$#" -ge 2 ] || error "'$1' option requires an argument" + dir=$2 shift 2 ;; -f | -fps) - [ "$#" -ge 2 ] || usage "'$1' option requires an argument" - FPS=$2 + [ "$#" -ge 2 ] || error "'$1' option requires an argument" + fps=$2 shift 2 ;; -i | -image) - [ "$#" -ge 2 ] || usage "'$1' option requires an argument" - IMAGE=$2 + [ "$#" -ge 2 ] || error "'$1' option requires an argument" + image=$2 shift 2 ;; -o | -out) - [ "$#" -ge 2 ] || usage "'$1' option requires an argument" - VIDEO=$2 - shift 2 - ;; - -s | -start) - [ "$#" -ge 2 ] || usage "'$1' option requires an argument" - START_NUMBER="-start_number $2" + [ "$#" -ge 2 ] || error "'$1' option requires an argument" + video=$2 shift 2 ;; -w | -webm) - FMT=webm - echo "Selected $FMT format, requires avconv..." + fmt=webm + echo "Selected $fmt format, requires avconv..." shift ;; -*) - usage "invalid option '$1'" + error "invalid option '$1'" ;; *) break @@ -116,37 +113,36 @@ done # MAIN # -[ -f "$(ls -1 $DIR/$IMAGE.*.png | head -1)" ] || usage "Cannot find first file in image sequence" +[ -f "$(find "$dir" -name "$image.*.png" | sort | head -1)" ] || \ + error "Cannot find first file in image sequence" -if [ "$FMT" = "webm" ] ; then +if [ "$fmt" = "webm" ] ; then if command -v avconv >/dev/null 2>&1 ; then echo "Creating image with avconv..." avconv \ - -framerate $FPS \ - $START_NUMBER \ - -i ${DIR}/${IMAGE}.%04d.png \ + -framerate "$fps" \ + -i "${dir}/${image}.%04d.png" \ -c:v libvpx -crf 15 -b:v 1M \ - $VIDEO.$FMT + "$video.$fmt" else - usage "Please install avconv" + error "Please install avconv" fi else - if command -v avconv >/dev/null 2>&1 ; then + if ! command -v avconv >/dev/null 2>&1 ; then echo "Creating image with avconv..." avconv \ - -framerate $FPS \ - $START_NUMBER \ - -i ${DIR}/${IMAGE}.%04d.png \ + -framerate "$fps" \ + -i "${dir}/${image}.%04d.png" \ -c:v libx264 -pix_fmt yuv420p \ - $VIDEO.$FMT + "$video.$fmt" elif command -v mencoder >/dev/null 2>&1 ; then echo "Creating image with mencoder..." mencoder \ - "mf://$DIR/$IMAGE.*.png" \ - -mf fps=$FPS \ - -o $VIDEO.$FMT \ + "mf://$dir/$image.*.png" \ + -mf fps="$fps" \ + -o "$video.$fmt" \ -ovc x264 else - usage "Please install avconv or mencoder" + error "Please install avconv or mencoder" fi fi diff --git a/bin/foamExec b/bin/foamExec index cb6fec4bf..b03c678bd 100755 --- a/bin/foamExec +++ b/bin/foamExec @@ -3,7 +3,7 @@ # ========= | # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox # \\ / O peration | -# \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation +# \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation # \\/ M anipulation | #------------------------------------------------------------------------------- # License @@ -48,8 +48,6 @@ # #------------------------------------------------------------------------------ usage() { - exec 1>&2 - while [ "$#" -ge 1 ]; do echo "$1"; shift; done cat< ... @@ -64,6 +62,12 @@ options: * run a particular OpenFOAM version of USAGE +} + +error () { + exec 1>&2 + while [ "$#" -ge 1 ]; do echo "$1"; shift; done + usage exit 1 } @@ -88,21 +92,21 @@ while [ "$#" -gt 0 ] do case "$1" in -h | -help) - usage + usage && exit 0 ;; -m | -mode) - [ "$#" -ge 2 ] || usage "'$1' option requires an argument" + [ "$#" -ge 2 ] || error "'$1' option requires an argument" etcOpts="$etcOpts $1 $2" # pass-thru to foamEtcFile shift ;; -p | -prefix) - [ "$#" -ge 2 ] || usage "'$1' option requires an argument" + [ "$#" -ge 2 ] || error "'$1' option requires an argument" etcOpts="$etcOpts $1 $2" # pass-thru to foamEtcFile prefixDir="$2" shift ;; -v | -version) - [ "$#" -ge 2 ] || usage "'$1' option requires an argument" + [ "$#" -ge 2 ] || error "'$1' option requires an argument" etcOpts="$etcOpts $1 $2" # pass-thru to foamEtcFile version="$2" shift @@ -112,7 +116,7 @@ do break ;; -*) - usage "invalid option '$1'" + error "invalid option '$1'" ;; *) break @@ -139,7 +143,7 @@ sourceRc() } -[ "$#" -ge 1 ] || usage "no application specified" +[ "$#" -ge 1 ] || error "no application specified" sourceRc exec "$@" diff --git a/bin/foamJob b/bin/foamJob index 6646e5dd8..5c1c6e04e 100755 --- a/bin/foamJob +++ b/bin/foamJob @@ -3,7 +3,7 @@ # ========= | # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox # \\ / O peration | -# \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation +# \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation # \\/ M anipulation | #------------------------------------------------------------------------------- # License @@ -31,8 +31,6 @@ # #------------------------------------------------------------------------------ usage() { - exec 1>&2 - while [ "$#" -ge 1 ]; do echo "$1"; shift; done cat< ... @@ -49,6 +47,12 @@ options: Redirects the output to 'log' in the case directory USAGE +} + +error () { + exec 1>&2 + while [ "$#" -ge 1 ]; do echo "$1"; shift; done + usage exit 1 } @@ -58,14 +62,14 @@ echoArgs() { for stringItem in "$@"; do - echo -n "${addSpace}" + printf "%s" "${addSpace}" if [ "${stringItem##* }" = "$stringItem" ] then - echo -n "$stringItem" + printf "%s" "$stringItem" addSpace=" " else - echo -n "'$stringItem'" + printf "%s" "'$stringItem'" addSpace=" " fi @@ -76,38 +80,11 @@ echoArgs() { unset version -# Replacement for possibly buggy 'which' findExec() { - 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 + which "$1" 2>/dev/null + command -v "$1" >/dev/null 2>&1 } - - # Main script #~~~~~~~~~~~~ unset parallelOpt screenOpt waitOpt @@ -118,11 +95,11 @@ while [ "$#" -gt 0 ] do case "$1" in -h | -help) - usage + usage && exit 0 ;; -case) - [ "$#" -ge 2 ] || usage "'$1' option requires an argument" - cd "$2" 2>/dev/null || usage "directory does not exist: '$2'" + [ "$#" -ge 2 ] || error "'$1' option requires an argument" + cd "$2" 2>/dev/null || error "directory does not exist: '$2'" shift 2 ;; -p | -parallel) @@ -142,7 +119,7 @@ do shift ;; -v | -version) - [ "$#" -ge 2 ] || usage "'$1' option requires an argument" + [ "$#" -ge 2 ] || error "'$1' option requires an argument" version="$2" shift 2 ;; @@ -151,7 +128,7 @@ do break ;; -*) - usage "invalid option '$1'" + error "invalid option '$1'" ;; *) break @@ -159,7 +136,7 @@ do esac done -[ "$#" -ge 1 ] || usage "No application specified" +[ "$#" -ge 1 ] || error "No application specified" # Use foamExec for a specified version @@ -169,11 +146,11 @@ then # When possible, determine if application even exists if [ -z "$version" ] then - findExec $1 >/dev/null || usage "Application '$1' not found" + findExec "$1" >/dev/null || error "Application '$1' not found" fi # Use foamExec for dispatching - APPLICATION=`findExec foamExec` || usage "'foamExec' not found" + APPLICATION=$(findExec foamExec) || error "'foamExec' not found" [ -n "$version" ] && APPLICATION="$APPLICATION -version $version" @@ -184,7 +161,7 @@ then fi else - APPLICATION=`findExec $1` || usage "Application '$1' not found" + APPLICATION=$(findExec "$1") || error "Application '$1' not found" echo "Application : $1" shift fi @@ -200,7 +177,7 @@ then # if [ -r "processor0" ] then - NPROCS="`/bin/ls -1d processor* | wc -l`" + NPROCS="$(/bin/ls -1d processor* | wc -l)" else echo "Case is not currently decomposed" if [ -r system/decomposeParDict ] @@ -218,7 +195,7 @@ then # # Find mpirun # - mpirun=`findExec mpirun` || usage "'mpirun' not found" + mpirun=$(findExec mpirun) || error "'mpirun' not found" mpiopts="-np $NPROCS" # diff --git a/bin/foamLog b/bin/foamLog index 8b7d6587e..ac79761d0 100755 --- a/bin/foamLog +++ b/bin/foamLog @@ -3,7 +3,7 @@ # ========= | # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox # \\ / O peration | -# \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation +# \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation # \\/ M anipulation | #------------------------------------------------------------------------------- # License @@ -35,11 +35,10 @@ siteDir=${WM_PROJECT_SITE:-${WM_PROJECT_INST_DIR:-}/site} userDir=$HOME/.OpenFOAM usage() { - exec 1>&2 - while [ "$#" -ge 1 ]; do echo "$1"; shift; done cat < +Usage: $Script [OPTIONS] +options: -list lists but does not extract -n create single column files with the extracted data only -quiet quiet operation @@ -49,13 +48,13 @@ Usage: $Script [OPTIONS] $Script - extracts xy files from OpenFOAM logs. USAGE - exit 1 } #------------------------------------------------------------------------------ printHelp() { -cat <&2 + while [ "$#" -ge 1 ]; do echo "$1"; shift; done + usage + exit 1 } diff --git a/bin/foamMonitor b/bin/foamMonitor index 8ae19858b..fda9121b9 100755 --- a/bin/foamMonitor +++ b/bin/foamMonitor @@ -3,7 +3,7 @@ # ========= | # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox # \\ / O peration | -# \\ / A nd | Copyright (C) 2015 OpenFOAM Foundation +# \\ / A nd | Copyright (C) 2015-2017 OpenFOAM Foundation # \\/ M anipulation | #------------------------------------------------------------------------------- # License @@ -32,17 +32,15 @@ # #------------------------------------------------------------------------------ usage() { - exec 1>&2 - while [ "$#" -ge 1 ]; do echo "$1"; shift; done cat< options: - -h | -help print the usage - -i | -idle