如何在shell中shell f 判断文件一个文件是否为空

Shell脚本中判断输入变量或者参数是否为空的方法
投稿:junjie
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了Shell脚本中判断输入变量或者参数是否为空的方法,本文总结了5种方法,并分别给出了代码实例,需要的朋友可以参考下
1.判断变量
read -p "input a word :" word
if& [ ! -n "$word" ] ;then
&&& echo "you have not input a word!"
&&& echo "the word you input is $word"
2.判断输入参数
#!/bin/bash
if [ ! -n "$1" ] ;then
&&& echo "you have not input a word!"
&&& echo "the word you input is $1"
以下未验证。
3. 直接通过变量判断
如下所示:得到的结果为: IS NULL
if [ ! $para1 ]; then
& echo "IS NULL"
& echo "NOT NULL"
4. 使用test判断
得到的结果就是: dmin is not set!
if test -z "$dmin"
& echo "dmin is not set!"
& echo "dmin is set !"
5. 使用""判断
if [ "$dmin" = "" ]
& echo "dmin is not set!"
& echo "dmin is set !"
下面是我在某项目中写的一点脚本代码, 用在系统启动时:
#! /bin/bash
echo "Input Param Is [$1]"
if [ ! -n "$1" ] ;then
&echo "you have not input a null word!"
&./app1;./app12;./app123
elif [ $1 -eq 2 ];then
&./app12;./app123
elif [ $1 -eq 90 ];then
&echo "yy";
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具在Linux下怎么用Bash判断是否存在某种模式的文件名 - Bash @ Linux - ITeye博客
博客分类:
问:在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
[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
[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
[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
[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
[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
[root@liunx0918 tmp2]# exists_pattern_files *.gif && echo "*.gif exists" || echo "*.gif not exists"
*.gif exists
[root@liunx0918 tmp2]#
本文链接:
下面贴一下我所需要的脚本内容。
对当前目录下各个子目录判断,如果子目录中包含*_codec.*形式的文件,就执行指定的动作。
for d in *
FILES=$(ls $d/*_codec.* 2&/dev/null)
if [ "$FILES" ]; then
#echo "$d" "[$FILES]"
(cd $d; make msg)
exists_pattern_files(){
[ -e "$1" ]
for d in *
if exists_pattern_files $d/*_codec.*; then
#echo "$d" "[$FILES]"
(cd $d; make msg)
codingstandards
浏览: 3122140 次
来自: 上海
楼主咋没分析下源码呢?
tail -F 就可以吧
新手学习了,就是不明白为一个网卡配多个ip有什么用
不错,谢谢!
不错,谢谢!shell&判断文件/目录是否为空
#判断文件目录是否为空
emptydir.sh
-----------------------------------------------------------
DIRECTORY=$1
if [ "`ls -A $DIRECTORY`" = "" ]; then
echo "$DIRECTORY is indeed empty"
echo "$DIRECTORY is not empty"
-----------------------------------------------------------
-----------------------------------------------------------
count=`ls $*|wc -w`
if [ "$count" & "0" ];
echo "file size $count"
echo "empty!"
-----------------------------------------------------------
#目录是否存在
ifmkdir.sh
-----------------------------------------------------------
dir="test"
if [ ! -d $dir ]; then
echo "$dir not exists"
mkdir "$dir"
echo "$dir exists!"
-----------------------------------------------------------
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 shell 判断文件后缀 的文章

 

随机推荐