【shell】条件判断式
2016-06-20 11:03
465 查看
条件判断式的表示格式:
文件判断式:
文件权限判断式:
文件比较判断式:
整数比较判断式:
字符串判断式:
多重条件判断式:
文件判断式:
[root@andon ~]# [ -e /root/1 ] && echo yes || echo no #注意[]里面的空格,第一个命令为真打印yes,否则打印no yes [root@andon ~]# [ -f /root/1 ] && echo yes || echo no yes [root@andon ~]# [ -d /root/1 ] && echo yes || echo no no
文件权限判断式:
[root@andon ~]# [ -r /root/1 ] && echo yes || echo no yes [root@andon ~]# [ -x /root/1 ] && echo yes || echo no no [root@andon ~]# [ -w /root/1 ] && echo yes || echo no yes
文件比较判断式:
[root@andon ~]# [ /root/1 -nt /root/2 ] && echo yes || echo no no [root@andon ~]# [ /root/1 -ot /root/2 ] && echo yes || echo no yes [root@andon ~]# [ /root/1 -ef /root/2 ] && echo yes || echo no no [root@andon ~]# ln 1 3 [root@andon ~]# [ /root/1 -ef /root/3 ] && echo yes || echo no yes
整数比较判断式:
[root@andon ~]# [ 4 -eq 4 ] && echo yes || echo no #等于 yes [root@andon ~]# [ 4 -ne 4 ] && echo yes || echo no ##不等于 no [root@andon ~]# [ 5 -gt 4 ] && echo yes || echo no ##大于 yes [root@andon ~]# [ 5 -lt 4 ] && echo yes || echo no ##小于 no [root@andon ~]# [ 5 -ge 4 ] && echo yes || echo no ##大于等于 yes [root@andon ~]# [ 5 -le 4 ] && echo yes || echo no ##小于等于 no
字符串判断式:
[root@andon ~]# [ -z $a ] && echo yes || echo no ##判断为空 no [root@andon ~]# [ -n $a ] && echo yes || echo no ##判断非空 yes [root@andon ~]# b=abc [root@andon ~]# [ $a == $b ] && echo yes || echo no yes [root@andon ~]# [ $a != $b ] && echo yes || echo no no
多重条件判断式:
[root@andon ~]# [ -n $b -a -z $a ] && echo yes || echo no ##与 no [root@andon ~]# [ -n $b -o -z $a ] && echo yes || echo no ##或 yes
相关文章推荐
- How to access HBase from spark-shell using YARN as the master on CDH 5.3 and Spark 1.2
- hdfs shell 命令
- Windows 7上执行Cake 报错原因是Powershell 版本问题
- powershell1.0简体中文版下载
- WinExec,ShellExecute ,CreateProcess 区别
- 编写"优美"的SHELLCODE
- Shell的命令行参数
- shell下的基础程序题练习
- [shell基础]——read命令
- shell脚本:练习题
- Shell 脚本知识回顾 (一) —— 基础篇
- [shell基础]——echo命令
- Shell 脚本知识回顾 (二) —— Shell变量
- Shell基本知识
- Shell 脚本知识回顾 (三) —— 替换、运算符、字符串、数组
- Shell 脚本知识回顾 (四) —— Shell 命令及Shell 相关语句
- Shell 脚本知识回顾 (五) —— Shell 循环
- Shell 脚本知识回顾 (六) —— Shell 函数
- Shell 脚本中如何使用make命令
- linux shell 流程控制