Linux shell常用命令

  1. 获取系统线程总数
    pstree -p | wc -l
  2. 获取指定名称进程id
    以mysql为例
    [root@localhost ~]# ps -elf | grep mysql |grep -v grep | awk '{print$4}' | xargs
    31622 31821
  3. 批量杀死指定名称进程
    以mysql为例
    kill `ps -elf | grep mysql |grep -v grep | awk '{print$4}' | xargs`
  4. shell将命令执行结果分割为数组
    [root@localhost dongsongz]# cat testarray.sh 
    #!/bin/bash
    
    processes=`ps -elf | grep tcpreplay | grep -v grep | awk '{print$4}' | xargs`
    echo $processes
    process_array=($processes)
    for id in ${process_array[@]};do
        echo $id
    done
    
    [root@localhost dongsongz]# ./testarray.sh 
    11424 11461 11521
    11424
    11461
    11521
    
  5. shell指定分割符分割字符串为数组
    [root@localhost dongsongz]# cat testarray2.sh 
    #!/bin/bash
    
    str=`cat ./test.txt`
    echo $str
    IFS=","
    array=($str)
    for i in ${array[@]};do
      echo $i
    done
    [root@localhost dongsongz]# ./testarray2.sh 
    hello,from,test!
    hello
    from
    test!
    

    shell可以通过IFS指定分割符(默认是空格)来分割字符串为数组

  6. shell按C风格for循环遍历数组
    [root@k8s-node2 debug]# cat for.sh 
    #!/bin/bash
    
    teststring='a,b,c,d,e,f,g'
    #指定,分割字符串
    IFS=","
    array=($teststring)
    
    #${#array[@]}表示数组array元素个数
    for((i=0;i<${#array[@]};i++))
    do
        #${array[$i]}表示第i个元素内容
        echo ${array[$i]}
    done
    
    [root@k8s-node2 debug]# ./for.sh 
    a
    b
    c
    d
    e
    f
    g
    

    ${!array[@]}${!array[*]},可以返回数组的成员序号,即哪些位置是有值的。

  7. shell解析命令行参数
    [root@localhost debug]# cat cmdarg.sh 
    #!/bin/bash
    
    usage="usage\n./cmdarg.sh -n [name] -u [user id]"
    if [[ $# -lt 4 ]];then
      echo -e $usage
      exit 1
    fi
    
    #先保存参数到数组argv,再遍历数组argv提取参数
    for((i=0;i<=$#;i++))
    do
      argv[$i]=`eval echo '$'"$i"`
      echo "$i, ${argv[$i]}"
    done
    
    for((i=0;i<$#;i++))
    do
      case ${argv[$i]} in
        -s) let i+=1
            name=${argv[$i]};;
        -u) let i+=1
            user_id=${argv[$i]};;
      esac
    done
    
    echo "name is $name, user id is $user_id"
    
    
    [root@localhost debug]# ./cmdarg.sh -s tom -u 101
    0, ./cmdarg.sh
    1, -s
    2, tom
    3, -u
    4, 101
    name is tom, user id is 101
  8. shell替换文件指定字符串为包含全局环境变量的字符串
    [root@localhost debug]# cat hello.sh 
    #!/bin/bash
    
    hello=1
    echo $hello
    
    [root@localhost debug]# export NAME=GrassInWind
    引用全局的环境变量需要使用'''包含在内部
    [root@localhost debug]# cat replace_with_env.sh 
    #!/bin/bash
    
    sed -i 's@hello=1@hello="hello from '''$NAME'''!"@g' hello.sh
    
    [root@localhost debug]# ./replace_with_env.sh 
    [root@localhost debug]# 
    [root@localhost debug]# cat hello.sh 
    #!/bin/bash
    
    hello="hello from GrassInWind!"
    echo $hello
    
  9.  shell在指定字符串所在行下一行添加一行
    [root@localhost debug]# sed -i '/hello=/a\hi=1' hello.sh
    [root@localhost debug]# cat hello.sh 
    #!/bin/bash
    
    hello="hello from GrassInWind!"
    hi=1
    echo $hello
    
  10. shell用指定字符串替换文件中包含指定字符串的整行内容
    [root@centos-docker root]# cat replaceIp.sh 
    #!/bin/bash
    
    file=test.txt
    ip="192.168.3.100"
    sed -i 's/"serviceIp".*/"serviceIp":"'''$ip'''",/g' $file
    
    [root@centos-docker root]# cat test.txt 
    {
        "test1":1,
        "test2":2,
        "serviceIp":"172.16.1.100",
        "test3":3
    }
    
    [root@centos-docker root]# ./replaceIp.sh 
    [root@centos-docker root]# cat test.txt 
    {
        "test1":1,
        "test2":2,
        "serviceIp":"192.168.3.100",
        "test3":3
    }
    

    通过sed命令可以替换文件内容,'s/"serviceIp".*/"serviceIp":"'''$ip'''",/g'表示将文件中满足"serviceIp"开头的任意内容替换为"serviceIp":"$ip",$ip是期望替换的IP变量。

  11. shell解压rpm包
    rpm2cpio test.rpm | cpio -idmv

你可能感兴趣的:(linux,运维)