http://bbs.chinaunix.net/thread-396805-1-1.html

shell 判断语句

流程控制 "if" 表达式 如果条件为真则执行then后面的部分:

if ....; then   .... elif ....; then   .... else   .... fi 大多数情况下,可以使用测试命令来对条件进行测试。比如可以比较字符串、判断文件是否存在及是否可读等等…   

通常用" [ ] "来表示条件测试。注意这里的空格很重要。要确保方括号的空格。

[ -f "somefile" ] :判断是否是一个文件

[ -x "/bin/ls" ] :判断/bin/ls是否存在并有可执行权限

[ -n "$var" ] :判断$var变量是否有值

[ "$a" = "$b" ] :判断$a和$b是否相等

-r file     用户可读为真   

-w file     用户可写为真   

-x file     用户可执行为真   

-f file     文件为正规文件为真   

-d file     文件为目录为真   

-c file     文件为字符特殊文件为真   

-b file     文件为块特殊文件为真   

-s file     文件大小非0时为真   

-t file     当文件描述符(默认为1)指定的设备为终端时为真

-n variable      判断一个变量是否有值

-z variable      判断一个变量是否为非空字符串

#########################################################

含条件选择的shell脚本 对于不含变量的任务简单shell脚本一般能胜任。但在执行一些决策任务时,就需要包含if/then的条件判断了。shell脚本编程支持此类运算,包括比较运算、判断文件是否存在等。 基本的if条件命令选项有: -eq —比较两个参数是否相等(例如,if [ 2 –eq 5 ]) -ne —比较两个参数是否不相等 -lt —参数1是否小于参数2 -le —参数1是否小于等于参数2 -gt —参数1是否大于参数2 -ge —参数1是否大于等于参数2 -f — 检查某文件是否存在(例如,if [ -f "filename" ]) -d — 检查目录是否存在 几乎所有的判断都可以用这些比较运算符实现。脚本中常用-f命令选项在执行某一文件之前检查它是否存在。 ################################################################# # 判断文件是否存在 #!/bin/sh today=`date -d yesterday +%y%m%d` file="apache_$today.tar.gz" cd /home/chenshuo/shell if [ -f "$file" ];then echo "OK" else echo "error $file" >error.log mail -s "fail backup from test" chenshuo@soufun.com <error.log fi =================================================

1.shell判断文件,目录是否存在或者具有权限    2.  #!/bin/sh    3.   4.myPath= "/var/log/httpd/"    5.myFile= "/var /log/httpd/access.log"    6.   7.#这里的-x 参数判断$myPath是否存在并且是否具有可执行权限    8.if  [ ! -x  "$myPath" ]; then    9.mkdir  "$myPath"    10.fi    11.   12.#这里的-d 参数判断$myPath是否存在    13.if  [ ! -d  "$myPath" ]; then    14.mkdir  "$myPath"    15.fi    16.   17.#这里的-f参数判断$myFile是否存在    18.if  [ ! -f  "$myFile"  ]; then    19.touch  "$myFile"    20.fi    21.   22.#其他参数还有-n,-n是判断一个变量是否有值    23.if  [ ! -n  "$myVar"  ]; then    24.echo  "$myVar is empty"    25.exit  0    26.fi    27.   28.#两个变量判断是否相等    29.if  [  "$var1"  =  "$var2"  ]; then    30.echo  '$var1 eq $var2'    31.else    32.echo  '$var1 not eq $var2'    33.fi

/****************************************************************************/

1  判断一个变量是否被定义

if [ -z $EDITOR ]

2  判断交互模式

if [ -t ]

3  测试文件权限

if [ ! -w "$LOGFILE"]

4 测试SHELL命令

if echo $list | grep "Peter" > /dev/null 2>&1

5 测试数值

if [ "10" -lt "12" ]

* -b file = True if the file exists and is block special file.     如果该文件存在并且是块特殊文件。

* -c file = True if the file exists and is character special file.如果该文件存在并且是字符特殊文件

* -d file = True if the file exists and is a directory.   如果该文件存在并且是一个目录。

* -e file = True if the file exists.         如果该文件存在

* -f file = True if the file exists and is a regular file   如果该文件存在并且是一个普通文件

* -g file = True if the file exists and the set-group-id bit is set.   如果该文件存在并且设置了组ID位。

* -k file = True if the files’ “sticky” bit is set.    如果文件的sticky “粘性”位被设置。

* -L file = True if the file exists and is a symbolic link.   该文件存在并且是一个符号链接。

* -p file = True if the file exists and is a named pipe.   该文件存在并且是一个命名管道。

