美文网首页
shell编程小知识

shell编程小知识

作者: 霸体 | 来源:发表于2019-05-24 11:15 被阅读0次

    shell正则匹配

    # 普通正则匹配判断
    if [[ "abc" =~ "a" ]] ; then echo succ ;fi
    if [[ "abc" =~ ^a ]] ; then echo succ; fi
    # 正则提取
    echo $var | awk '{a=$0;match(a,"[a-z]+");s=substr(a,RSTART,RLENGTH);print s}' 
    

    shell 字符串操作

    内置的shell字符串操作不需要系统调用,不需要上下文切换,处理速度非常快,掌握所有的内置shell字符串操作是shell脚本的最重要 的基础。

    表达式 命令含义
    ${a-Hello} 如果a未定义则返回Hello
    ${a:-Hello} 如果a未定义或为空则返回Hello
    ${a=Hello} 如果a未定义则覆盖为Hello
    ${a:=Hello} 如果a未定义或为空则覆盖为Hello
    ${a+Hello} 如果a已经定义则返回Hello
    ${a:+Hello} 如果a已经定义且不为空则返回Hello
    ${!a*} 返回所有以a开头的变量
    ${#string} 返回string的长度
    ${string:position} 提取子串,postion从0开始
    ${string:position:length} 同上
    ${string#expr} 左边删除
    ${string##expr} 左边删除尽可能多
    ${string%expr} 右边删除
    ${string%%expr} 右边删除尽可能多
    ${string/a/b} a替换为b
    ${string//a/b} 所有a替换为b
    ${string/#a/b} 开头的a替换为b
    ${string/%a/b} 结尾的a替换为b
    #字符串大小写的一些操作
    echo "$HI" # HellO
    echo ${HI^} # HellO  首字符大写
    echo ${HI^^} # HELLO 所有字符大写
    echo ${HI,} # hellO 首字符小写
    echo ${HI,,} # hello 所有字符小写
    echo ${HI~} # hellO  首字符大小写取反
    echo ${HI~~} #hELLo  所有字符大小写取反
    # 你还可以使用负数作为offset,这时候就是从后往前算起。注意负数要用括号括起来,避免冲突。
    echo ${TEXT:(-4)}
    

    获取进程已经执行的时间(其他线程属性可类推)

    # read读取变量的语法
    read pid costTime <<<  `echo "123" "1000ms" `
    # ps 高级用法
    ps -eo pid,etime,cmd  | grep "your cmd"
    # 组合使用
    read pid costTime <<< `ps -eo pid,etime,cmd | grep "key" | awk '{print $1" "$2}' `
    

    awk中调用shell脚本

    # awk中按照行读取一个文件内容
     awk 'BEGIN{"wc -l /tmp/tmp.log |cut -d'\'' '\'' -f1" | getline cnt;
     for(i=0;i<cnt;++i){
     "cat /tmp/tmp.log" | getline line;print i":"line;
     } 
     }'   
    

    数组操作

    declare -a ARY # 0 1 2 做下标的数组
    declare -A MAP # 任意key做下标的数组
    MAP+=([a]=1 [b]=2)
    ARY+=(a b c)
    
    echo ${ARY[1]}
    echo ${MAP[a]}
    echo "${ARY[@]}"
    echo "${MAP[@]}"
    echo "${ARY[@]:0:1}" #数组切片
    echo "${#ARY[@]}"   #数组长度
    echo "${!MAP[@]}"  #数组的所有keys
    
    ARY[4]=a
    echo ${ARY[@]}
    echo ${ARY[3]}
    

    程序结构语句

    
    # while
    cnt=3
    while (( $cnt )) ; do
    echo "now the cnt  is : $cnt"
    cnt=$((cnt-1))
    done
    
    # for
    for (( i=0 ; i<3 ; ++i )) ; do
    echo "now index is $i"
    done
    
    # until
    cnt=0
    until [[ $cnt == 3 ]]; do
        #statements
        cnt=$((cnt+1))
        echo "now cnt is $cnt"
    done
    
    # if
    cnt=1
    if [[ $cnt = 0 ]]; then
        #statements
        echo "cnt is 0"
    elif [[ $cnt = 1 ]]; then
        #statements
        echo "cnt is 1"
    fi
    
    # case
    cnt="ef"
    case $cnt in
        a* )
        echo "cnt is b*"
        ;;
        e* )
        echo "cnt is e*"
        ;;
        * )
        echo "default"
        ;;
    esac
    
    # 操作符|| 以及  && 可以灵活的控制程序执行
    # 灵活使用 || 和 && 可以避免很多if语句
    
    [[ $cnt ]] || echo "变量 cnt 不存在" && return 1 ;
    printf "%d" "$cnt" || echo "变量cnt不是数字" && return 1;
    
    

    相关文章

      网友评论

          本文标题:shell编程小知识

          本文链接:https://www.haomeiwen.com/subject/fjfgzqtx.html