语法格式

if command;then

  commands

fi

其中的command包含如下:

shell command

任何shell命令,如果shell命令返回0,代表true,否则,代表false。并且多个command可以同时作为if的判断条件,即可以写为:

  if command1;command2;command3;then

    commands

  fi

此时如果command3返回的结果是0,则运行commands,否则不运行,虽然command1和command2也会运行,但是它们运行的返回结果没有任何影响

 test condition 或者 [ condition ]

两者等价,其中的condition可以是数字比较,可以是字符串比较,可以是文件比较:

数字比较

num1 eq num2 等于

num1 ne num2 不等于

num1 gt num2 大于

num1 ge num2 大于等于

num1 lt num2 小于

num1 le num2 小于等于

字符串比较

str1 = str2 等于

str1 != str2 不等于

str1 > str2 大于

str1 < str2 小于

-n str str的长度是否大于0

-z str str的长度是否为0

文件比较

-d file file是否为目录

-f file  file是否为文件

-e file file是否存在

-s file file存在并且不为空

-r file file存在且可读

-w file file存在且可写

-x file file存在且可以执行

-O file file存在并且属于当前用户

-G file file存在并且属于当前用户所在的initial group(也叫default group)

file1 -nt file2 file1是否比file2新

file1 -ot file2 file1是否比file2旧

condition也可以复合的,通过&& 或者 || 连接:

[ condition1 ] && [ condition2]

[ condition1 ] || [condition2 ]

shell中的if语句的更多相关文章

  1. Shell中的循环语句实例

    1.for循环语句实例1.1 最基本的for循环 #!/bin/bash for x in one two three four do     echo number $x done 注:" ...

  2. shell中的比较语句

    Linux比较字符串.判断文件是否存在及是否可读等,通常用"[]"来表示条件测试. 注意:这里的空格很重要.要确保方括号的空格.笔者就曾因为空格缺少或位置不对,而浪费好多宝贵的时间 ...

  3. shell中的循环语句

    for语法格式 for var in list;do commands done 其中list可以包含: 1) 直接写 for alpha in a b c d;do echo $alpha done ...

  4. Shell中常用的语句

    exit 完全中断脚本的执行 break 中断脚本的循环,但是会执行循环外的语句 continue 跳出本次循环,进行下一次循环 进一步了解三者的区别,有如下实验: 执行该脚本: 脚本正常运行情况: ...

  5. shell中的case语句

    case语法: case $arg in arg1) 语句1 ;; arg2) 语句2 ;; *) help 语句 ;; esac eg: eg:

  6. shell中的循环语句while

    循环语句的结构: ------------| while 条件        | do | 需要执行的命令   | done  | -----------| 例如: 1.while一直循环 2.whi ...

  7. shell 中 if then语句中会跟着-ne -ge之类的参数的含义

    if [ 1 -ne 1 ];then...fi这是指当1不等于1时执行then后的语句 -eq:等于-ne:不等于-le:小于等于-ge:大于等于-lt:小于-gt:大于

  8. shell中的判断语句

    1.字符串判断 str1 = str2 当两个串有相同内容.长度时为真 str1 != str2 当串str1和str2不等时为真 -n str1 当串的长度大于0时为真(串非空,变量) -z str ...

  9. shell 中的if语句

    if [ t != "." -a t != ".." ] 之前一直不知道 -a 是什么意思,后来才知道     -a = and  ;    -o = or

随机推荐

  1. 提升PHP速度的53个建议

      1.如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. 2.$row[’id’] 的速度是$row[id]的7倍. 3.echo 比 print 快,并且使用e ...

  2. C# 读取EXCEL数据

       /// <summary> /// 读取EXCEL数据 /// </summary> /// <param name="Path">< ...

  3. PHP程序员衰老后的下场

    长期从事编程活动的程序员都期望在50多岁时能爬到一个足够高的职位,或者能顺利的退休. 但我在这里讨论的可能是一个你还没有想过的问题:如果到那时你失业了呢? 50多岁时你的职业仕途会成为一个问题.如果你 ...

  4. java练习-滚动文字

    <marquee direction="left" onMouseOver="this.scrollAmount=5" onMouseOut=" ...

  5. Ubuntu 14.04 标题栏实时显示上下行网速、CPU及内存使用情况

    首先当然是用wget下载indicator-sysmonitor,终端执行命令:wget -c https://launchpad.net/indicator-sysmonitor/trunk/4.0 ...

  6. 测试一下PHP官方的新一代PHP加速插件ZendOpcache的性能及配置

    过程不表,都比较顺利 参考如下URL: http://www.lvtao.net/server/ZendOpcache.html 大家知道目前PHP的缓存插件一般有三个:APC.eAccelerato ...

  7. PYTHON线程知识再研习D---可重入锁

    不多解释,预防普通锁不正规的获取与释放 #!/usr/bin/env python # -*- coding: utf-8 -*- import threading import time class ...

  8. 如何将android studio项目转换成eclipse

    更新:虽然本人坚守eclipse很久,但是现在我也不再推荐继续用eclispe了,自己的项目用还没什么问题,但是你如果想用github上的项目,用eclispe会越来越难.如果你仍然感兴趣,继续看下面 ...

  9. 开机启动tomcat

    windows: 成功之后在dos窗口键入 service.bat install Tomcat 输完然后按Enter键,出现如下窗口,便成功了. 进入windows服务管理,设成是自动的. #chk ...

  10. Linux企业级项目实践之网络爬虫(23)——系统测试:找出系统中的bug

    为了验证爬虫的业务流程.性能和健壮性需要进行测试. 软件测试是描述一种用来促进鉴定软件的正确性.完整性.安全性和质量的过程.软件测试的经典定义是:在规定的条件下对程序进行操作,以发现程序错误,衡量软件 ...