一,连接远程数据库(保证在服务器上能使用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