Debian Codegraph @

HTML/pathmanagement.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Parsing and Managing Pathnames</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
9 "><LINK
10 REL="HOME"
11 TITLE="Advanced Bash-Scripting Guide"
12 HREF="index.html"><LINK
13 REL="PREVIOUS"
14 TITLE="Awk"
15 HREF="awk.html"><LINK
16 REL="NEXT"
17 TITLE="Exit Codes With Special Meanings"
18 HREF="exitcodes.html"><META
19 HTTP-EQUIV="Content-Style-Type"
20 CONTENT="text/css"><LINK
21 REL="stylesheet"
22 HREF="common/kde-common.css"
23 TYPE="text/css"><META
24 HTTP-EQUIV="Content-Type"
25 CONTENT="text/html; charset=iso-8859-1"><META
26 HTTP-EQUIV="Content-Language"
27 CONTENT="en"><LINK
28 REL="stylesheet"
29 HREF="common/kde-localised.css"
30 TYPE="text/css"
31 TITLE="KDE-English"><LINK
32 REL="stylesheet"
33 HREF="common/kde-default.css"
34 TYPE="text/css"
35 TITLE="KDE-Default"></HEAD
36 ><BODY
37 CLASS="APPENDIX"
38 BGCOLOR="#FFFFFF"
39 TEXT="#000000"
40 LINK="#AA0000"
41 VLINK="#AA0055"
42 ALINK="#AA0000"
43 STYLE="font-family: sans-serif;"
44 ><DIV
45 CLASS="NAVHEADER"
46 ><TABLE
47 SUMMARY="Header navigation table"
48 WIDTH="100%"
49 BORDER="0"
50 CELLPADDING="0"
51 CELLSPACING="0"
52 ><TR
53 ><TH
54 COLSPAN="3"
55 ALIGN="center"
56 >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
57 ></TR
58 ><TR
59 ><TD
60 WIDTH="10%"
61 ALIGN="left"
62 VALIGN="bottom"
63 ><A
64 HREF="awk.html"
65 ACCESSKEY="P"
66 >Prev</A
67 ></TD
68 ><TD
69 WIDTH="80%"
70 ALIGN="center"
71 VALIGN="bottom"
72 ></TD
73 ><TD
74 WIDTH="10%"
75 ALIGN="right"
76 VALIGN="bottom"
77 ><A
78 HREF="exitcodes.html"
79 ACCESSKEY="N"
80 >Next</A
81 ></TD
82 ></TR
83 ></TABLE
84 ><HR
85 ALIGN="LEFT"
86 WIDTH="100%"></DIV
87 ><DIV
88 CLASS="APPENDIX"
89 ><H1
90 ><A
91 NAME="PATHMANAGEMENT"
92 ></A
93 >Appendix D. Parsing and Managing Pathnames</H1
94 ><P
95 >Emmanual Rouat contributed the following example of parsing
96 and transforming <I
97 CLASS="FIRSTTERM"
98 >filenames</I
99 > and, in
100 particular, <A
101 HREF="special-chars.html#PATHNAMEREF"
102 >pathnames</A
103 >. It draws
104 heavily on the functionality of <I
105 CLASS="FIRSTTERM"
106 >sed</I
107 >.</P
108 ><P
109 ><TABLE
110 BORDER="0"
111 BGCOLOR="#E0E0E0"
112 WIDTH="100%"
113 ><TR
114 ><TD
115 ><PRE
116 CLASS="PROGRAMLISTING"
117 > 1&nbsp;#!/usr/bin/env bash
118 2&nbsp;#-----------------------------------------------------------
119 3&nbsp;# Management of PATH, LD_LIBRARY_PATH, MANPATH variables...
120 4&nbsp;# By Emmanuel Rouat &#60;no-email&#62;
121 5&nbsp;# (Inspired by the bash documentation 'pathfuncs' and on
122 6&nbsp;# discussions found on stackoverflow:
123 7&nbsp;# http://stackoverflow.com/questions/370047/
124 8&nbsp;# http://stackoverflow.com/questions/273909/#346860 )
125 9&nbsp;# Last modified: Sat Sep 22 12:01:55 CEST 2012
126 10&nbsp;#
127 11&nbsp;# The following functions handle spaces correctly.
128 12&nbsp;# These functions belong in .bash_profile rather than in
129 13&nbsp;# .bashrc, I guess.
130 14&nbsp;#
131 15&nbsp;# The modular aspect of these functions should make it easy
132 16&nbsp;# to expand them to handle path substitutions instead
133 17&nbsp;# of path removal etc....
134 18&nbsp;#
135 19&nbsp;# See http://www.catonmat.net/blog/awk-one-liners-explained-part-two/
136 20&nbsp;# (item 43) for an explanation of the 'duplicate-entries' removal
137 21&nbsp;# (it's a nice trick!)
138 22&nbsp;#-----------------------------------------------------------
139 23&nbsp;
140 24&nbsp;# Show $@ (usually PATH) as list.
141 25&nbsp;function p_show() { local p="$@" &#38;&#38; for p; do [[ ${!p} ]] &#38;&#38;
142 26&nbsp;echo -e ${!p//:/\\n}; done }
143 27&nbsp;
144 28&nbsp;# Filter out empty lines, multiple/trailing slashes, and duplicate entries.
145 29&nbsp;function p_filter()
146 30&nbsp;{ awk '/^[ \t]*$/ {next} {sub(/\/+$/, "");gsub(/\/+/, "/")}!x[$0]++' ;}
147 31&nbsp;
148 32&nbsp;# Rebuild list of items into ':' separated word (PATH-like).
149 33&nbsp;function p_build() { paste -sd: ;}
150 34&nbsp;
151 35&nbsp;# Clean $1 (typically PATH) and rebuild it
152 36&nbsp;function p_clean()
153 37&nbsp;{ local p=${1} &#38;&#38; eval ${p}='$(p_show ${p} | p_filter | p_build)' ;}
154 38&nbsp;
155 39&nbsp;# Remove $1 from $2 (found on stackoverflow, with modifications).
156 40&nbsp;function p_rm()
157 41&nbsp;{ local d=$(echo $1 | p_filter) p=${2} &#38;&#38;
158 42&nbsp; eval ${p}='$(p_show ${p} | p_filter | grep -xv "${d}" | p_build)' ;}
159 43&nbsp;
160 44&nbsp;# Same as previous, but filters on a pattern (dangerous...
161 45&nbsp;#+ don't use 'bin' or '/' as pattern!).
162 46&nbsp;function p_rmpat()
163 47&nbsp;{ local d=$(echo $1 | p_filter) p=${2} &#38;&#38; eval ${p}='$(p_show ${p} |
164 48&nbsp; p_filter | grep -v "${d}" | p_build)' ;}
165 49&nbsp;
166 50&nbsp;# Delete $1 from $2 and append it cleanly.
167 51&nbsp;function p_append()
168 52&nbsp;{ local d=$(echo $1 | p_filter) p=${2} &#38;&#38; p_rm "${d}" ${p} &#38;&#38;
169 53&nbsp; eval ${p}='$(p_show ${p} d | p_build)' ;}
170 54&nbsp;
171 55&nbsp;# Delete $1 from $2 and prepend it cleanly.
172 56&nbsp;function p_prepend()
173 57&nbsp;{ local d=$(echo $1 | p_filter) p=${2} &#38;&#38; p_rm "${d}" ${p} &#38;&#38;
174 58&nbsp; eval ${p}='$(p_show d ${p} | p_build)' ;}
175 59&nbsp;
176 60&nbsp;# Some tests:
177 61&nbsp;echo
178 62&nbsp;MYPATH="/bin:/usr/bin/:/bin://bin/"
179 63&nbsp;p_append "/project//my project/bin" MYPATH
180 64&nbsp;echo "Append '/project//my project/bin' to '/bin:/usr/bin/:/bin://bin/'"
181 65&nbsp;echo "(result should be: /bin:/usr/bin:/project/my project/bin)"
182 66&nbsp;echo $MYPATH
183 67&nbsp;
184 68&nbsp;echo
185 69&nbsp;MYOTHERPATH="/bin:/usr/bin/:/bin:/project//my project/bin"
186 70&nbsp;p_prepend "/project//my project/bin" MYOTHERPATH
187 71&nbsp;echo "Prepend '/project//my project/bin' \
188 72&nbsp;to '/bin:/usr/bin/:/bin:/project//my project/bin/'"
189 73&nbsp;echo "(result should be: /project/my project/bin:/bin:/usr/bin)"
190 74&nbsp;echo $MYOTHERPATH
191 75&nbsp;
192 76&nbsp;echo
193 77&nbsp;p_prepend "/project//my project/bin" FOOPATH # FOOPATH doesn't exist.
194 78&nbsp;echo "Prepend '/project//my project/bin' to an unset variable"
195 79&nbsp;echo "(result should be: /project/my project/bin)"
196 80&nbsp;echo $FOOPATH
197 81&nbsp;
198 82&nbsp;echo
199 83&nbsp;BARPATH="/a:/b/://b c://a:/my local pub"
200 84&nbsp;p_clean BARPATH
201 85&nbsp;echo "Clean BARPATH='/a:/b/://b c://a:/my local pub'"
202 86&nbsp;echo "(result should be: /a:/b:/b c:/my local pub)"
203 87&nbsp;echo $BARPATH</PRE
204 ></TD
205 ></TR
206 ></TABLE
207 ></P
208 ><P
209 >***</P
210 ><P
211 >David Wheeler kindly permitted me to use his instructive
212 examples.</P
213 ><P
214 ><TABLE
215 BORDER="0"
216 BGCOLOR="#E0E0E0"
217 WIDTH="100%"
218 ><TR
219 ><TD
220 ><PRE
221 CLASS="PROGRAMLISTING"
222 > 1&nbsp;Doing it correctly: A quick summary
223 2&nbsp;by David Wheeler
224 3&nbsp;http://www.dwheeler.com/essays/filenames-in-shell.html
225 4&nbsp;
226 5&nbsp;So, how can you process filenames correctly in shell? Here's a quick
227 6&nbsp;summary about how to do it correctly, for the impatient who "just want the
228 7&nbsp;answer". In short: Double-quote to use "$variable" instead of $variable,
229 8&nbsp;set IFS to just newline and tab, prefix all globs/filenames so they cannot
230 9&nbsp;begin with "-" when expanded, and use one of a few templates that work
231 10&nbsp;correctly. Here are some of those templates that work correctly:
232 11&nbsp;
233 12&nbsp;
234 13&nbsp; IFS="$(printf '\n\t')"
235 14&nbsp; # Remove SPACE, so filenames with spaces work well.
236 15&nbsp;
237 16&nbsp; # Correct glob use:
238 17&nbsp; #+ always use "for" loop, prefix glob, check for existence:
239 18&nbsp; for file in ./* ; do # Use "./*" ... NEVER bare "*" ...
240 19&nbsp; if [ -e "$file" ] ; then # Make sure it isn't an empty match.
241 20&nbsp; COMMAND ... "$file" ...
242 21&nbsp; fi
243 22&nbsp; done
244 23&nbsp;
245 24&nbsp;
246 25&nbsp;
247 26&nbsp; # Correct glob use, but requires nonstandard bash extension.
248 27&nbsp; shopt -s nullglob # Bash extension,
249 28&nbsp; #+ so that empty glob matches will work.
250 29&nbsp; for file in ./* ; do # Use "./*", NEVER bare "*"
251 30&nbsp; COMMAND ... "$file" ...
252 31&nbsp; done
253 32&nbsp;
254 33&nbsp;
255 34&nbsp;
256 35&nbsp; # These handle all filenames correctly;
257 36&nbsp; #+ can be unwieldy if COMMAND is large:
258 37&nbsp; find ... -exec COMMAND... {} \;
259 38&nbsp; find ... -exec COMMAND... {} \+ # If multiple files are okay for COMMAND.
260 39&nbsp;
261 40&nbsp;
262 41&nbsp;
263 42&nbsp; # This skips filenames with control characters
264 43&nbsp; #+ (including tab and newline).
265 44&nbsp; IFS="$(printf '\n\t')"
266 45&nbsp; controlchars="$(printf '*[\001-\037\177]*')"
267 46&nbsp; for file in $(find . ! -name "$controlchars"') ; do
268 47&nbsp; COMMAND "$file" ...
269 48&nbsp; done
270 49&nbsp;
271 50&nbsp;
272 51&nbsp;
273 52&nbsp; # Okay if filenames can't contain tabs or newlines --
274 53&nbsp; #+ beware the assumption.
275 54&nbsp; IFS="$(printf '\n\t')"
276 55&nbsp; for file in $(find .) ; do
277 56&nbsp; COMMAND "$file" ...
278 57&nbsp; done
279 58&nbsp;
280 59&nbsp;
281 60&nbsp;
282 61&nbsp; # Requires nonstandard but common extensions in find and xargs:
283 62&nbsp; find . -print0 | xargs -0 COMMAND
284 63&nbsp;
285 64&nbsp; # Requires nonstandard extensions to find and to shell (bash works).
286 65&nbsp; # variables might not stay set once the loop ends:
287 66&nbsp; find . -print0 | while IFS="" read -r -d "" file ; do ...
288 67&nbsp; COMMAND "$file" # Use quoted "$file", not $file, everywhere.
289 68&nbsp; done
290 69&nbsp;
291 70&nbsp;
292 71&nbsp;
293 72&nbsp; # Requires nonstandard extensions to find and to shell (bash works).
294 73&nbsp; # Underlying system must include named pipes (FIFOs)
295 74&nbsp; #+ or the /dev/fd mechanism.
296 75&nbsp; # In this version, variables *do* stay set after the loop ends,
297 76&nbsp; # and you can read from stdin.
298 77&nbsp; #+ (Change the 4 to another number if fd 4 is needed.)
299 78&nbsp;
300 79&nbsp; while IFS="" read -r -d "" file &#60;&#38;4 ; do
301 80&nbsp; COMMAND "$file" # Use quoted "$file" -- not $file, everywhere.
302 81&nbsp; done 4&#60; &#60;(find . -print0)
303 82&nbsp;
304 83&nbsp;
305 84&nbsp; # Named pipe version.
306 85&nbsp; # Requires nonstandard extensions to find and to shell's read (bash ok).
307 86&nbsp; # Underlying system must include named pipes (FIFOs).
308 87&nbsp; # Again, in this version, variables *do* stay set after the loop ends,
309 88&nbsp; # and you can read from stdin.
310 89&nbsp; # (Change the 4 to something else if fd 4 needed).
311 90&nbsp;
312 91&nbsp; mkfifo mypipe
313 92&nbsp;
314 93&nbsp; find . -print0 &#62; mypipe &#38;
315 94&nbsp; while IFS="" read -r -d "" file &#60;&#38;4 ; do
316 95&nbsp; COMMAND "$file" # Use quoted "$file", not $file, everywhere.
317 96&nbsp; done 4&#60; mypipe</PRE
318 ></TD
319 ></TR
320 ></TABLE
321 ></P
322 ></DIV
323 ><DIV
324 CLASS="NAVFOOTER"
325 ><HR
326 ALIGN="LEFT"
327 WIDTH="100%"><TABLE
328 SUMMARY="Footer navigation table"
329 WIDTH="100%"
330 BORDER="0"
331 CELLPADDING="0"
332 CELLSPACING="0"
333 ><TR
334 ><TD
335 WIDTH="33%"
336 ALIGN="left"
337 VALIGN="top"
338 ><A
339 HREF="awk.html"
340 ACCESSKEY="P"
341 >Prev</A
342 ></TD
343 ><TD
344 WIDTH="34%"
345 ALIGN="center"
346 VALIGN="top"
347 ><A
348 HREF="index.html"
349 ACCESSKEY="H"
350 >Home</A
351 ></TD
352 ><TD
353 WIDTH="33%"
354 ALIGN="right"
355 VALIGN="top"
356 ><A
357 HREF="exitcodes.html"
358 ACCESSKEY="N"
359 >Next</A
360 ></TD
361 ></TR
362 ><TR
363 ><TD
364 WIDTH="33%"
365 ALIGN="left"
366 VALIGN="top"
367 >Awk</TD
368 ><TD
369 WIDTH="34%"
370 ALIGN="center"
371 VALIGN="top"
372 >&nbsp;</TD
373 ><TD
374 WIDTH="33%"
375 ALIGN="right"
376 VALIGN="top"
377 >Exit Codes With Special Meanings</TD
378 ></TR
379 ></TABLE
380 ></DIV
381 ></BODY
382 ></HTML
383 >