Scripts in bin: improved script quality, option listing and
exit status on -help
This commit is contained in:
69
bin/foamJob
69
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<<USAGE
|
||||
|
||||
Usage: ${0##*/} [OPTION] <application> ...
|
||||
@ -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"
|
||||
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user