如何在Shell脚本中提取文件名和扩展名


当前第2页 返回上一页

最后在一个shell脚本中测试所有内容。使用以下内容创建新的脚本文件。在执行脚本期间,文件名将作为命令行参数传递。

1

2

3

4

5

6

7

8

9

10

11

#!/bin/bash

 

fullfilename=$1

filename=$(basename "$fullfilename")

fname="${filename%.*}"

ext="${filename##*.}"

 

echo "Input File: $fullfilename"

echo "Filename without Path: $filename"

echo "Filename without Extension: $fname"

echo "File Extension without Name: $ext"

我们以文件名作为命令行参数来执行脚本。

1

2

3

4

5

6

$ ./script.sh /etc/apache2/apache2.conf

 

Input File: /etc/apache2/apache2.conf

Filename without Path: apache2.conf

Filename without Extension: apache2

File Extension without Name: conf

本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的Linux教程视频栏目!

以上就是如何在Shell脚本中提取文件名和扩展名的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

shell脚本高级编程的详细讲解

实例分享shell字符截取命令之cut命令

linux运维之shell变量.md

linux怎么运行shell程序

linux--bash 和 shell讲解

如何在shell脚本中传递命令行参数

linux中shell脚本怎么运行

通过几个案例掌握shell编程条件分支结构

linux中什么是shell

如何在shell脚本中为用户分配密码

更多相关阅读请进入《shell》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...