STYLE: linearAxialAngularSpring: indentation
[OpenFOAM-2.0.x.git] / bin / foamCleanPath
blob6bae9ca55e9e4866c3272f3350575bea78ae779a
1 #!/bin/sh
2 #------------------------------------------------------------------------------
3 # ========= |
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 # \\ / O peration |
6 # \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 # \\/ M anipulation |
8 #-------------------------------------------------------------------------------
9 # License
10 # This file is part of OpenFOAM.
12 # OpenFOAM is free software: you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
17 # OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
18 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 # for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 # Script
26 # foamCleanPath
28 # Description
29 # Usage: foamCleanPath [-strip] path [wildcard] .. [wildcard]
31 # Prints its argument (which should be a ':' separated path)
32 # without all
33 # - duplicate elements
34 # - elements whose start matches a wildcard
35 # - inaccessible directories (with the -strip (at your option)
37 # Note
38 # - this routine will fail when directories have embedded spaces
39 # - false matches possible if a wildcard contains '.' (sed regex)
40 # - the wildcards can themselves can be written together and separated
41 # by colons or whitespace
42 #------------------------------------------------------------------------------
43 usage() {
44 cat <<USAGE 1>&2
45 Usage: ${0##*/} [OPTION] path [wildcard1] .. [wildcardN]
46 options:
47 -strip remove inaccessible directories
48 -help print the usage
50 Prints its argument (which should be a ':' separated list) cleansed from
51 - duplicate elements
52 - elements whose start matches one of the wildcard(s)
53 - inaccessible directories (with the -strip (at your option)
55 Exit status
56 0 on success
57 1 for miscellaneous errors.
58 2 initial value of 'path' is empty
60 USAGE
61 exit 1
65 unset strip
66 # parse options
67 while [ "$#" -gt 0 ]
69 case "$1" in
70 -h | -help)
71 usage
73 -strip)
74 strip=true
75 shift
78 break
80 esac
81 done
84 [ "$#" -ge 1 ] || usage
86 dirList="$1"
87 shift
89 [ -n "$1" ] || exit 2 # quick exit on empty 'dirList'
92 ##DEBUG echo "input>$dirList<" 1>&2
94 # preserve current IFS and split on colon or whitespace
95 oldIFS="$IFS"
96 IFS=': '
98 # "wildcard1 ... wildcardN" may have been passed as a single parameter
99 # or may contain ':' separators
100 set -- $*
102 # strip out wildcards via sed
103 while [ "$#" -ge 1 ]
105 wildcard=$1
106 shift
107 ##DEBUG echo "remove>$wildcard<" 1>&2
108 if [ -n "$wildcard" ]
109 then
110 dirList=$(echo "$dirList:" | sed -e "s@${wildcard}[^:]*:@@g")
112 done
114 # split on ':' (and on space as well to avoid any surprises)
115 IFS=': '
116 set -- $dirList
118 ##DEBUG echo "intermediate>$dirList<" 1>&2
120 # rebuild the list from scratch
121 unset dirList
122 for dir
124 ##DEBUG echo "check>$dir<" 1>&2
125 #- dirs must exist
126 if [ -e "$dir" ]
127 then
128 #- no duplicate dirs
129 duplicate=$(echo " $dirList " | sed -ne "s@ $dir @DUP@p")
131 if [ ! "$duplicate" ]
132 then
133 dirList="$dirList $dir"
135 elif [ "$strip" != true ]
136 then
137 # Print non-existing directories if not in 'strip' mode.
138 dirList="$dirList $dir"
140 done
142 # split on whitespace
143 IFS=' '
144 set -- $dirList
146 # rejoin on ':'
147 IFS=':'
148 dirList="$*"
150 # restore IFS
151 IFS="$oldIFS"
153 ##DEBUG echo "output>$dirList<" 1>&2
154 echo "$dirList"
156 # -----------------------------------------------------------------------------