给出如下文件名:
/the/path/foo.txt
bar.txt
我希望得到:
foo
bar
为什么这行不通?
#!/bin/bash
fullfile=$1
fname=$(basename $fullfile)
fbname=${fname%.*}
echo $fbname
正确的做法是什么?
给出如下文件名:
/the/path/foo.txt
bar.txt
我希望得到:
foo
bar
为什么这行不通?
#!/bin/bash
fullfile=$1
fname=$(basename $fullfile)
fbname=${fname%.*}
echo $fbname
正确的做法是什么?
当前回答
您不必调用外部basename命令。相反,你可以使用以下命令:
$ s=/the/path/foo.txt
$ echo "${s##*/}"
foo.txt
$ s=${s##*/}
$ echo "${s%.txt}"
foo
$ echo "${s%.*}"
foo
注意,这个解决方案应该适用于所有最近(2004年以后)的POSIX兼容shell,(例如bash、dash、ksh等)。
2.6.2参数展开
关于bash字符串操作的更多信息:http://tldp.org/LDP/LG/issue18/bash.html
其他回答
basename命令有两个不同的调用;在一种情况下,您只指定路径,在这种情况下,它会给出最后一个组件,而在另一种情况下,您还会给出一个后缀,它将删除该后缀。因此,您可以通过使用basename的第二次调用来简化示例代码。此外,要注意正确引用:
fbname=$(basename "$1" .txt) echo "$fbname"
如果你想更好地使用Windows文件路径(在Cygwin下),你也可以试试这个:
fname=${fullfile##*[/|\\]}
这将在Windows上使用BaSH时考虑反斜杠分隔符。
basename和cut的组合可以很好地工作,即使是在.tar.gz这样的双结尾的情况下:
fbname=$(basename "$fullfile" | cut -d. -f1)
如果这个解决方案比Bash参数扩展需要更少的算术能力,那将是有趣的。
您不必调用外部basename命令。相反,你可以使用以下命令:
$ s=/the/path/foo.txt
$ echo "${s##*/}"
foo.txt
$ s=${s##*/}
$ echo "${s%.txt}"
foo
$ echo "${s%.*}"
foo
注意,这个解决方案应该适用于所有最近(2004年以后)的POSIX兼容shell,(例如bash、dash、ksh等)。
2.6.2参数展开
关于bash字符串操作的更多信息:http://tldp.org/LDP/LG/issue18/bash.html
下面是另一种(更复杂的)获取文件名或扩展名的方法,首先使用rev命令反转文件路径,从第一个开始切割。然后再次反转文件路径,就像这样:
filename=`rev <<< "$1" | cut -d"." -f2- | rev`
fileext=`rev <<< "$1" | cut -d"." -f1 | rev`