• shell脚本使用记录一:操作文件


    一,连接远程数据库(保证在服务器上能使用mysql命令行,至少要安装mysql客户端)

    #!/bin/bash
    HOSTNAME="ip"
    PORT="3306"
    USERNAME="username"
    PASSWORD="password"
    DBNAME="test"
    TABLENAME="shell_test"
    insert_sql="insert into $TABLENAME values(NULL,'billchen',2)"
    mysql -h$HOSTNAME  -P$PORT  -u$USERNAME -p$PASSWORD $DBNAME -e  "$insert_sql"

    二,遍历文件夹,并输出该文件夹下所有的所有文件夹信息(计算文件夹大小)

    #!/bin/bash
    
    #function:new ls
    #author:reed
    
    Files=$1
    if [ $# -eq 1 ];then
            for FileList in $(find $1);do
                    FileType=$(ls -lhd $FileList |awk -F' ' '{print $1}'|cut -c 1)
                    if [ "$FileType" == d ];then
                            DirSize=$(du -sh $FileList|awk '{print $1}')
                            ls -lhd $FileList|sed "s/[^ ]+/$DirSize/5" 
                    else
                            ls -lh $FileList 
                    fi
    
            done
    else
            echo "--usage:$0 +[directory] or [file];"
            echo "--example:$0 /root"
    fi

    如果想把这些信息输入到文件中:

    #!/bin/bash
    
    #function:new ls
    #author:reed
    
    Files=$1
    if [ $# -eq 1 ];then
            for FileList in $(find $1);do
                    FileType=$(ls -lhd $FileList |awk -F' ' '{print $1}'|cut -c 1)
                    if [ "$FileType" == d ];then
                            DirSize=$(du -sh $FileList|awk '{print $1}')
                            ls -lhd $FileList|sed "s/[^ ]+/$DirSize/5" >> /root/yaming/vedio-log/mylog.txt
                    else
                            ls -lh $FileList >> /root/yaming/vedio-log/mylog2.txt
                    fi
    
            done
    else
            echo "--usage:$0 +[directory] or [file];"
            echo "--example:$0 /root"
    fi

    如果还想把上面文件中的信息存入数据库中:

    #!/bin/bash
    
    Host="ip"
    TABLE="statistics_storage"
    passwd="password"
    filename=`date +%Y-%m-%d.txt`
    cat /root/yaming/vedio-log/${filename}|tr " " "," > /root/yaming/vedio-log/${filename}
    cat /root/yaming/vedio-log/${filename} | sed 's/^/NULL	/g' |sed 's/	/,/g' >> /root/yaming/vedio-log/${filename}
    cat /root/yaming/vedio-log/${filename} |while read line
    do
    echo $line
    query=`echo "$line" |awk -F ","  '{ printf("%s,"%s","%s","%s","%s","%s","%s","%s","%s","%s"", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10)}'`
    echo $query
    mysql -h$Host dbname  -u username -p$passwd  <<EOF
    INSERT INTO $TABLE VALUES($query);
    EOF
    done
  • 相关阅读:
    机器学习框架之sklearn简介
    ubuntu 16.04下使用 python pip的安装问题。
    ubuntu 16.04 搭建git小型服务器
    使用config 来管理ssh的会话
    【linux】su、sudo、sudo su、sudo -i的用法和区别
    【python】确保文件写入结束
    【python】描述符descriptor
    【python】类(资料+疑惑)
    【pymongo】mongodb cursor id not valid error
    【python】继承关系和isinstance
  • 原文地址:https://www.cnblogs.com/inspred/p/10616716.html
Copyright © 2020-2023  润新知