views:

155

answers:

2

I'm trying to write a post-commit hook for SVN, which is hosted on our development server. My goal is to try to automatically checkout a copy of the committed project to the directory where it is hosted on the server. However I need to be able to read only the last directory in the directory string passed to the script in order to checkout to the same sub-directory where our projects are hosted.

For example if I make an SVN commit to the project "example", my script gets "/usr/local/svn/repos/example" as its first argument. I need to get just "example" off the end of the string and then concat it with another string so I can checkout to "/server/root/example" and see the changes live immediately.

Thanks.

+4  A: 

basename does remove the directory prefix of a path:

$ basename /usr/local/svn/repos/example
example
$ echo "/server/root/$(basename /usr/local/svn/repos/example)"
/server/root/example
sth
+1, beat me to it
Wrikken
basename is definitely what I'm looking for. How can get the basename of an argument stored into a variable though? E.g. `SUBDIR="/path/to/whatever/$(basename $1)"`
tj111
@tj111: yes, that's how you get it stored in a variable...
sth
I get the error "basename: missing operand"
tj111
@tj111: sounds like is no `$1`, or `$1` is empty
sth
+4  A: 

Bash can get the last part of a path without having to call the external basename:

subdir="/path/to/whatever/${1##*/}"
Dennis Williamson