zoukankan      html  css  js  c++  java
  • 转 功能强大的shell:if条件语句

    原文 http://blog.zol.com.cn/2322/article_2321767.html

    功能强大的shell:if条件语句
    if语句测试条件,测试条件返回真(0)或假(1)后,可相应执行一系列语句。if语句结构对错误检查非常有用。其格式为:
    if 条件1
    then 命令1
    elif 条件2
    then 命令2
    else
    命令3
    fi

    –b 当file存在并且是块文件时返回真
    -c 当file存在并且是字符文件时返回真
    -d 当pathname存在并且是一个目录时返回真
    -e 当pathname指定的文件或目录存在时返回真
    -f 当file存在并且是正规文件时返回真
    -g 当由pathname指定的文件或目录存在并且设置了SGID位时返回为真
    -h 当file存在并且是符号链接文件时返回真,该选项在一些老系统上无效
    -k 当由pathname指定的文件或目录存在并且设置了“粘滞”位时返回真
    -p 当file存在并且是命令管道时返回为真
    -r 当由pathname指定的文件或目录存在并且可读时返回为真
    -s 当file存在文件大小大于0时返回真
    -u 当由pathname指定的文件或目录存在并且设置了SUID位时返回真
    -w 当由pathname指定的文件或目录存在并且可执行时返回真。一个目录为了它的内容被访问必然是可执行的。
    -o 当由pathname指定的文件或目录存在并且被子当前进程的有效用户ID所指定的用户拥有时返回真。
    UNIX Shell 里面比较字符写法:

    -eq   等于
    -ne    不等于
    -gt    大于
    -lt    小于
    -le    小于等于
    -ge   大于等于
    -z    空串
    =     两个字符相等
    !=    两个字符不等
    -n    非空串
    更为详细的说明:

    运算符                     描述                          示例

    文件比较运算符

    -e filename     如果 filename 存在,则为真            [ -e /var/log/syslog ]

    -d filename     如果 filename 为目录,则为真          [ -d /tmp/mydir ]

    -f filename     如果 filename 为常规文件,则为真      [ -f /usr/bin/grep ]

    -L filename     如果 filename 为符号链接,则为真      [ -L /usr/bin/grep ]

    -r filename     如果 filename 可读,则为真            [ -r /var/log/syslog ]

    -w filename     如果 filename 可写,则为真            [ -w /var/mytmp.txt ]

    -x filename     如果 filename 可执行,则为真          [ -L /usr/bin/grep ]

    filename1 -nt filename2 如果 filename1 比 filename2 新,则为真 [ /tmp/install/etc/services -nt /etc/services ]

    filename1 -ot filename2   如果 filename1 比 filename2 旧,则为真  [ /boot/bzImage -ot arch/i386/boot/bzImage ]

    字符串比较运算符 (请注意引号的使用,这是防止空格扰乱代码的好方法)

    -z string               如果 string 长度为零,则为真               [ -z $myvar ]

    -n string                      如果 string 长度非零,则为真        [ -n $myvar ]

    string1 = string2         如果 string1 与 string2 相同,则为真     [ $myvar = one two three ]

    string1 != string2        如果 string1 与 string2 不同,则为真     [ $myvar != one two three ]

    算术比较运算符

    num1 -eq num2              等于         [ 3 -eq $mynum ]

    num1 -ne num2              不等于       [ 3 -ne $mynum ]

    num1 -lt num2               小于        [ 3 -lt $mynum ]

    num1 -le num2            小于或等于     [ 3 -le $mynum ]

    num1 -gt num2             大于          [ 3 -gt $mynum ]

    num1 -ge num2             大于或等于    [ 3 -ge $mynum ]

    变量:  ("")双引号:用于标记多个特殊符号,但是对$,,`,!符号例外
    ('')单引号:作用同上,但是可以标记""所不能的特殊符号
    (``)反引号[!前的那个符号,不是单引号]一般用来执行命令
    ()反斜杠:转义符号,用于标记单个特殊符号.[root@redhat ~]# winner=bob
    [root@redhat ~]# notice="the person who won is $winner"
    [root@redhat ~]# echo $notice
    the person who won is bob ##将$winner解析成变量
    [root@redhat ~]# winner=bob
    [root@redhat ~]# notice='the person who won is $winner'
    [root@redhat ~]# echo $notice
    the person who won is $winner ##$winner只是符号
    [root@redhat ~]# echo $winner
    bob
    [root@redhat ~]# winner=bob
    [root@redhat ~]# notice="the person who won is $winner"
    [root@redhat ~]# echo $notice
    the person who won is $winner ##用将$转义成字符
    [root@redhat ~]# echo $winner
    bob
    [root@redhat ~]# listnew='ls *.new'
    [root@redhat ~]# echo $listnew
    ls main.cf.new master.cf.new ##仔细查看,两个结果不一样的
    [root@redhat ~]# listnew=`ls *.new`
    [root@redhat ~]# echo $listnew
    main.cf.new master.cf.new

    字符:  空格(space): 解析命令行参数
    (*)(?)({and}):产生文件名列表
    (.): 代表当前目录
    ($): 对变量求值
    (>)(<):    重定向标准输入或输出
    (&):       执行后台命令
    (|):       管道输出
    一般命令:  echo 显示变量及字符
    read 从用户处接受输入
    <<  输入重定向
    脚本命令相关:  $0 Linux命令名
    $n 命令行参数
    $* 由号码1开始的所有的命令行参数
    $@ 分别访问命令行参数
    $# 命令参数的个数
    运算:  exprort 将局部变量导出为该shell中的全局变量
    +,-,*,/,>,<,>=,<=,=(用于字符串的比较),==(用于数字的比较),!=,&(与),|(或),!(非).[root@redhat ~]# let a=2*7
    [root@redhat ~]# echo $a
    14
    [root@redhat ~]# let a="2 * 7"
    [root@redhat ~]# echo $a
    14
      test比较运算,在比较运算中,正确结果的返回值是0,而不是传统程序上的1
      -gt 一个数是否大于另一个数
    -lt 小于
    -ge 大于等于
    -le 小于等于
    -eq 等于(用于数值)
    -ne 不等于
    -z 是否为空字符串
    -n 字符串是否大于0
    =  字符串是否相等
    != 是否不等
    str 是否非空
    -a  与
    -o 或
    !  非
    -f 是否为普通文件
    -s 文件是否为空
    -r 文件是否可读
    -w 文件是否可写
    -x 文件是否可执行
    -d 是否为目录
    -h 是否为符号链接
    -c 是否与字符设备相关联
    -b 是否与块文件相关联[root@redhat ~]# num=5
    [root@redhat ~]# test $num -eq 10
    [root@redhat ~]# echo $?
    1 ##返回比较的结果值
    [root@redhat ~]# num=5
    [root@redhat ~]# test $num -eq 5
    [root@redhat ~]# echo $?
    0
    下面的[]符号和test的作用等同[root@redhat ~]# num=5
    [root@redhat ~]# [ $num -eq 10 ] ##注意中间空格的地方
    [root@redhat ~]# echo $?
    1
    [root@redhat ~]# num=5
    [root@redhat ~]# [ $num -eq 5 ]
    [root@redhat ~]# echo $?
    0
    给个小脚本试试[root@redhat ~]# vi lss.sh
    #!/bin/bash ##使用bash
    echo This is a list Bash cript:
    echo S: list sizes
    echo L: List all file information
    echo C: list c Files
    echo -n "Please enther choice:"
    read choice ##等待用户输入
    case $choice in
    s|S)          ##如果输入的是s,不区分大小写
    ls -s
    ;;
    l|L)         ##如果输入的是l,不区分大小写
    ls -l
    ;;
    c|C) ##如果输入的是c,不区分大小写
    ls *.new
    ;;
    *) ##都不是的话
    echo error
    esac
    [root@redhat ~]# chmod +x lss.sh
    [root@redhat ~]# ./lss.sh
    This is a list Bash cript:
    S: list sizes
    L: List all file information
    C: list c Files
    Please enther choice:l
    total 69
    -rw-r--r-- 1 root root 1251 Oct 24 15:01 anaconda-ks.cfg
    drwx------ 3 root root 1024 Oct 24 15:04 Desktop
    -rw-r--r-- 1 root root 545 Jan 19 15:22 genhtml.sh
    -rw-r--r-- 1 root root 47583 Oct 24 15:01 install.log
    -rw-r--r-- 1 root root 4702 Oct 24 15:01 install.log.syslog
    -rwxr-xr-x 1 root root 248 Jan 19 15:56 lss.sh
    -rw-r--r-- 1 root root 2737 Jan 6 12:45 main.cf.new
    -rw-r--r-- 1 root root 3125 Jan 6 12:45 master.cf.new
    -rwxr-xr-x 1 root root 263 Dec 5 17:39 userandgroupadd.sh

  • 相关阅读:
    fw:Hadoop、Pig、Hive、Storm、NOSQL 学习资源收集【Updating】
    【Azure 环境】用 PowerShell 调用 AAD Token, 以及调用Azure REST API(如资源组列表)
    【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
    【Azure 应用程序见解】通过无代码方式在App Service中启用Application Insights后,如何修改在Application Insights中显示的App Service实例名呢?
    【Azure API 管理】API Management service (APIM) 如何实现禁止外网访问
    【Azure 环境】Azure门户中 Metrics 图表的聚合指标每项具体代表什么意思呢?
    【Azure Redis 缓存】Redis导出数据文件变小 / 在新的Redis复原后数据大小压缩近一倍问题分析
    【Azure 事件中心】在Windows系统中使用 kafkaconsumergroups.bat 查看Event Hub中kafka的consumer groups信息
    RadToolTip (点击按钮,弹出ToolTip)
    Url传值,传入中文,读取时出现乱码的解决方案
  • 原文地址:https://www.cnblogs.com/or2-/p/3504071.html
Copyright © 2011-2022 走看看