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. Delphi的手机程序隐藏顶部信号栏

    把TForm的BorderStyle设置为None 记之!

  2. google-chrome 启动报错 nss_util.cc(627)] NSS_VersionCheck("3.26") failed. NSS >= 3.26 is required

    一.错误情况 报错如下: [0807/144244.712736:FATAL:nss_util.cc(627)] NSS_VersionCheck("3.26") failed. ...

  3. for循环中创建线程执行问题

    先执行以一个简单的示例: static void Main(string[] args) { List<int> taskConsumes = new List<int>() ...

  4. Maven快速入门(五)Maven的依赖管理

    前面我们讲了maven项目中的最重要的文件:pom.xml 配置文件相关内容.介绍了pom 是如何定义项目,如何添加依赖的jar 包的等. 我们知道,在Maven的生命周期中,存在编译.测试.运行等过 ...

  5. tomcat9启动报错too low setting for -Xss

    在tomcat下部署war包启动时报错,关键错误信息如下: Caused by: java.lang.IllegalStateException: Unable to complete the sca ...

  6. MAC电脑如何将常规视频中音频提取出来(转换格式并调整采样频率),并利用讯飞语音识别文字

    1.下载好相关视频 2.选中需要提取视频,鼠标右键找到「编码所选视频文件」 3.设置中,下拉选择「仅音频」,点击继续 4.找到已提取成功的音频,鼠标右键或快捷键「command + I」,显示简介.默 ...

  7. 大一C语言学习笔记(5)---函数篇-定义函数需要了解注意的地方;定义函数的易错点;详细说明函数的每个组合部分的功能及注意事项

    博主学习C语言是通过B站上的<郝斌C语言自学教程>,对于C语言初学者来说,我认为郝斌真的是在全网C语言学习课程中讲的最全面,到位的一个,这个不是真不是博主我吹他哈,大家可以去B站去看看,C ...

  8. Chrome handless无界面浏览器的脚本操作

    1.什么是Phantomjs (已经停止更新) 是一个无界面的浏览器 支持页面元素查找,js的执行等 由于不进行css和gui渲染,运行效率要比真实的浏览器要快很多 2.如何使用Phantomjs? ...

  9. 【linux系统】命令学习(四)统计命令

    sort 排序 -b 忽略开头的空白符 -f 将小写字母看做为大写字母 -h 根据存储容量排序(kb mb hb )从小到大 -n 按数字排序 -o 把结果写入文件 -r 以相反的顺序来排序 -t 指 ...

  10. ELK 7.4.2 单机安装配置

    Java环境准备 JDK下载 https://www.oracle.com/technetwork/java/javase/overview/index.html [root@manager ~]# ...