本文介绍了Ubuntu bash脚本:如何用最后一个斜杠分割路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文件(例如称为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 when path has no slashes?
Thx
推荐答案
使用basename
和dirname
,这就是您所需要的.
Use basename
and dirname
, that's all you need.
part1=`dirname "$p"`
part2=`basename "$p"`
这篇关于Ubuntu bash脚本:如何用最后一个斜杠分割路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!