if命令小结

人海茫茫,相见即缘

if语句

在shell程序编写中,条件判断是不可缺少的一环,if是执行条件判断的主力命令,通过if,可以实现对数字,字符串和文件的各种条件判断。

一、if的基本语法

if的基本语法如下:

if [ condition ];#或者if [[ condition ]
then
commands;
fi

if [ condition ];
then
commands;
elif [ condition ];
then
    commands;
else 
    commands;
fi

比如:

[root@redhatclient ~]# a=1
[root@redhatclient ~]# if [[ $a -gt 0 ]]; then echo $a; fi
1

这是最简单的if数值判断,还有稍微复杂一点点的:

[root@redhatclient ~]# a=1
[root@redhatclient ~]# if [ $a -gt 1];
> then
> echo "a is gt than 1";
> elif [ $a -eq 1];
> then
> echo "a is eq to 1";
> fi
-bash: [: missing `]'
-bash: [: missing `]'
[root@redhatclient ~]# if [ $a -gt 1 ]; then echo "a is gt than 1"; elif [ $a -eq 1 ]; then echo "a is eq to 1"; fi
a is eq to 1
[root@redhatclient ~]# 

可以看到,首先语法和其它语言大致相同,其次第一次执行这个命令的时候会报错,是因为在第一次的命令中,右中括号前面忘记打空格了。在if后面使用中括号,不管是单中括号还是双中括号,左中括号的右面和右中括号的左面都需要一个空格。

二、使用if进行数值比较(整数)

使用if进行数值比较,不能时候用>,<,=等比较符,需要时候用以下参数进行数值比较:

参数含义
-eq(equal)测试两个数是否相等
-ne(not equal)测试两个数是否不相等
-gt(greater than)测试-gt前的数是否大于后面的数
-lt(little than)测试-lt前的数是否小于后面的数
-ge(greater or equal)测试-ge前的数是否大于或者等于后面的数
-le(little or equal)测试-le前面的数是否小于或者等于后面的数
[root@redhatclient ~]# a=1
[root@redhatclient ~]# b=1
[root@redhatclient ~]# if [ $a -eq $b ]; then echo "a eq b "; else echo "a not eq b"; fi
a eq b 
[root@redhatclient ~]# 

三、使用if进行字符串比较

使用if进行字符串比较,需要用到的参数如下:

参数含义
-z str1测试字符串str1的长度是否为0,长度为零则为真
-n str1测试字符串str1 的长度是否不为0,不为0则为真
str1 = str2str1与str2相同则为真,str与等号之间可以有空格也可以没有空格
str1 != str2两个字符串不相同则为真
str1字符不为空则为真,与-n相同

-z和-n的用法:

[root@redhatclient ~]# a="linux"
[root@redhatclient ~]# if [ -z $a ]
> then
> echo "a is null"
> fi
[root@redhatclient ~]# a=""
[root@redhatclient ~]# if [ -z $a ]; then echo "a is null"; fi
a is null
[root@redhatclient ~]# 
[root@redhatclient ~]# a="linux"
[root@redhatclient ~]# if [ -n $a ]; then echo "a is not  null"; fi
a is not  null
[root@redhatclient ~]# 

=的用法

[root@redhatclient ~]# a=linux
[root@redhatclient ~]# b=linux
[root@redhatclient ~]# if [ $a = $b ];then echo "a is equal to b";fi
a is equal to b
[root@redhatclient ~]# 
[root@redhatclient ~]# if [ $a=$b ];then echo "a is equal to b";fi
a is equal to b

四、使用if进行文件夹和文件的判断

参数含义
-e file测试file是否存在
-f file测试file是否为普通文件
-d dir测试dir是否为目录
-r file测试file是否对当前用户可读
-w file测试file是否对当前用户可写
-x file测试file是否对当前用户可执行
-b file测试file是否为块文件
-c file测试file是否为字符文件
-L file测试file是否为符号链接文件

相关示例

drwxr-xr-x   2 root root  4096 Feb 21 01:49 test
drwxr-xr-x   3 root root  4096 Dec 13 16:21 .veritas
drwxr-xr-x.  2 root root  4096 Dec 13 03:22 Videos
-rw-------   1 root root   116 Feb  1 01:17 .Xauthority
-rw-------   1 root root  3047 Feb  7 23:03 .xsession-errors
-rw-------   1 root root  2545 Jan  9 04:08 .xsession-errors.old
[root@redhatclient ~]# if [ -d ./test ];then echo "test is a directory";fi
test is a directory
[root@redhatclient ~]# 

-f的使用

[root@redhatclient test]# ls -al
total 184
drwxr-xr-x   2 root root   4096 Feb 21 01:49 .
dr-xr-x---. 27 root root   4096 Feb 16 06:41 ..
-rw-r--r--   1 root root      0 Feb 21 01:49 1
-rw-r--r--   1 root root      0 Feb 16 06:49 1.txt
-rw-r--r--   1 root root 102400 Feb 22 05:27 a
-rwxr--r--   1 root root  26486 Feb  8 23:30 check
-rw-r--r--   1 root root     48 Feb 15 00:40 f1
-rw-r--r--   1 root root    121 Jan 31 18:19 log
-rw-r--r--   1 root root  26318 Feb  4 18:56 output.session
-rw-r--r--   1 root root   1024 Feb  5 23:12 t1.data
-rwxr--r--   1 root root     45 Feb  7 22:17 test
-rw-r--r--   1 root root   1115 Feb  4 18:56 timing.log
[root@redhatclient test]# if [ -f check ];then echo "check is a file";fi
check is a file
[root@redhatclient test]# 

-x的使用

[root@redhatclient test]# if [ -x check ];then echo "check is executable";fi
check is executable
[root@redhatclient test]# 

-e的用法

[root@redhatclient test]# if [ -e f1 ]
> then
> echo "fi exists";
> else
> echo "fi doesn't exist";
> fi
fi exists
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值