shell条件判断

shell条件判断(流程控制的执行由判断条件的执行结果决定)
分类:
文件状态:
-e:文件(可以是文件也可以是文件夹)是否存在
-d:是不是目录
-f:是不是文件
-w:
-r:
-x:
-L:是不是链接

字符比较:
   =
   != [ str1 != str2 ] 注意空格的使用。
   -z
   ! -z
数值比较:
    -eq
   -ne
   -gt
   -ge

-lt
-le
逻辑比较:

命令:
test:
man test
[ 选项 对象 ] :注意两边必须要有空格
[root@iLor scripts]# [ -f 1.jpg]
-bash: [: missing `]'
[root@iLor scripts]# [-f 1.jpg ]
-bash: [-f: command not found
条件判断返回结果(echo $?)只有2种:0(真)或者非0(假)

read的使用:
read命令接收标准输入(键盘)的输入,或其他文件描述符的输入(后面在说)。得到输入后,read命令将数据放入一个标准变量中。
语法:
read –p “” arguments

数值比较:(整数比较)
int1 选项 int2
-eq:等于
-ne:不等于
-gt:大于
-ge:大于等于
-lt:小于
-le:小于等于
逻辑比较:(2个或2个以上判断条件时使用逻辑比较)
逻辑与(and) -a && 多个条件同时成立
逻辑或(or) -o|| 只要有一个条件成立就可以

       例:
       [root@iLor shell_day01_am]# [ 6 -gt 5 ] && [ 4 -lt 8 ]

[root@iLor shell_day01_am]# echo ?  0  [root@iLor shell_day01_am]# [ 6 -gt 5 ] && [ 4 -lt 3 ]  [root@iLor shell_day01_am]# echo?
1
[root@iLor shell_day01_am]# [ 6 -gt 5 ] || [ 4 -lt 3 ]
[root@iLor shell_day01_am]# echo ?  0  [root@iLor shell_day01_am]# [ 6 -gt 5 -o 4 -lt 3 ]  [root@iLor shell_day01_am]# echo?
0
注意使用-a –o与&& ||的区别
逻辑非 !
= !=
-z ! –z





posted @ 2019-01-30 17:27  whatislinux  阅读(103)  评论(0编辑  收藏  举报