【Shell学习】shell脚步例子

#!/bin/bash

home_path="/root/.jenkins/jobs"
path_file=${home_path}/path.txt
log_path=${home_path}/clear.log
disk_size_consum=$(df -h|grep -v grep|grep "vg_control-lv_root"|awk '{print $5}'|sed 's/\%//g')
disk_limit=90
find ${home_path} -name "builds"|sed 's/\.\///g' > ${path_file}
date_time=$(date +'%Y%m%d') 
clear()
{
     while read line
     do
        echo -e "当前清理工程${line}"
        cd ${line}
        build_num=`ls -t|grep '^[0-9]'|wc -l`
    need_build=$((build_num-6))
    [ ${build_num} != 0 ] && rm_build=`ls -t|grep '^[0-9]'|tail -${need_build}|xargs` && rm -rf ${rm_build} && echo -e "当前清理工程${line}成功"
     done < ${path_file}
}

verify_disk()
{
   if [ ${disk_size_consum} -ge ${disk_limit} ];then 
    clear
   else 
       echo -e "$(date +'%Y-%m-%d %H:%M:%S')-磁盘使用率为${disk_size_consum}%, 未超过${disk_limit}" 
   fi   
}
main()
{
   echo -e "\\033[40;31mUsage: $0 [OPTION]...\\033[0m"
   echo -e "\\033[40;31mExample [1]:$0 直接执行清理脚本\\033[0m"
   echo -e "        \\033[40;31m[2]:$0 disk 校验磁盘大小是否大于等于95%,满足则清理,不满足不清理\\033[0m"
   echo -e "        \\033[40;31m查看日志:clear.log\\033[0m"
}
case $1 in
    "")
        read -p "真的需要清理么?YES/NO:" v
        if [ $v == "YES" -o $v == "y" -o $v == "Y" -o $v == "yes" ];then
               clear
        else
             echo -e "Exit!!!"
        fi;;
        "disk")
    verify_disk | tee -a ${log_path};;
    "-help"|"-h")
        main;;
        *)
        echo -e "\\033[31;5mInput parameter error, please input['空','disk']\\033[0m"
esac
View Code
#!/bin/bash

home_path="/home/edr_auto_test/packet/platform"
pkg_name=${home_path}/tools/.pkg_name.txt
log_path=${home_path}/tools/clear.log
disk_size_consum=$(df -h|grep -v grep|grep -A1 "/dev/mapper/ubuntu11-root"|awk '{print $4}'|sed 's/\%//g')
disk_limit=90
find ${home_path}/test_pkg -name "*pkg"|sed 's/\.\///g' > ${pkg_name}
date_time=$(date +'%Y%m%d') 
clear()
{
     while read line
     do
        echo -e "当前清理PKG${line}"
        cd $(dirname ${line})
        build_num=`ls -t|grep 'pkg$'|wc -l`
    need_build=$((build_num-4))
    [ ${build_num} != 0 ] && rm_build=`ls -t|grep 'pkg$'|tail -${need_build}|xargs` && rm -rf ${rm_build} && echo -e "当前清理PKG${line}成功"
     done < ${pkg_name}
}

verify_disk()
{
   if [ ${disk_size_consum} -ge ${disk_limit} ];then 
        echo -e "$(date +'%Y-%m-%d %H:%M:%S')-磁盘使用率为${disk_size_consum}%, 超过${disk_limit},执行清理"
        clear
   else 
       echo -e "$(date +'%Y-%m-%d %H:%M:%S')-磁盘使用率为${disk_size_consum}%, 未超过${disk_limit}" 
   fi   
}
main()
{
   echo -e "\\033[40;31mUsage: $0 [OPTION]...\\033[0m"
   echo -e "\\033[40;31mExample [1]:$0 直接执行清理脚本\\033[0m"
   echo -e "        \\033[40;31m[2]:$0 disk 校验磁盘大小是否大于等于90%,满足则清理,不满足不清理\\033[0m"
   echo -e "        \\033[40;31m查看日志:clear.log\\033[0m"
}
case $1 in
    "")
        read -p "真的需要清理么?YES/NO:" v
        if [ $v == "YES" -o $v == "y" -o $v == "Y" -o $v == "yes" ];then
               clear
        else
             echo -e "Exit!!!"
        fi;;
        "disk")
    verify_disk | tee -a ${log_path};;
    "-help"|"-h")
        main;;
        *)
        echo -e "\\033[31;5mInput parameter error, please input['空','disk']\\033[0m"
esac
View Code

 

#!/bin/bash
home_path="/root/.jenkins/jobs"path_file=${home_path}/path.txtlog_path=${home_path}/clear.logdisk_size_consum=$(df -h|grep -v grep|grep "vg_control-lv_root"|awk '{print $5}'|sed 's/\%//g')disk_limit=90find ${home_path} -name "builds"|sed 's/\.\///g' > ${path_file}date_time=$(date +'%Y%m%d') clear(){     while read line     do        echo -e "当前清理工程${line}"        cd ${line}        build_num=`ls -t|grep '^[0-9]'|wc -l`need_build=$((build_num-6))[ ${build_num} != 0 ] && rm_build=`ls -t|grep '^[0-9]'|tail -${need_build}|xargs` && rm -rf ${rm_build} && echo -e "当前清理工程${line}成功"     done < ${path_file}}
verify_disk(){   if [ ${disk_size_consum} -ge ${disk_limit} ];then clear   else        echo -e "$(date +'%Y-%m-%d %H:%M:%S')-磁盘使用率为${disk_size_consum}%, 未超过${disk_limit}"    fi   }main(){   echo -e "\\033[40;31mUsage: $0 [OPTION]...\\033[0m"   echo -e "\\033[40;31mExample [1]:$0 直接执行清理脚本\\033[0m"   echo -e "        \\033[40;31m[2]:$0 disk 校验磁盘大小是否大于等于95%,满足则清理,不满足不清理\\033[0m"   echo -e "        \\033[40;31m查看日志:clear.log\\033[0m"}case $1 in"")        read -p "真的需要清理么?YES/NO:" v        if [ $v == "YES" -o $v == "y" -o $v == "Y" -o $v == "yes" ];then       clear        else             echo -e "Exit!!!"        fi;;    "disk")verify_disk | tee -a ${log_path};;"-help"|"-h")        main;;        *)        echo -e "\\033[31;5mInput parameter error, please input['空','disk']\\033[0m"esac

posted @ 2020-08-17 16:12  gtea  阅读(219)  评论(0编辑  收藏  举报