* -r file = True if the file exists and is readable.   文件存在并且是可读的

* -s file = True if the file exists and its size is greater than zero. 文件存在,它的大小是大于零

* -S file = True if the file exists and is a socket.     文件存在并且是一个套接字

* -t fd = True if the file descriptor is opened on a terminal.   文件描述符是在一个终端上打开的

* -u file = True if the file exists and its set-user-id bit is set. 文件存在,它的设置用户ID位被设置了

* -w file = True if the file exists and is writable.     文件存在并且可写

* -x file = True if the file exists and is executable.     文件存在并且是可执行的

* -O file = True if the file exists and is owned by the effective user id.    文件存在并且是所拥有的有效用户ID

* -G file = True if the file exists and is owned by the effective group id. 文件存在并且拥有有效的gruop id。

* file1 -nt file2 = True    if file1 is newer, by modification date, than file2.     如果file1更新

* file1 ot file2 = True    if file1 is older than file2.         如果file1更旧

* file1 ef file2 = True     if file1 and file2 have the same device and inode numbers.file1和file2有相同的设备和节点号

* -z string = True if the length of the string is 0.        字符串的长度为0

* -n string = True if the length of the string is non-zero.   字符串的长度不为零

* string1 = string2 = True if the strings are equal.

*string1 != string2 = True if the strings are not equal.

!expr = True if the expr evaluates to false.

* expr1 -a expr2 = True if both expr1 and expr2 are true.     且为真

* expr1 -o expr2 = True is either expr1 or expr2 is true.       或

两个档案之间的判断与比较 ;例如『 test file1 -nt file2 』     * -nt 第一个档案比第二个档案新     * -ot 第一个档案比第二个档案旧     * -ef 第一个档案与第二个档案为同一个档案( link 之类的档案)

逻辑的『和(and)』『或(or)』     * && 逻辑的 AND 的意思     * || 逻辑的 OR 的意思

运算符号 代表意义 = 等于 != 不等于 < 小于 > 大于 -eq 等于 -ne 不等于 -lt 小于 -gt 大于 -le 小于或等于 -ge 大于或等于 -a 双方都成立(and) -o 单方成立(or) -z 空字符串 -n 非空字符串

/************************************************************/

格式如下,在比较时,数字和字符串用不同的比较符号

1.如果a>b且a<c

if (( a > b )) && (( a < c ))      或者

if [[ $a > $b ]] && [[ $a < $c ]]

或者          if [ $a -gt $b -a $a -lt $c ]

2.如果a>b或a<c

if (( a > b )) || (( a < c ))              或者       if [[ $a > $b ]] || [[ $a < $c ]]

或者        if [ $a -gt $b -o $a -lt $c ]

3. -o = or , -a = and , 但我一向只用 || 或者 &&

4."||"和"&&"在SHELL里可以用吗?也就是第一个写成if [ a>b && a<c ]也可以吗?

可用, 但是要两个独立的 [ ] , [[ ]] 或 (( ))      看 1

5 -ne 比较数字 (numberic) ; != 比较字符 (string), 但后者拿来    比较数字也可,只是不是标准用法    -lt 是等同 < , 但 < 只能在 shell 的数值操作符 (( )) 或    者 逻缉操作符 [[ ]] 才可使用, -lt , -eq , -gt , -ge    -le , 这些是 test , 就是 [ ] 这个内建命令使用的条件操    作符, 数字使用; = , != 字符用, == 这个该是 [[ ]] 用的,    可用来比对正规表示式, 但用在 [ ] 也可,只是不太正统用法       /************************************************************/

test命令用法。功能:检查文件和比较值

1)判断表达式 if test  (表达式为真) if test !表达式为假

-----

2.  自增的写法
1. i=`expr $i + 1`;
2. let i+=1;
3. ((i++));                // not support by some shell.
4. i=$[$i+1];           //not support by some shell.
5. i=$(( $i + 1 ))
 
嵌入式平台上发现,3, 4不支持。

