zoukankan      html  css  js  c++  java
  • if && grep

    if    条件 
    then
            Command
    else
            Command
    fi                               别忘了这个结尾
    ——————————————————————————

    [ ] &&        ——快捷if

    ——————————————————————————

    条件表达式

        if [ -f file ]    如果文件存在

        if [ -d ... ]    如果目录存在

        if [ -s file ]    如果文件存在且非空

        if [ -r file ]    如果文件存在且可读

        if [ -w file ]    如果文件存在且可写

        if [ -x file ]    如果文件存在且可执行

    整数变量表达式
        if [ int1 -eq int2 ]     如果 int1 等于 int2     
        if [ int1 -ne int2 ]     如果不等于 
        if [ int1 -ge int2 ]     如果 >=  
        if [ int1 -gt int2 ]      如果 >  
        if [ int1 -le int2 ]       如果 <=  
        if [ int1 -lt int2 ]        如果 <

    复杂逻辑判断

    -a         与 
    -o        或 
    !        非

     
    if 条件判断中有多个条件
    #!/bin/bash
    score=$1
    if [ $score = 5 ]||[ $score = 3 ];then
        echo right
    else
        echo wrong
    fi
     
    -------------------------------------------------------
    #!/bin/bash
    score=$1
    if [ $score -gt 5 ]||[ $score -lt 3 ];then
        echo right
    else
        echo wrong
    fi
     
    -------------------------------------------------------
    #!/bin/bash
    score=$1
    if [ $score -gt 15 ]||([ $score -lt 8 ]&&[ $score -ne 5 ]);then
        echo right
    else
        echo wrong
    fi
     
     
    -------------------------------------------------------
    或:

    #!/bin/bash

    count="$1"

    if [ $count -gt 15 -o $count -lt 5 ];then

       echo right

    fi
     
    且:

    #!/bin/bash

    count="$1"

    if [ $count -gt 5 -a $count -lt 15 ];then

       echo right

    fi
    -------------------------------------------------------
    score=$1
    if [[ $score -gt 15 || $score -lt 8 && $score -ne 5 ]];then
        echo right
    else
        echo wrong
    fi
    记住必须加两个中括号
    grep -c 输出匹配行数量
             -i  搜索是忽略大小写
             -h  查询多文件时不显示文件名
             -l   只列出符合匹配的文件名,不列出具体匹配行
             -n  列出所有匹配行,并显示行号
             -s  不显示不存在或匹配文本的错误信息
             -v  显示不包含匹配文本的所有行
             -w  匹配整词
             -x   匹配整行
             -r   递归搜索,搜多当前目录及子目录
             -q  禁止输出任何结果,以退出状态表示搜索是否成功
             -b  打印匹配行距文件头部偏移量,以字节为单位
             -o  与-b结合使用,打印匹配的词距文件头部偏移量,以字节为单位
             -E  支持扩展正则表达式
             -F  不支持正则表达式,按照字符串的字面意思进行匹配
     
    实例:
       grep -n 123 123.txt  列出123.txt中包含123的行并显示行号
       grep -E "a|b" 123.txt 多条件过滤  123.txt中包含a或者b的行
      grep  "a|b" 123.txt 同上
      grep -v 'a|b 123.txt 多条件过滤,123.txt中不包含a和b的行
      
  • 相关阅读:
    hibernate_0100_HelloWorld
    MYSQL子查询的五种形式
    JSF是什么?它与Struts是什么关系?
    nop指令的作用
    htmlparser实现从网页上抓取数据(收集)
    The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the
    FCKeditor 在JSP上的完全安装
    Java遍历文件夹的2种方法
    充电电池和充电时间说明
    吃知了有什么好处
  • 原文地址:https://www.cnblogs.com/zhaozhenguo666/p/5512777.html
Copyright © 2011-2022 走看看