问:在Linux下怎么用Bash判断是否存在某种模式的文件名?
比如,用脚本判断是否有 *_codec.* 形式的文件名,或者有 *.gif 形式的文件名。
答:
方法一:
[ "$(ls *.gif 2>/dev/null)" ] && echo "*.gif exists" || echo "*.gif not exists"
方法二:
exists_pattern_files(){
[ -e "$1" ]
}
exists_pattern_files *.gif && echo "*.gif exists" || echo "*.gif not exists"
探索过程:
用 test -e file 或 [ -e file ] 只能判断单个确定名称的文件,如果是通配符模式指定的文件名,这种方式就不凑效了。
有三种情况需要考虑到:
(1)没有匹配此模式的文件;
(2)刚好有一个匹配此模式的文件;
(3)有多于一个匹配此模式的文件;
下面以 *.gif 模式来测试:
(1)没有匹配此模式的文件;
[root@liunx0918 tmp0]# ls *.gif
ls: *.gif: 没有那个文件或目录
[root@liunx0918 tmp0]# [ -e *.gif ] && echo "*.gif exists" || echo "*.gif not exists"
*.gif not exists
[root@liunx0918 tmp0]#
(2)刚好有一个匹配此模式的文件;
[root@liunx0918 tmp0]# touch 1.gif
[root@liunx0918 tmp0]# ls *.gif
1.gif
[root@liunx0918 tmp0]# [ -e *.gif ] && echo "*.gif exists" || echo "*.gif not exists"
*.gif exists
[root@liunx0918 tmp0]#
(3)有多于一个匹配此模式的文件;
[root@liunx0918 tmp0]# touch 2.gif
[root@liunx0918 tmp0]# ls *.gif
1.gif 2.gif
[root@liunx0918 tmp0]# [ -e *.gif ] && echo "*.gif exists" || echo "*.gif not exists"
-bash: [: 1.gif: binary operator expected
*.gif not exists
[root@liunx0918 tmp0]#
前面两种情况,还可以判断,第三种情况脚本就会报错,谁又能保证匹配的文件不会多于一个呢?
首先想到:可以用 ls 命令先列出该模式文件的输出进行判断。如下所示:
(1)没有匹配此模式的文件;
[root@liunx0918 tmp1]# ls *.gif
ls: *.gif: 没有那个文件或目录
[root@liunx0918 tmp1]# [ "$(ls *.gif)" ] && echo "*.gif exists" || echo "*.gif not exists"
ls: *.gif: 没有那个文件或目录
*.gif not exists
[root@liunx0918 tmp1]#
注意上面标记为红色的内容,其实脚本中不希望看到这个信息,这个信息是输出在标准错误输出的,用下面的方法可以
[root@liunx0918 tmp1]# [ "$(ls *.gif 2>/dev/null)" ] && echo "*.gif exists" || echo "*.gif not exists"
*.gif not exists
[root@liunx0918 tmp1]#
(2)刚好有一个匹配此模式的文件;
[root@liunx0918 tmp1]# touch 1.gif
[root@liunx0918 tmp1]# ls *.gif
1.gif
[root@liunx0918 tmp1]# [ "$(ls *.gif 2>/dev/null)" ] && echo "*.gif exists" || echo "*.gif not exists"
*.gif exists
[root@liunx0918 tmp1]#
(3)有多于一个匹配此模式的文件;
[root@liunx0918 tmp1]# touch 2.gif
[root@liunx0918 tmp1]# ls *.gif
1.gif 2.gif
[root@liunx0918 tmp1]# [ "$(ls *.gif 2>/dev/null)" ] && echo "*.gif exists" || echo "*.gif not exists"
*.gif exists
[root@liunx0918 tmp1]#
忽然灵光一闪,既然可以通过ls列举文件的方式来进行,其实Bash本身就支持文件名通配符展开,不妨来写个简单的函数
exists_pattern_files(){
[ -e "$1" ]
}
[root@liunx0918 tmp2]# exists_pattern_files(){
> [ -e "$1" ]
> }
注意,把文件名模式作为参数传递给此函数(不带任何引号),Bash就会自动展开文件模式,有多少个匹配的文件就会有多少个参数,而模式本身不会当做参数传递给函数;
如果没有匹配的文件名称,把模式本身传递给函数。而这个函数的实现部分相当简单,只需要对第一个参数指定的文件进行判断即可,为了保险起见,带上双引号。
(1)没有匹配此模式的文件;
[root@liunx0918 tmp2]# ls *.gif
ls: *.gif: 没有那个文件或目录
[root@liunx0918 tmp2]# exists_pattern_files *.gif && echo "*.gif exists" || echo "*.gif not exists"
*.gif not exists
(2)刚好有一个匹配此模式的文件;
[root@liunx0918 tmp2]# touch 1.gif
[root@liunx0918 tmp2]# ls *.gif
1.gif
[root@liunx0918 tmp2]# exists_pattern_files *.gif && echo "*.gif exists" || echo "*.gif not exists"
*.gif exists
(3)有多于一个匹配此模式的文件;
[root@liunx0918 tmp2]# touch 2.gif
[root@liunx0918 tmp2]# ls *.gif
1.gif 2.gif
[root@liunx0918 tmp2]# exists_pattern_files *.gif && echo "*.gif exists" || echo "*.gif not exists"
*.gif exists
[root@liunx0918 tmp2]#
本文链接:http://codingstandards.iteye.com/blog/1183303
下面贴一下我所需要的脚本内容。
对当前目录下各个子目录判断,如果子目录中包含*_codec.*形式的文件,就执行指定的动作。
实现一:
for d in *
do
FILES=$(ls $d/*_codec.* 2>/dev/null)
if [ "$FILES" ]; then
#echo "$d" "[$FILES]"
(cd $d; make msg)
fi
done
实现二:
exists_pattern_files(){
[ -e "$1" ]
}
for d in *
do
if exists_pattern_files $d/*_codec.*; then
#echo "$d" "[$FILES]"
(cd $d; make msg)
fi
done
分享到:
相关推荐
Linux命令行和bash脚本编程圣经 Linux命令行和bash脚本编程圣经
linux下提示bash-command not found.docx
linux bash shell 实现源码
这是一个关于在linux下bash的编程的PPT
在linux下进行bash编程来维护系统 是linux运维手册 shell编程
linux shell bash 源码等等
以上就是小编为大家带来的解决ssh远程登陆linux显示-bash-4.1$的问题全部内容了,希望大家多多支持软件开发网~ 您可能感兴趣的文章:浅谈linux中shell变量$#,$@,$0,$1,$2的含义解释php $_SERVER windows系统与linux...
linux平台的bash shell开发帮助文档(手册),英文版,版本4.2。日常开发shell翻阅, 章节如下: Definitions:: Some definitions used i n the rest of this manual. Basic Shell Features:: The shell "building ...
这个是我当年考rhce是收集的资料,现在rhce已过了,我觉得这些资料还是不错的,特拿出来和大家分享
linux服务器用户登录后显示-bash问题l
linux shell scripting with bash,讲linux下shell编程,全英文版,喜欢的下
Linux的bash练习
Bash中文手册,适合Linux shell入门级使用!
高级linux bash shell 编程! 高级linux bash shell 编程! 高级linux bash shell 编程! 高级linux bash shell 编程! 高级linux bash shell 编程! 高级linux bash shell 编程! 高级linux bash shell 编程!
Bash Shell 管理Linux
在《linux常用命令与高级Bash脚本编写指南(两个CHM)》中的压缩包忘记加上脚本源码了,对不起大家,可以从这里下载。上文地址: http://download.csdn.net/source/2270969 压缩包解开后可以找到上文中对应的全部bash...
这本书介绍了bash大量的细节和广阔的范围,使用大量的例子详细地介绍了 Bash的语法,各种技巧,调试等等的技术,以循序渐进的学习方式,让你了解Bash的所有特性,在书中还有许多练习可以引导你思考,以得到更深入的...
1.上面这三个文件是bash shell的用户环境配置文件,位于用户的主目录下。其中.bash_profile是最重要的一个配置文件,它在用户每次登录系统时被读取,里面的所有命令都会被bash执行。.profile(由Bourne Shell和Korn ...
主要介绍了bash判断文件或文件夹是否存在的shell代码,需要的朋友可以参考下
linux bash vim配置文件,不错的配置,可以节省大家时间。