linux shell中的条件判断语句的更多相关文章

  1. shell中的条件判断以及与python中的对比

    shell中比如比较字符串.判断文件是否存在及是否可读等,通常用"[]"来表示条件测试. 注意:这里的空格很重要.要确保方括号的空格. if ....; then          ...

  2. shell编程2 ---条件判断语句

    shell编程2 一.条件判断语句 1.按照文件类型进行判断 eg:test -e /etc/passwd  或者  [ -e /etc/passwd ]  &&echo yes || ...

  3. shell中的条件判断if和测试

    (一)条件判断 if 中-z 到 -d 的意思 [ -a file ] 若file存在,则为真. [ -b file ] 若file存在且是一个块特殊文件,则为真. [ -c file ] 若file ...

  4. javascript中对条件判断语句的优化 分类: JavaScript 2015-06-07 09:54 832人阅读 评论(2) 收藏

    不管写什么程序,平时都会用到条件语句,如:if...else... switch这样的语句,来达到对条件的判断.下面看来一段代码: function abc(test){ if (test == 1) ...

  5. shell中的条件判断、参数以及变量替换

    文章转自: http://www.cnblogs.com/maxupeng/archive/2011/07/02/2096551.html 一.test命令 test命令是shell内部命令,它计算作 ...

  6. SQL中的条件判断语句(case when zhen if,ifnull)用法

    简介: case具有两种格式.简单case函数和case搜索函数.这两种方式,可以实现相同的功能.简单case函数的写法相对比较简洁,但是和case搜索函数相比,功能方面会有些限制,比如写判定式.还有 ...

  7. shell中的条件判断

    read命令 命令的语法: read [参数] 变量名 常用的参数如下: -t timeout 设定超时时间. -p prompt 设定提示信息,该提示信息将会显在光标前. 条件测试: 条件测试可以判 ...

  8. Shell编程中的条件判断(条件测试)

    Shell中的条件判断(测试)类型: 1) 整数测试 2) 字符测试 3) 文件测试 条件测试的表达式:        (注: expression 与 [] 之间空格不能省略) [ expressi ...

  9. linux Shell中常用的条件判断

    linux Shell中常用的条件判断 -b file            若文件存在且是一个块特殊文件,则为真 -c file            若文件存在且是一个字符特殊文件,则为真 -d ...

随机推荐

  1. 【lwip】04-内存管理

    目录 前言 4. 内存管理 4.1 内存分配策略 4.1.1 固定大小的内存块 4.1.2 可变大小分配 4.2 动态内存池(pool) 4.2.1 介绍 4.2.2 内存池的预处理 4.2.3 内存 ...

  2. Shell 脚本批量添加用户和用户密码

    #!/bin/bash#批量添加用户 设置密码for i in `seq 1 10`do if ! id user$i &> /dev/null then useradd user$i ...

  3. CentOS部署多台服务器JDK(shell脚本部署)

    部署7台新服务器的jdk,数量不算多,但也不打算一台一台的部署,写了个脚本执行 [ #!/bin/bash# JDK 安装包名jdk_packge="jdk-8u162-linux-x64. ...

  4. docker使用redis过程出现的问题记录

    第一次使用docker搭建成功了单机版redis,但在使用过程中,还是遇到了不少问题,故而先把这些问题记录下来,以防后面再出现会忘记. 目前,只是在docker中搭建了三个单机版的容器,打算先捣鼓一周 ...

  5. 『学了就忘』Linux基础命令 — 26、帮助命令

    目录 1.man命令 (1)man命令的快捷键 (2)man命令的帮助级别(了解即可) (3)man命令的使用 2.info命令 3.help命令 4.--help选项 1.man命令 man是最常见 ...

  6. dart系列之:dart语言中的函数

    目录 简介 函数的参数 main函数 匿名函数 闭包 函数的返回值 总结 简介 函数是所有编程语言都有的内容,不管是面向对象还是面向过程,函数都是非常重要的一部分.dart中的函数和java中的函数有 ...

  7. kafka删除topic中的数据,适用于比较高的版本

    server.properties中增加选项 delete.topic.enable=true 修改之后重启kafka 进入kafka目录,输入命令 bin/kafka-topics.sh --zoo ...

  8. Beyond compare 4.2.3 激活和秘钥

    安装完 Beyond Compare 4.2.3 以后.打开输入密匙是不是会跳到官网去?不用慌,我们只需要删除你安装Beyond Compare 4目录下的 BCUnrar.dll 文件,然后再打开就 ...

  9. 逐浪CMSv8.2发布-集成Node与Vue脚手架和PowerShell支持的新一代网站管理系统

    极速下载:https://www.z01.com/down/3713.shtml 楼倚霜树外,镜天无一毫. 南山与秋色,气势两相高. -(唐)杜牧 北京时间2020年10月20日:领先的CMS与web ...

  10. layui 如果调用 from 内嵌入的 iframe子页面方法

    (人笨,占时想法的办法,不要骂,不要骂,怕了怕了,想到别的会来改的) 父页面; <%@ page language="java" contentType="text ...