如何通过最后一个斜杠分割路径? [英] How to split path by last slash?

查看:32
本文介绍了如何通过最后一个斜杠分割路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件(比如名为 list.txt),其中包含文件的相对路径,每行一个路径,例如:

I have a file (say called list.txt) that contains relative paths to files, one path per line, i.e. something like this:

foo/bar/file1
foo/bar/baz/file2
goo/file3

我需要编写一个 bash 脚本,一次处理一个路径,在最后一个斜杠处将其拆分,然后启动另一个进程,将路径的两个部分作为参数提供给它.到目前为止,我只有循环部分:

I need to write a bash script that processes one path at a time, splits it at the last slash and then launches another process feeding it the two pieces of the path as arguments. So far I have only the looping part:

for p in `cat list.txt`
do
   # split $p like "foo/bar/file1" into "foo/bar/" as part1 and "file1" as part2
   inner_process.sh $part1 $part2
done

我如何拆分?这在路径没有斜线的退化情况下是否有效?

How do I split? Will this work in the degenerate case where the path has no slashes?

推荐答案

使用 basenamedirname,这就是你所需要的.

Use basename and dirname, that's all you need.

part1=$(dirname "$p")
part2=$(basename "$p")

这篇关于如何通过最后一个斜杠分割路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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