栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

shell学习之路--条件判断

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

shell学习之路--条件判断

目录

一、条件测试结构

二、 实例分析

三、文件测试与整数比较

 四、组合比较与测试


一、条件测试结构

1.if/then结构支持以下形式:

 If…fi

 If…else…fi

 If…elif…else…fi

2.if/then结构判断命令列表的退出状态码是否为0

2.1 if grep -q Bash file1 查看

2.2 if echo “Linux”  | grep -q “$inu”

2.3 if test -z “$1”

2.4 if /usr/bin/test -z “$1”

2.5 id [ -z “$1” ] ([ 是个命令 ] 是 [ 是一命令)

2.6 if [[ $conditionl || $conditionl@ ]]

[root@server1 ~]# rpm -qf /usr/bin/test

coreutils-8.22-21.el7.x86_64

[root@server1 ~]# rpm -qf /usr/bin/[

coreutils-8.22-21.el7.x86_64

[root@server1 ~]# v1=1

[root@server1 ~]# v2=2

[root@server1 ~]# [ "$v1" -ne "$v2" ] && echo "Not equal" (-ne 不等于)

Not equal

二、 实例分析

1.条件测试结构   Test Construects

1.1 fi test condition -true 与 if [ condition-true ] 相同

if [ condition-true ]

then

  command 1

  command 2

  …

else

  command 1

  command 2

  …

fi

if [ -x “$filename” ] ; then

if : ; then echo “ok”; fi

1.2 [ … ]对格式有严格要求:

 1.2.1 [ ]中每个组件都需要用空格符分隔

 1.2.2 [ ]中的变量和常量最好都用双引号引起来

1.3 [[…]]结构比[…]结构更加通用

1.4 命令true至此的类型:

 1.4.1 数值比较

 1.4.2 字符串比较

 1.4.3 文件比较

#!/bin/bash
# if命令还可以对命令进行测试,并是仅仅是中括号中的条件

#比较两个文件是否一样
date >testfile1.txt
date >testfile2.txe
if cmp testfile1.txt testfile2.txt &> /dev/null #抑制输出
  then echo "Flies are indetical"
  else echo "Flies are differ"
fi
#在grep中,很有用
if grep -q CST testfile1.txt
  then echo "File contains at lease one occrence of CST"
fi
#复杂一些的
word=Linux
letter_swquence=inu
if echo "$word" | grep -q "$letter_sequence"
then
 echo "$lteter_sequence found in $word"
else
 echo "$lteter_sequence not  found in $word"
fi
rm testfile[12].txt
exit 0

#!/bin/bash
# 0是真
echo "Ttesting "0""
if [ 0 ]
then
  echo "0 is ture"
else
  echo "0 is false"
fi
echo 

# 1是真 !!!
echo "Testing "1""
if [ 1 ]
then
 echo "1 is ture"
else
 echo "1 is false"
fi
echo 

# -1是真!!!
echo "Testing "-1""
if [ -1 ]
then
 echo "-1 is ture"
else
 echo "-1 is false"
fi
echo 

# NULL是假
echo "Testing "NULL""
if [ ] # NULL 空状态
then
  echo "NULL is ture"
else
  echo "NULL is false"
fi
echo 
#随意字符是真
echo "Testing "sxz""
if [ sxz ]
then
 echo "Radom string is ture"
else
 echo "Radom string is false"
fi
echo 
# 未初始化的变量是假
echo "Testing "$UninitVar""
if [ $UninitVar ]
# if [ -n $UninitVar ] #这样更加规范的写法
then
 echo "UninitVar variable is ture"
else
 echo "UninitVar variable is false"
fi
echo

三、文件测试与整数比较

1.文件测试符  File test operators

-e

文件存在

-a

与-e相同,已被“弃用”了

-f

表示是一个普通文件(并不是目录或者设备文件)

-s

文件大小不为零

-d

表示这是一个目录

-b

表示这是一个块设备(软盘,光驱等)

-c

表示这是一个字符设备(键盘,声卡等)

-p

这个文件是个管道

-h

这是一个符号链接

-L

这是一个符号链接

-s

表示这是一个socket

-t

文件(描述符)被关联到一个终端设备上

-r

当前用户是否具有可读权限

-w

当前用户是否具有可写权限

-x

当前用户是否具有可执行权限

-g

Set-group-id(sgid)标记被设置到文件或目录上

-u

Set-user-id(suid)标记被设置到文件上

-k

设置粘贴位

-o

当前用户是否是文件的用有者

-G

文件的group-id是否与当前用户

-N

从上一次被读取到现在为止,文件是否被修改过

f1 -nt f2

文件f1比文件f2新

  f1 -ot f2

文件f1比文件f2旧

f1 -ef f2

文件f1比文件f2是相同的硬链接

#!/bin/bash
# 检查文件是否存在
echo -e "Enter the name of the file  : c"
read file_name
if [ ! -f $file_name ]
then
 :
else
 echo "$file_name not exists"
fi
exit 0

 

 2.整数比较操作符  Integer Comparison

2.1 在中括号中使用 square bracket

-eq

equal

等于

if [ “$a” -eq “$b” ]

-ne

not equal

不等于

if [ “$a” -ne “$b” ]

-gt

greater than

大于

if [ “$a” -gt “$b” ]

-ge

greater  then or equal

大于等于

if [ “$a” -ge “$b” ]

-lt

less  then

下于

if [ “$a” -lt “$b” ]

-le

less than or equal

下于等于

if [ “$a” -le “$b” ]

2.2 在双圆括号中使用 Double-Parenthess

<

下于

if ((“$a” < “$b” ))

<=

下于等于

if ((“$a” < = “$b” ))

>

大于

if ((“$a”  > “$b” ))

>=

大于等于

if ((“$a” >= “$b” ))

3.字符串比较符  String Comparion

操作符

含义

示例

备注

=

等于

if [ “$a” = “$b” ]

==

等于

if [ “$a” == “$b” ]

与=等价

!=

不等于

if [ “$a” != “$b” ]

<

小于

if [ “$a” < “$b” ]

if [“$a” < “$b”

在[ ]结构中的,需要被转义

>

大于

if [ “$a” >“$b” ]

if [ “$a” /> “$b” ]

在[ ]结构中的,需要被转义

-z

长度为零

if [ -z “$a” ]

字符串长度为零

-n

长度不为零

if [ -n “$a” ]

str

非空

if [ $a]

检查字符串str是否非空字符串

注:1.<和>,按照ASCII字进行排序

    2.建议把字符串用双引号引用起来

#!/bin/bash
a=4
b=5
if [ "$a" -ne "$b" ]
then
  echo "$a is not equal to $b"
  echo "(arithmetic comparison)"
fi
echo 

if [ "$a" != "$b" ]
then
  echo [ "$a is not equal to $b" ]
  echo "(string comparison)"
fi

#!/bin/bash
# 编程时,强烈建议对字符串引用起来!
# 本脚本进行验证
# 例1:-n 对未声明的变量,采用引用
if [ -n "$string1" ]
then
  echo "$string1 "string1" is not null "
else
  echo "$string1 "string1" is null "
fi
# 会输出:string1 "string1" is null 
echo 
#例2:-n对未声明的变量,没有引用
if [ -n $string2 ]
then
  echo "$string2 "string2" is not null "
else
  echo "$string2 "string2" is null "
fi
# 会输出:string1 "string1" is not  null  
echo 
#例3:-n 变量引用,对未声明的变量,采用引用
if [ "$string3" ]
then
  echo "$string "string3" is not null "
else
  echo "$string "string3" is null "
fi
# 会输出:string "string3" is null 
echo 
#例4:-n 变量引用,对为声明的变量,没有引用
if [ $string4 ]
then
  echo "$string "string4" is not null "
else
  echo "$string "string4" is null "
fi
# 会输出:string4 "string4"  is   null 
echo 
#例5:变量引用,已声明并赋值的变量,采用引用
string5=Initialized
if [ "$string5" ]
then
  echo "$string "string5" is not null "
else
  echo "$string "string5" is null "
fi
# 会输出:string "string5" is not  null 
echo 
#例6:变量引用,已声明并赋值的变量,不采用引用
string6=Initialized
if [ "$string6" ]
then
  echo "$string "string6" is not null "
else
  echo "$string "string6" is null "
fi
# 会输出:string "string6" is not  null 
echo 
#例7:一个特殊的字符串,如果不进行引用
string7="a = b"
if [ $string7 ]
then
  echo "$string "string7" is not null "
else
  echo "$string "string7" is null "
fi
# 会输出:string "string7" is   null 
echo 

exit 0

#!/bin/bash
# 使用more来查看gzip文件
# 定义错误退出代码
NOARGS=65
NOTFOUND=66
NOTGZIP=67
# 判断是否传递参数
if [ $# -eq 0 ]
then
  echo "Usage: `basename $0` filename " >&2
  exit $NOARGS
fi

filename=$1
if [ ! -f "$filename" ]
then
  echo "File $filename no found !" >&2
  exit $NOTFOUND
fi

# 检查文件 “扩展名”
# 其实,这不是最交的判断是否zip格式的方法
if [ ${filename##*.} != "gz" ]
then
 echo "File $1 is not a gzipped file !"
 exit $NOTZIP
fi

zcat $1 | more
exit 0

 Tip:文件名(testtext,txt.gz)是否是以.gz 结尾的?

方法1:

  从最右边开始,取3个字符,判断是否是.gz

方法2:

  参数替换

  $ {filename##*.}

  语法是:

  ${variable#pattern} 匹配最短

  ${variable##pattern} 匹配最长

 四、组合比较与测试

1.组合比较  Compound Comparison

1.1通过布尔操作符来比较

 1.1.1 -a 逻辑与

 [ expl -a pxp2 ]

1.1.2 -o 逻辑或

 [ exp1 -o exp2 ]

1.2通过比较操作符来组合比较

1.2.1 && 逻辑与

 [ exp1 && exp2 ]

1.2.2 || 逻辑或

 [ epx1 || exp2]

#!/bin/bash
a=24
b=47
# 1 而且 &&
if [ "$a" -eq 24 ] && [ "$b" -eq 47 ]
then
 echo "Test #1 succeeds."
else
 echo "Test #1 fails."
fi
#如果这样写,会出错
#if [ "$a" -eq 24 && “$b" -eq 47 ]
#如果这样写,没有问题
#if [[ "$a" -eq 24 && "$b" -eq 47 ]]
# 2 或者 ||
if [ "$a" -eq 88 ] || [ "$b" -eq 47 ]
then
 echo "Test #2 succeeds."
else
 echo "Test #2 fails."
fi
# 3 而且 -a 
if [ "$a" -eq 24 -a  "$b" -eq 47 ]
then
 echo "Test #3 succeeds."
else
 echo "Test #3 fails."
fi
# 4 或者 -o
if [ "$a" -eq 88 -o "$b" -eq 47 ]
then
 echo "Test #4 succeeds."
else
 echo "Test #4 fails."
fi
# 5 字符串比较,而且 &&
a=aaaa
b=bbbb
if [ "$a" = aaaa ] && [ "$b" = bbbb ]
then
 echo "Test #5 succeeds."
else
 echo "Test #5 fails."
fi

exit 0

 

2.嵌套的if条件测试  Nested if Confition Teste

if [ exp1 ]
then 
 if [ exp2 ]
 then 
  dosomething # 仅当exp1 和exp2均满足时
 fi
fi

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/963570.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号