zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    梯度下降的矩阵分解公式推导
    再谈矩阵分解在推荐系统中的应用
    浅谈矩阵分解在推荐系统中的应用
    tomcat局域网内发布html
    通过JavaScript动态生成html控件
    html 复选框checkbox
    HTML <frameset>不同frame之间传值
    OpenLayers 案例一
    ubuntu java开发环境jdk安装
    如何成为Python高手
  • 原文地址:https://www.cnblogs.com/inspred/p/10616716.html
Copyright © 2011-2022 走看看