在 Bash 的文件路径参数中获取最后一个目录名/文件名 [英] Get last dirname/filename in a file path argument in Bash

查看:26
本文介绍了在 Bash 的文件路径参数中获取最后一个目录名/文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 SVN 编写一个提交后挂钩,它托管在我们的开发服务器上.我的目标是尝试自动将已提交项目的副本签出到它在服务器上托管的目录.但是,我需要能够仅读取传递给脚本的目录字符串中的最后一个目录,以便签出到托管我们项目的同一子目录.

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.

例如,如果我对项目example"进行 SVN 提交,我的脚本将/usr/local/svn/repos/example"作为它的第一个参数.我只需要从字符串的末尾取出example",然后将它与另一个字符串连接起来,这样我就可以结帐到/server/root/example"并立即查看更改.

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.

推荐答案

basename 确实删除了路径的目录前缀:

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

这篇关于在 Bash 的文件路径参数中获取最后一个目录名/文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