Rob Hudson wrote:
Looks like I can do:
path = `dirname $path` # for directory
path = `basename $path` # similar to path=${path##/*/}
Is this the best option?
That is a good option. It provides backward compatibility
with the boring shell (Bourne). However I prefer the $()
syntax since it provides for recursion. It is also a lot
more graceful about quotes.
path="$(dirname "${path}")"
But to answer your original question
directory="${path%/*}"
The % means to chop stuff off the end of the path envariable.
The pattern to chop off is /*, which means a slash followed
by anything. Since we only used one %, it chops off the
minimum that matches the pattern. If we had used %% instead,
it would chop off the maximum, resulting in the empty string.
Finally, by enclosing it in quotes we allow for paths with
blanks in them. (Blanks are rather rude, but some people use
them, especially people with a Windoze background.)
The example you provided is fragile. By starting with a /
it only extracts the basename if the path starts with a /.
So relative paths would fail to extract.
But lets use something similar
as a start on a different approach. First extract the filename
and then use that to extract the directory.
filename="${path##*/}"
Remove everything up to and including the first slash.
directory="${path%/${filename}}"
Remove from path $filename and its preceding slash. Notice
that since there are no wild cards in this path it doesn't
matter if we use % or %%.
One thing I don't know about these competing solutions
is whether the latter ones are faster than using
--
Allen Brown [EMAIL PROTECTED] http://www.peak.org/~abrown/
Is sex dirty? Only if it's done right. ---Woody Allen
Rob Hudson wrote:
Given a variable with something like this in it:
/Volumes/Projects/Testing/Simple_Carbon_App.pkg
I need to extract the base path to get:
/Volumes/Projects/Testing
I found how to get the last part in an example, though I don't fully
understand what's going on. Assuming "path" has the first volume
path, calling this:
path=${path##/*/}
Will set path to: Simple_Carbon_App.pkg
I'm looking for more or less the opposite of that.
Any pointers to documentation would be good. Working code is nice
too. :P
Thanks,
Rob
_______________________________________________
EUGLUG mailing list
[email protected]
http://www.euglug.org/mailman/listinfo/euglug