euglug.org

[Eug-lug] calling all bash experts…

작성자

카테고리:



Fred JamesThu Dec 8 16:12:03 PST 2005

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
>
Rob Hudson
Are you looking for something like the following?
Regards
Fred James

BasePath=`echo $PathVariable | awk '{
    for ( i = 1; i < NF; i++ ) {
       printf ( "/%s", $i )
    }
    printf ( "\n" )
}'`