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不支持。

最新文章

  1. Python’s SQLAlchemy vs Other ORMs[转发 7] 比较结论
  2. 机器码call和jmp地址的计算
  3. jquery-ui 之Sortable详解
  4. 图片缩放时java.lang.IllegalArgumentException: pointerIndex out of range解决方案
  5. java 简单的文件上传
  6. node.js(三)url处理
  7. flask开发表单
  8. 分享一个公众号h5裂变吸粉源码工具
  9. Typescript学习总结之接口
  10. FZU 2277 Change(dfs序+树状数组)
  11. Mac破解Sublime Text 3 3176
  12. sed匹配多行替换
  13. php的静态化
  14. 2018.09.19 atcoder AtCoDeer and Election Report(贪心)
  15. Django思维导图
  16. 微信小程序信息展示
  17. (转)fiddler模拟post请求
  18. Spring------Spring boot data jpa的使用方法
  19. [转]java中Collections.sort排序详解
  20. Socket连接何时需要断开

热门文章

  1. 『学了就忘』Linux基础命令 — 20、文件操作的相关命令
  2. Canvas 放烟花合集 -- 用粉丝头像做成烟花绽放&#129512;
  3. 官宣 .NET RC 2
  4. C++ STL的一些应用
  5. C++中简单使用HP-Socket
  6. Flink 实践教程:入门(6):读取 PG 数据写入 ClickHouse
  7. SpringCloud微服务实战——搭建企业级开发框架(十九):Gateway使用knife4j聚合微服务文档
  8. Maven 依赖调解源码解析(三):传递依赖,路径最近者优先
  9. int,double与机器字长
  10. [nowcoder5668I]Sorting the Array