得到的只是从路径文件名中的Bash脚本 [英] Get just the filename from a path in a Bash script
本文介绍了得到的只是从路径文件名中的Bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将如何得到的只是文件名没有扩展名并没有指定路径?
下面没有给我延期,但我仍然有路径附:
source_file_filename_no_ext = $ {SOURCE_FILE%。*}
解决方案
大多数UNIX系统具有只是目的基本名
可执行文件。
PAX> A =的/ tmp / file.txt的
PAX> B = $($基名一)
PAX>回声$ B
file.txt的
如果你想有一个唯一的bash的解决方案,你可以启动:
PAX> A =的/ tmp / XX / file.tar.gz
PAX> XPath的= $ {一%/ *}
PAX>的Xbase = $ {一个## * /}
PAX> xfext = $ {的Xbase ## *。}
PAX>点¯xpreF = $ {的Xbase%。*}
PAX>回声,回声路径= $ {}的XPath;回声preF = $ {X preF};回声EXT = $ {} xfext路径= / tmp目录/ XX
preF =文件.tar
EXT = GZ
这是小片段集的XPath
(文件路径), X preF
(文件preFIX)和 xfext
(文件扩展名)。
How would I get just the filename without the extension and no path?
The following gives me no extension, but I still have the path attached:
source_file_filename_no_ext=${source_file%.*}
解决方案
Most UNIXes have a basename
executable for just that purpose.
pax> a=/tmp/file.txt
pax> b=$(basename $a)
pax> echo $b
file.txt
If you want a bash-only solution, you can start with:
pax> a=/tmp/xx/file.tar.gz
pax> xpath=${a%/*}
pax> xbase=${a##*/}
pax> xfext=${xbase##*.}
pax> xpref=${xbase%.*}
pax> echo;echo path=${xpath};echo pref=${xpref};echo ext=${xfext}
path=/tmp/xx
pref=file.tar
ext=gz
That little snippet sets xpath
(the file path), xpref
(the file prefix) and xfext
(the file extension).
这篇关于得到的只是从路径文件名中的Bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文