| 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 #!/usr/bin/env bash |
| 118 | 2 #----------------------------------------------------------- |
| 119 | 3 # Management of PATH, LD_LIBRARY_PATH, MANPATH variables... |
| 120 | 4 # By Emmanuel Rouat <no-email> |
| 121 | 5 # (Inspired by the bash documentation 'pathfuncs' and on |
| 122 | 6 # discussions found on stackoverflow: |
| 123 | 7 # http://stackoverflow.com/questions/370047/ |
| 124 | 8 # http://stackoverflow.com/questions/273909/#346860 ) |
| 125 | 9 # Last modified: Sat Sep 22 12:01:55 CEST 2012 |
| 126 | 10 # |
| 127 | 11 # The following functions handle spaces correctly. |
| 128 | 12 # These functions belong in .bash_profile rather than in |
| 129 | 13 # .bashrc, I guess. |
| 130 | 14 # |
| 131 | 15 # The modular aspect of these functions should make it easy |
| 132 | 16 # to expand them to handle path substitutions instead |
| 133 | 17 # of path removal etc.... |
| 134 | 18 # |
| 135 | 19 # See http://www.catonmat.net/blog/awk-one-liners-explained-part-two/ |
| 136 | 20 # (item 43) for an explanation of the 'duplicate-entries' removal |
| 137 | 21 # (it's a nice trick!) |
| 138 | 22 #----------------------------------------------------------- |
| 139 | 23 |
| 140 | 24 # Show $@ (usually PATH) as list. |
| 141 | 25 function p_show() { local p="$@" && for p; do [[ ${!p} ]] && |
| 142 | 26 echo -e ${!p//:/\\n}; done } |
| 143 | 27 |
| 144 | 28 # Filter out empty lines, multiple/trailing slashes, and duplicate entries. |
| 145 | 29 function p_filter() |
| 146 | 30 { awk '/^[ \t]*$/ {next} {sub(/\/+$/, "");gsub(/\/+/, "/")}!x[$0]++' ;} |
| 147 | 31 |
| 148 | 32 # Rebuild list of items into ':' separated word (PATH-like). |
| 149 | 33 function p_build() { paste -sd: ;} |
| 150 | 34 |
| 151 | 35 # Clean $1 (typically PATH) and rebuild it |
| 152 | 36 function p_clean() |
| 153 | 37 { local p=${1} && eval ${p}='$(p_show ${p} | p_filter | p_build)' ;} |
| 154 | 38 |
| 155 | 39 # Remove $1 from $2 (found on stackoverflow, with modifications). |
| 156 | 40 function p_rm() |
| 157 | 41 { local d=$(echo $1 | p_filter) p=${2} && |
| 158 | 42 eval ${p}='$(p_show ${p} | p_filter | grep -xv "${d}" | p_build)' ;} |
| 159 | 43 |
| 160 | 44 # Same as previous, but filters on a pattern (dangerous... |
| 161 | 45 #+ don't use 'bin' or '/' as pattern!). |
| 162 | 46 function p_rmpat() |
| 163 | 47 { local d=$(echo $1 | p_filter) p=${2} && eval ${p}='$(p_show ${p} | |
| 164 | 48 p_filter | grep -v "${d}" | p_build)' ;} |
| 165 | 49 |
| 166 | 50 # Delete $1 from $2 and append it cleanly. |
| 167 | 51 function p_append() |
| 168 | 52 { local d=$(echo $1 | p_filter) p=${2} && p_rm "${d}" ${p} && |
| 169 | 53 eval ${p}='$(p_show ${p} d | p_build)' ;} |
| 170 | 54 |
| 171 | 55 # Delete $1 from $2 and prepend it cleanly. |
| 172 | 56 function p_prepend() |
| 173 | 57 { local d=$(echo $1 | p_filter) p=${2} && p_rm "${d}" ${p} && |
| 174 | 58 eval ${p}='$(p_show d ${p} | p_build)' ;} |
| 175 | 59 |
| 176 | 60 # Some tests: |
| 177 | 61 echo |
| 178 | 62 MYPATH="/bin:/usr/bin/:/bin://bin/" |
| 179 | 63 p_append "/project//my project/bin" MYPATH |
| 180 | 64 echo "Append '/project//my project/bin' to '/bin:/usr/bin/:/bin://bin/'" |
| 181 | 65 echo "(result should be: /bin:/usr/bin:/project/my project/bin)" |
| 182 | 66 echo $MYPATH |
| 183 | 67 |
| 184 | 68 echo |
| 185 | 69 MYOTHERPATH="/bin:/usr/bin/:/bin:/project//my project/bin" |
| 186 | 70 p_prepend "/project//my project/bin" MYOTHERPATH |
| 187 | 71 echo "Prepend '/project//my project/bin' \ |
| 188 | 72 to '/bin:/usr/bin/:/bin:/project//my project/bin/'" |
| 189 | 73 echo "(result should be: /project/my project/bin:/bin:/usr/bin)" |
| 190 | 74 echo $MYOTHERPATH |
| 191 | 75 |
| 192 | 76 echo |
| 193 | 77 p_prepend "/project//my project/bin" FOOPATH # FOOPATH doesn't exist. |
| 194 | 78 echo "Prepend '/project//my project/bin' to an unset variable" |
| 195 | 79 echo "(result should be: /project/my project/bin)" |
| 196 | 80 echo $FOOPATH |
| 197 | 81 |
| 198 | 82 echo |
| 199 | 83 BARPATH="/a:/b/://b c://a:/my local pub" |
| 200 | 84 p_clean BARPATH |
| 201 | 85 echo "Clean BARPATH='/a:/b/://b c://a:/my local pub'" |
| 202 | 86 echo "(result should be: /a:/b:/b c:/my local pub)" |
| 203 | 87 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 Doing it correctly: A quick summary |
| 223 | 2 by David Wheeler |
| 224 | 3 http://www.dwheeler.com/essays/filenames-in-shell.html |
| 225 | 4 |
| 226 | 5 So, how can you process filenames correctly in shell? Here's a quick |
| 227 | 6 summary about how to do it correctly, for the impatient who "just want the |
| 228 | 7 answer". In short: Double-quote to use "$variable" instead of $variable, |
| 229 | 8 set IFS to just newline and tab, prefix all globs/filenames so they cannot |
| 230 | 9 begin with "-" when expanded, and use one of a few templates that work |
| 231 | 10 correctly. Here are some of those templates that work correctly: |
| 232 | 11 |
| 233 | 12 |
| 234 | 13 IFS="$(printf '\n\t')" |
| 235 | 14 # Remove SPACE, so filenames with spaces work well. |
| 236 | 15 |
| 237 | 16 # Correct glob use: |
| 238 | 17 #+ always use "for" loop, prefix glob, check for existence: |
| 239 | 18 for file in ./* ; do # Use "./*" ... NEVER bare "*" ... |
| 240 | 19 if [ -e "$file" ] ; then # Make sure it isn't an empty match. |
| 241 | 20 COMMAND ... "$file" ... |
| 242 | 21 fi |
| 243 | 22 done |
| 244 | 23 |
| 245 | 24 |
| 246 | 25 |
| 247 | 26 # Correct glob use, but requires nonstandard bash extension. |
| 248 | 27 shopt -s nullglob # Bash extension, |
| 249 | 28 #+ so that empty glob matches will work. |
| 250 | 29 for file in ./* ; do # Use "./*", NEVER bare "*" |
| 251 | 30 COMMAND ... "$file" ... |
| 252 | 31 done |
| 253 | 32 |
| 254 | 33 |
| 255 | 34 |
| 256 | 35 # These handle all filenames correctly; |
| 257 | 36 #+ can be unwieldy if COMMAND is large: |
| 258 | 37 find ... -exec COMMAND... {} \; |
| 259 | 38 find ... -exec COMMAND... {} \+ # If multiple files are okay for COMMAND. |
| 260 | 39 |
| 261 | 40 |
| 262 | 41 |
| 263 | 42 # This skips filenames with control characters |
| 264 | 43 #+ (including tab and newline). |
| 265 | 44 IFS="$(printf '\n\t')" |
| 266 | 45 controlchars="$(printf '*[\001-\037\177]*')" |
| 267 | 46 for file in $(find . ! -name "$controlchars"') ; do |
| 268 | 47 COMMAND "$file" ... |
| 269 | 48 done |
| 270 | 49 |
| 271 | 50 |
| 272 | 51 |
| 273 | 52 # Okay if filenames can't contain tabs or newlines -- |
| 274 | 53 #+ beware the assumption. |
| 275 | 54 IFS="$(printf '\n\t')" |
| 276 | 55 for file in $(find .) ; do |
| 277 | 56 COMMAND "$file" ... |
| 278 | 57 done |
| 279 | 58 |
| 280 | 59 |
| 281 | 60 |
| 282 | 61 # Requires nonstandard but common extensions in find and xargs: |
| 283 | 62 find . -print0 | xargs -0 COMMAND |
| 284 | 63 |
| 285 | 64 # Requires nonstandard extensions to find and to shell (bash works). |
| 286 | 65 # variables might not stay set once the loop ends: |
| 287 | 66 find . -print0 | while IFS="" read -r -d "" file ; do ... |
| 288 | 67 COMMAND "$file" # Use quoted "$file", not $file, everywhere. |
| 289 | 68 done |
| 290 | 69 |
| 291 | 70 |
| 292 | 71 |
| 293 | 72 # Requires nonstandard extensions to find and to shell (bash works). |
| 294 | 73 # Underlying system must include named pipes (FIFOs) |
| 295 | 74 #+ or the /dev/fd mechanism. |
| 296 | 75 # In this version, variables *do* stay set after the loop ends, |
| 297 | 76 # and you can read from stdin. |
| 298 | 77 #+ (Change the 4 to another number if fd 4 is needed.) |
| 299 | 78 |
| 300 | 79 while IFS="" read -r -d "" file <&4 ; do |
| 301 | 80 COMMAND "$file" # Use quoted "$file" -- not $file, everywhere. |
| 302 | 81 done 4< <(find . -print0) |
| 303 | 82 |
| 304 | 83 |
| 305 | 84 # Named pipe version. |
| 306 | 85 # Requires nonstandard extensions to find and to shell's read (bash ok). |
| 307 | 86 # Underlying system must include named pipes (FIFOs). |
| 308 | 87 # Again, in this version, variables *do* stay set after the loop ends, |
| 309 | 88 # and you can read from stdin. |
| 310 | 89 # (Change the 4 to something else if fd 4 needed). |
| 311 | 90 |
| 312 | 91 mkfifo mypipe |
| 313 | 92 |
| 314 | 93 find . -print0 > mypipe & |
| 315 | 94 while IFS="" read -r -d "" file <&4 ; do |
| 316 | 95 COMMAND "$file" # Use quoted "$file", not $file, everywhere. |
| 317 | 96 done 4< 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 | > </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 | > |