- the path is accesible either way, but use setenv for consistency with other environment variables. It also simplifies foamCleanPath again.
144 lines
3.6 KiB
Bash
Executable File
144 lines
3.6 KiB
Bash
Executable File
#!/bin/sh
|
|
#------------------------------------------------------------------------------
|
|
# ========= |
|
|
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
|
# \\ / O peration |
|
|
# \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
|
|
# \\/ M anipulation |
|
|
#-------------------------------------------------------------------------------
|
|
# License
|
|
# This file is part of OpenFOAM.
|
|
#
|
|
# OpenFOAM is free software; you can redistribute it and/or modify it
|
|
# under the terms of the GNU General Public License as published by the
|
|
# Free Software Foundation; either version 2 of the License, or (at your
|
|
# option) any later version.
|
|
#
|
|
# OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
# for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with OpenFOAM; if not, write to the Free Software Foundation,
|
|
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
#
|
|
# Script
|
|
# foamCleanPath
|
|
#
|
|
# Description
|
|
# Usage: foamCleanPath [-strip] path [wildcard] .. [wildcard]
|
|
#
|
|
# Prints its argument (which should be a ':' separated path)
|
|
# without all
|
|
# - duplicate elements
|
|
# - elements whose start matches a wildcard
|
|
# - inaccessible directories (with the -strip option)
|
|
#
|
|
# Note:
|
|
# - this routine will fail when directories have embedded spaces
|
|
# - false matches possible if a wildcard contains '.' (sed regex)
|
|
#------------------------------------------------------------------------------
|
|
usage() {
|
|
cat <<USAGE 1>&2
|
|
Usage: ${0##*/} [OPTION] path [wildcard1] .. [wildcardN]
|
|
options:
|
|
-strip remove inaccessible directories
|
|
-help print the usage
|
|
|
|
Prints its argument (which should be a ':' separated list) cleansed from
|
|
- duplicate elements
|
|
- elements whose start matches one of the wildcard(s)
|
|
- inaccessible directories (with the -strip option)
|
|
|
|
USAGE
|
|
exit 1
|
|
}
|
|
|
|
|
|
unset strip
|
|
# parse options
|
|
while [ "$#" -gt 0 ]
|
|
do
|
|
case "$1" in
|
|
-h | -help)
|
|
usage
|
|
;;
|
|
-strip)
|
|
strip=true
|
|
shift
|
|
;;
|
|
*)
|
|
break
|
|
;;
|
|
esac
|
|
done
|
|
|
|
|
|
[ "$#" -ge 1 ] || usage
|
|
|
|
dirList="$1"
|
|
shift
|
|
|
|
##DEBUG echo "input>$dirList<" 1>&2
|
|
|
|
# preserve current IFS and split on whitespace
|
|
oldIFS="$IFS"
|
|
IFS=' '
|
|
|
|
# "wildcard1 ... wildcardN" may have been passed as a single parameter
|
|
set -- $*
|
|
|
|
# strip out wildcards via sed
|
|
while [ "$#" -ge 1 ]
|
|
do
|
|
wildcard=$1
|
|
shift
|
|
##DEBUG echo "remove>$wildcard<" 1>&2
|
|
dirList=$(echo "$dirList" | sed -e "s@${wildcard}[^:]*:@@g")
|
|
done
|
|
|
|
# split on ':' (and on space as well to avoid any surprises)
|
|
IFS=': '
|
|
set -- $dirList
|
|
|
|
##DEBUG echo "intermediate>$dirList<" 1>&2
|
|
|
|
# rebuild the list from scratch
|
|
unset dirList
|
|
for dir
|
|
do
|
|
##DEBUG echo "check>$dir<" 1>&2
|
|
#- dirs must exist
|
|
if [ -e "$dir" ]
|
|
then
|
|
#- no duplicate dirs
|
|
duplicate=$(echo " $dirList " | sed -ne "s@ $dir @DUP@p")
|
|
|
|
if [ ! "$duplicate" ]
|
|
then
|
|
dirList="$dirList $dir"
|
|
fi
|
|
elif [ "$strip" != true ]
|
|
then
|
|
# Print non-existing directories if not in 'strip' mode.
|
|
dirList="$dirList $dir"
|
|
fi
|
|
done
|
|
|
|
# split on whitespace
|
|
IFS=' '
|
|
set -- $dirList
|
|
|
|
# rejoin on ':'
|
|
IFS=':'
|
|
dirList="$*"
|
|
|
|
# restore IFS
|
|
IFS="$oldIFS"
|
|
|
|
##DEBUG echo "output>$dirList<" 1>&2
|
|
echo "$dirList"
|
|
|
|
# -----------------------------------------------------------------------------
|