解压缩文件名前缀如果没有击路径和扩展 [英] Extract File Basename Without Path and Extension in Bash
本文介绍了解压缩文件名前缀如果没有击路径和扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于文件名这样的:
/the/path/foo.txt
bar.txt
我希望能得到
foo
bar
为什么不起作用?
Why this doesn't work?
#!/bin/bash
fullfile=$1
fname=$(basename $fullfile)
fbname=${fname%.*}
echo $fbname
什么是做正确的方式?
What's the right way to do it?
推荐答案
您不必调用外部基本名
命令。相反,你可以使用下面的命令:
You don't have to call external basename
command. Instead, you could use the following commands:
$ s=/the/path/foo.txt
$ echo ${s##*/}
foo.txt
$ s=${s##*/}
$ echo ${s%.txt}
foo
$ echo ${s%.*}
foo
(从这里),即使OP是专门约BASH,重要的是要强调的是,上述命令使用bash shell的参数扩展,不得在其他壳工作。
(From here) even though the OP is specifically about BASH, it is important to emphasize that the above commands use the BASH Shell Parameter Expansion and may not work in other shells.
这篇关于解压缩文件名前缀如果没有击路径和扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文