本文摘自PHP中文网,作者不言,侵删。
有时你可能需要在不同的变量中提取文件名和扩展名来完成bash shell编程中的任务。本篇文章将介绍从完整的文件名或路径中提取文件名和文件扩展名。以下是详细的内容
1、获取没有路径的文件名:
首先从输入文件名中删除完整的文件路径。例如,如果文件名输入为/etc/apache2/apache2.conf,则仅提取完整文件名为apache.conf.
1 2 3 4 5 | #!/bin/bash
fullfilename= "/etc/apache2/apache2.conf"
filename=$(basename "$fullfilename" )
echo $filename
|
2、没有扩展名的文件名:
现在,从提取的不带路径的完整文件名中提取不带扩展名的文件名,如下所示。
1 2 3 4 5 | #!/bin/bash
fullfilename= "/etc/apache2/apache2.conf"
filename=$(basename "$fullfilename" )
fname= "${filename%.*}"
echo $fname
|
3、没有名称的文件扩展名:
现在从提取的不带路径的完整文件名中提取不带名称的文件扩展名。
1 2 3 4 5 6 | #!/bin/bash
fullfilename= "/etc/apache2/apache2.conf"
filename=$(basename "$fullfilename" )
ext= "${filename##*.}"
echo $ext
|
4、测试:
阅读剩余部分
相关阅读 >>
shell编程实战之执行多个目录下的php程序
linux shell ftp按照日期去下载文件的方法
freeworld.posterous.com linux bash shell cheat sheet----autho:raphael
linux运维之shell变量.md
linux怎么执行shell脚本
详细讲解linux shell中的printf的用法
有关linux中shell内置判断语句的介绍
如何让shell访问正在运行的docker容器
linux中终端与shell的概念
shell判断字符串为空的方法
更多相关阅读请进入《shell》频道 >>
转载请注明出处:木庄网络博客 » 如何在Shell脚本中提取文件名和扩展名