zoukankan      html  css  js  c++  java
  • 常用的shell脚本

    1、循环

    # /bin/bash
    i=1
    while [1]; do
        sleep 1
        service xxx status
        if [$? == 0]; then
            echo "status"
            exit 1
        fi
        service xxx start
    done
    #!/bin/bash
    i = 1
    for i in {1..5000}
    do
       echo ------$i------
    done

    2、循环读取数组

    configs=(a1 a2 a3 a4)
    for conf in ${configs[@]}
    do
    done

    len=${#configs[@]}
    entry=${configs[$i]}

    3、字符串对比

    if [ "$service"x = "$service2"x ]; then
    fi

    4、读取文件循环行

    #!/bin/bash
    cat $1 | while read line
    do
       patch -p1 -i ../$line
    done

    5、i自增

    let i+=1
    echo $i

    6、判断相等和不相等

    if [ $i -eq 10000]; then
    fi
    if [ $i -ne 0]; then
    fi

    7、统计行数

    j=`cat /var/log/messages.log | grep "xxx" | wc -l`

    8、统计c代码行数

    find . -name "*.c"|xargs cat|grep -v -e '^$' -e '^s*//.*$' -e '^s*/*.*$' -e '^s**.*$' | wc -l
    find . -name "*.h"|xargs cat|grep -v -e '^$' -e '^s*//.*$' -e '^s*/*.*$' -e '^s**.*$' | wc -l
    排除文件中空格行,排除注释行,统计的真实代码行数

    9、awk获取服务的进程ID

    pid=`service xx status  | grep Main | awk '{print $3}'`

    10、当 -I 与 xargs 结合使用,每一个参数命令都会被执行一次,如下命令实现杀死 某个关键字A的所有进程

    ps -ef | grep A | grep -v 'grep' | awk '{print $2}' | xargs -I % kill -9 %

    11、批量scp文件

    #!/bin/bash
    cat $1 | while read line
    do
      dir=${line%/*}
      mkdir -p $dir
      ./cp-passwd $line $dir
    done
    
    cp-passwd
    #!/bin/expect
    set file [lindex $argv 0]
    set dir [lindex $argv 1]
    spawn  scp $file root@9.71.5.95:$dir/
    set timeout 30
    expect "password:"
    send "Huawei@CLOUD8!
    "
    expect eof

    12、获取rpm包名称,并创建文件目录,解压rpm源码包

    #!/bin/bash
    dir=`pwd`
    src=centos_7.3_src
    rpm=centos_7.3_rpm
    
    cat $1 | while read src_rpm
    do
            rpm_name=${src_rpm%-*}
            rpm_name=${rpm_name%-*}
            echo $rpm_name
            cd $dir/$rpm
            if [ $? -ne 0 ];then
                echo 'cd $dir/$rpm is failed'
                break
            fi
            mkdir $rpm_name && cd $rpm_name
            if [ $? -ne 0 ]; then
                echo 'mkdir $rpm_name && cd $rpm_name is failed'
                break
            fi
            rpm2cpio $dir/$src/$src_rpm | cpio -id > /dev/null 2>&1
    done

    13、查看安装版本中是否已经包含所有rpm列表中的包

    #!/bin/bash
    RPM=
    cat $1 | while read line
    do
        RPM=$(echo $line | awk -F "." '{print $1}') >/dev/null
        rpm -qa | grep $RPM > /dev/null
        if [ $? -ne 0 ]; then
            echo $line
        fi
    done

    14、生成一个大文件

    dd if=/dev/zero of=large_file_in bs=1K count=1
  • 相关阅读:
    python利用pip安装模块出现报错:ReadTimeoutError: HTTPSConnectionPool的解决方法
    20届一战南理工软件工程(专硕)824
    python—socket编程
    python—异常处理
    python—面向对象设计
    python—装饰器
    python—模块与包
    python—迭代器,生成器与for循环机制
    python—文件处理
    python—函数
  • 原文地址:https://www.cnblogs.com/xingmuxin/p/11310213.html
Copyright © 2011-2022 走看看