找回密码
 立即注册
Qt开源社区 门户 查看内容

工作常用Linux命令与shell脚本(二)

2019-9-29 13:25| 发布者: admin| 查看: 789| 评论: 0

摘要: 今天更新几个常用脚本1.磁盘空间不足时,将每日的数据文件夹打包。下面的脚本是循环遍历该目录下的所有以日期命名的文件夹名字,然后将当天文件夹日期的100天前文件夹全部打包。 #!/bin/bash#进入目标文件夹cd /hom ...
    今天更新几个常用脚本
1.磁盘空间不足时,将每日的数据文件夹打包。

    下面的脚本是循环遍历该目录下的所有以日期命名的文件夹名字,然后将当天文件夹日期的100天前文件夹全部打包。     
    #!/bin/bash#进入目标文件夹cd /home/pladmin/jc/dataLogfor file in /home/pladmin/jc/dataLog/*dostr="$file"substr=${str##*/}#得到100天前文件夹name=`date -d"100 day ago $substr " +%Y%m%d`if [ -d $name ];then tar -zcvf $name.tar.gz $name --remove-filesecho"$name已打包为$name.tar.gz,并已删除$name文件夹"elseecho"$name文件夹不存在"fidone


    2.将文件名字拆分,并插入到对应表的分区中。

        例如文件名为T_CD_RECORD20190702.del,要将它插入T_CD_RECORD表的20190702分区中。
      #1.先读取文件2.截取表名和日期3.插入表中#!/bin/bash#T_CD_RECORD20190702.deldate=$1dataPath=/home/hadoop/sdb1/mxfsql/$1for file in `ls $dataPath`do tableName=$(echo${file%%2*}|tr [A-Z] [a-z]) tempTableDate=2${file#*2} tableDate=${tempTableDate%.*} hive -e "use sddl;alter table $tableName add partition (etl_date='$tableDate');" hadoop fs -put $dataPath/$file /warehouse/hive/sddl.db/$tableName/etl_date=$tableDate/done

      3.监控作业执行状态的脚本

          如果文件内容为running,则等待15秒。如果不是running,则跳出死循环。
        #!/bin/bash#循环读取#test_zjy2.shstatelog_path=/home/pladmin/shell/mkt/log/state.logwhile (( 1 ))do line=`cat ${statelog_path}` state=`echo${line}`if [ ${state} = "running" ];thenecho"等待中" sleep 15selseecho"退出"breakfi



        ----------------------------------------------------------------------------------------------------------------------
        我们尊重原创,也注重分享,文章来源于微信公众号:远少技术分享会,建议关注公众号查看原文。如若侵权请联系qter@qter.org。
        ----------------------------------------------------------------------------------------------------------------------

        鲜花

        握手

        雷人

        路过

        鸡蛋
        
        公告
        可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了