Newer
Older
#!/bin/sh
#------------------------------------------------------------------------------
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
# \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
#-------------------------------------------------------------------------------
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
# Script
# foamCleanPath
#
# Description
# Usage: foamCleanPath [-strip] path [wildcard] .. [wildcard]
#
# Prints its argument (which should be a ':' separated path)
# without the following:
# - 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)
# - the wildcards themselves can be written together and separated
#------------------------------------------------------------------------------
usage() {
cat <<USAGE 1>&2
Usage: ${0##*/} [OPTION] path [wildcard1] .. [wildcardN]
options:
-debug print debug information to stderr
-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)
Exit status
0 on success
1 for miscellaneous errors.
2 initial value of 'path' is empty
# Parse options
unset optDebug optStrip
while [ "$#" -gt 0 ]
do
case "$1" in
-debug)
optDebug=true
;;
optStrip=true
;;
*)
break
;;
esac
# Basic checks, setup
dirList="$1"
[ -n "$dirList" ] || exit 2 # Quick exit on empty 'dirList'
#-------------------------------------------------------------------------------
# Debugging (optional)
if [ -n "$optDebug" ]
then
printDebug() { while [ "$#" -ge 1 ]; do echo "$1" 1>&2; shift; done; }
else
printDebug() { true; } # No-op
fi
# Check directory existence (optional)
if [ -n "$optStrip" ]
then
isDir() { test -d "$1"; } # Check for directory
else
isDir() { true; } # No check (always true)
fi
# The "wildcard1 ... wildcardN" may have been passed as a single parameter
# or may contain ':' separators
oldIFS="$IFS" # Preserve initial IFS
IFS=': ' # Split on colon, whitespace
printDebug "input>$dirList<"
# Strip out wildcards via sed. Path and wildcard cannot contain '?'.
while [ "$#" -ge 1 ]
wildcard="$1"
shift
if [ -n "$wildcard" ]
then
printDebug "remove>$wildcard<"
dirList=$(echo "$dirList:" | sed -e "s?${wildcard}[^:]*:??g")
printDebug "intermediate>$dirList<"
IFS=': ' # Split on colon, whitespace (to avoid surprises)
set -- $dirList
IFS="$oldIFS" # Restore initial IFS
# Rebuild the list
unset dirList
for dir
do
printDebug "check>$dir< in $dirList"
if isDir "$dir"
then
# Detect duplicates (ie, dir already in the list)
duplicate=$(echo ":$dirList:" | sed -ne '\?:'"$dir"':?p')
if [ -n "$duplicate" ]
then
printDebug "duplicate>$dir<"
else
dirList="${dirList}${dirList:+:}$dir"
fi
fi
printDebug "output>$dirList<"
echo "$dirList"
#------------------------------------------------------------------------------