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