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. 『与善仁』Appium基础 — 5、常用ADB命令(二)

    目录 9.查看手机运行日志 (1)Android 日志 (2)按级别过滤日志 (3)按 tag 和级别过滤日志 (4)日志格式 (5)清空日志 10.获取APP的包名和启动名 方式一: 方式二: 11 ...

  2. 【编译原理】LL1文法语法分析器

    上篇文章[编译原理]语法分析--自上向下分析 分析了LL1语法,文章最后说给出栗子,现在补上去. 说明: 这个语法分析器是利用LL1分析方法实现的. 预测分析表和终结符以及非终结符都是针对一个特定文法 ...

  3. 【python】使用python十分钟创建个人聊天机器人教程

    以青云客和图灵机器人接口示范python创建个人聊天机器人教程 一.以青云客聊天机器人为例示范get请求 官方网址:http://api.qingyunke.com/ 1.接入指引 请求地址 http ...

  4. js--数组的 Array.of() 和 Array.from() 方法的使用总结

    前言 JavaScript 中数组的本质是一个对象,它存在的 length 属性值随数组元素的长度变化,但是开发中经常会遇到拥有 length 属性和若干索引属性的对象,被称为类数组对象,类数组对象和 ...

  5. vue中axios的post和get请求示例

    POST请求 methods: { isclick() { if (this.account == "" || this.pwd == "") { this.$ ...

  6. 开发规范 - UML图

    依赖关系是用一套带箭头的虚线表示,他通常描述一个对象在运行期间会用到另一个对象的关系.如图为例码农只有在工作的时候才会用到 Mac 电脑,所以这种依赖关系是依赖于运行状态的.通常情况下是在程序里面通过 ...

  7. Python基础(@property)

    class Point(object): # def get_score(self): # return self.score # def set_score(self,value): # if no ...

  8. Mac下查看 Java 安装目录位置和安装数量

    /usr/libexec/java_home -V 第一个红框是安装数量, 第二个红框是目前正在使用的 JDK 版本位置

  9. CTF入门学习2->Web基础了解

    Web安全基础 00 Web介绍 00-00 Web本意是网,这里多指万维网(World Wide Web),是由许多互相连接的超文本系统组成的,通过互联网访问. Web是非常广泛的互联网应用,每天都 ...

  10. 利用opencv进行简易的拍照并处理照片

    今天用python写了一个调用摄像头拍照并对图片进行素描化或动漫化的小demo. 首先我的环境是:PyCharm+python3.8+opencv-python(4.4.0.42) 我们分析一下思路, ...