nev*_*int 303 unix linux bash filenames
给定这样的文件名:
/the/path/foo.txt
bar.txt
Run Code Online (Sandbox Code Playgroud)
我希望得到:
foo
bar
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?
#!/bin/bash
fullfile=$1
fname=$(basename $fullfile)
fbname=${fname%.*}
echo $fbname
Run Code Online (Sandbox Code Playgroud)
什么是正确的方法呢?
gho*_*g74 594
您不必调用外部basename命令.相反,您可以使用以下命令:
$ s=/the/path/foo.txt
$ echo ${s##*/}
foo.txt
$ s=${s##*/}
$ echo ${s%.txt}
foo
$ echo ${s%.*}
foo
Run Code Online (Sandbox Code Playgroud)
请注意,这个解决方案应该在所有最近的(工作后2004年)POSIX兼容的外壳(例如bash,dash,ksh等).
有关bash String Manipulations的更多信息:http: //tldp.org/LDP/LG/issue18/bash.html
Mic*_*yan 271
的基名命令有两种不同调用; 在一个中,您只指定路径,在这种情况下,它会为您提供最后一个组件,而在另一个中,您还会提供一个将删除的后缀.因此,您可以使用basename的第二次调用来简化示例代码.另外,要小心正确引用事物:
fbname=$(basename "$1" .txt) echo "$fbname"
小智 57
basename和cut的组合工作正常,即使在双重结尾的情况下.tar.gz:
fbname=$(basename "$fullfile" | cut -d. -f1)
Run Code Online (Sandbox Code Playgroud)
如果此解决方案比Bash参数扩展需要更少的算术能力,那将会很有趣.
agc*_*agc 19
纯粹bash,不basename,没有变量杂耍.设置一个字符串并echo:
s=/the/path/foo.txt
echo ${s//+(*\/|.*)}
Run Code Online (Sandbox Code Playgroud)
输出:
foo
Run Code Online (Sandbox Code Playgroud)
注意:bash extglob选项必须是"on",(在Ubuntu上它默认为"on"),如果不是,请执行:
shopt -s extglob
Run Code Online (Sandbox Code Playgroud)
走过${s//+(*\/|.*)}:
${s- 从$ s开始.// 替换模式的每个实例.+(匹配一个或更多的的模式列表中括号.*\/比赛前东西/.(第1种模式)|要么.(模式分隔符.).*匹配之后什么..(第2种模式)bash结束模式列表..结束参数扩展 - 因为没有)(在字符串替换之前),匹配的模式将被删除.相关}背景:
Run Code Online (Sandbox Code Playgroud)${parameter/pattern/string} Pattern substitution. The pattern is expanded to produce a pat? tern just as in pathname expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with /, all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with #, it must match at the begin? ning of the expanded value of parameter. If pattern begins with %, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / fol? lowing pattern may be omitted. If parameter is @ or *, the sub? stitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.
Run Code Online (Sandbox Code Playgroud)If the extglob shell option is enabled using the shopt builtin, several extended pattern matching operators are recognized. In the following description, a pattern-list is a list of one or more patterns separated by a |. Composite patterns may be formed using one or more of the fol? lowing sub-patterns: ?(pattern-list) Matches zero or one occurrence of the given patterns *(pattern-list) Matches zero or more occurrences of the given patterns +(pattern-list) Matches one or more occurrences of the given patterns @(pattern-list) Matches one of the given patterns !(pattern-list) Matches anything except one of the given patterns
san*_*ica 16
以下是oneliners:
$(basename ${s%.*})$(basename ${s} .${s##*.})我需要这个,就像bongbang和w4etwetewtwet所要求的一样.
这是获取文件名或扩展名的另一种(更复杂的)方法,首先使用rev命令反转文件路径,从第一个文件切换.,然后再次反转文件路径,如下所示:
filename=`rev <<< "$1" | cut -d"." -f2- | rev`
fileext=`rev <<< "$1" | cut -d"." -f1 | rev`
Run Code Online (Sandbox Code Playgroud)