在 bash 中提取没有路径和扩展名的文件基名[重复]

2024-10-23 08:47:00
admin
原创
222
摘要:问题描述:给定如下文件名:/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

正确的做法是什么?


解决方案 1:

您不必调用外部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`

来源:Shell 命令语言 2.6.2 参数扩展

有关 bash 字符串操作的更多信息: http://tldp.org/LDP/LG/issue18/bash.html

解决方案 2:

basename命令有两种不同的调用方式;一种是只指定路径,在这种情况下,它会给出最后一个组件;另一种是,你还会给出一个后缀,它会删除这个后缀。因此,你可以使用 basename 的第二次调用来简化示例代码。另外,请注意正确引用以下内容:

fbname=$(基本名称“$1”.txt)
回显“$fbname”

解决方案 3:

基本名称和剪切的组合可以很好地工作,即使在双结尾的情况下也是如此.tar.gz

fbname=$(basename "$fullfile" | cut -d. -f1)

如果这个解决方案需要的算术能力比 Bash 参数扩展更少,那将会很有趣。

解决方案 4:

以下是一些要点:

  1. $(basename "${s%.*}")

  2. $(basename "${s}" ".${s##*.}")

我需要这个,和 bongbang 和 w4etwetewtwet 要求的一样。

例子:

$ s=/the/path/foo.txt
$ echo "$(basename "${s%.*}")"
foo
$ echo "$(basename "${s}" ".${s##*.}")"
foo

解决方案 5:

纯粹的bash,没有basename,没有变量杂耍。设置一个字符串并echo

p=/the/path/foo.txt
echo "${p//+(*/|.*)}"

输出:

foo

注意:bash extglob选项必须为“on”(Ubuntu默认将extglob设置为“on”),如果不是,请执行以下操作:

shopt -s extglob

走过${p//+(*/|.*)}

  1. ${p——以$p开头。

  2. //替换下列模式的每个实例。

  3. +(匹配括号中的一个或多个模式列表(直到下面的第 7 项)。

  4. 第一个模式:*/匹配文字“ /”字符之前的任何内容。

  5. 模式分隔符|,在本例中起到逻辑或的作用。

  6. 第二种模式:.*匹配文字“ .”之后的任何内容 - 也就是说,bash.”中的只是一个句点字符,而不是 正则表达式点。

  7. )结束模式列表

  8. }结束参数扩展。对于字符串替换,通常会有另一个/there,后面跟着替换字符串。但是由于没有/there,匹配的模式将被替换为空;这会删除匹配项。

相关man bash背景:

  1. 模式替换

  ${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.
  1. 扩展模式匹配

  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

解决方案 6:

这是获取文件名或扩展名的另一种(更复杂)方法,首先使用命令rev反转文件路径,从第一个剪切.,然后再次反转文件路径,如下所示:

filename=`rev <<< "$1" | cut -d"." -f2- | rev`
fileext=`rev <<< "$1" | cut -d"." -f1 | rev`

解决方案 7:

如果您想要更好地使用 Windows 文件路径(在 Cygwin 下),您也可以尝试以下操作:

fname=${fullfile##*[/|\\]}

当在 Windows 上使用 BaSH 时,这将考虑反斜杠分隔符。

解决方案 8:

这只是我想到提取扩展的另一种方法,使用此线程中的帖子和我更熟悉的小型知识库。

ext="$(rev <<< "$(cut -f "1" -d "." <<< "$(rev <<< "file.docx")")")"

注意:请对我使用引号的方式提出建议;它对我有用,但我可能忽略了它们的正确使用(我可能使用了太多)。

解决方案 9:

使用 basename 命令。它的手册页在这里: http ://unixhelp.ed.ac.uk/CGI/man-cgi?basename

相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   2577  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1553  
  IPD(Integrated Product Development)流程作为一种先进的产品开发管理模式,在众多企业中得到了广泛应用。其中,技术评审与决策评审是IPD流程中至关重要的环节,它们既有明显的区别,又存在紧密的协同关系。深入理解这两者的区别与协同,对于企业有效实施IPD流程,提升产品开发效率与质量具有重要意义...
IPD管理流程   26  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、ClickUp、Freshdesk、GanttPRO、Planview、Smartsheet、Asana、Nifty、HubPlanner、Teamwork。在当今快速变化的商业环境中,项目管理软件已成为企业提升效率、优化资源分配和确保项目按时交付的关键工具。然而...
项目管理系统   21  
  建设工程项目质量关乎社会公众的生命财产安全,也影响着企业的声誉和可持续发展。高质量的建设工程不仅能为使用者提供舒适、安全的环境,还能提升城市形象,推动经济的健康发展。在实际的项目操作中,诸多因素会对工程质量产生影响,从规划设计到施工建设,再到后期的验收维护,每一个环节都至关重要。因此,探寻并运用有效的方法来提升建设工程...
工程项目管理制度   18  
热门文章
项目管理软件有哪些?
曾咪二维码

扫码咨询,免费领取项目管理大礼包!

云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用