代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Shell
】
混合一个文件夹下的文件
作者:
Castle.liu
/ 发布于
2014/6/9
/
1160
文件夹下每小时生成文件,需求是把昨天生成的24个文件合成一个然后压缩存储;
#!/bin/bash #Author: Castle Liu #Date: 2014/06/04 #Desc: merge files from a given dir,files should start with ${_BASE_NAME} #Usage: mergeFile -D[-d] [-c] -[-m] _YESTERDAY=`date -d yesterday +"%Y%m%d"` _WORK_D="" _DEFAULT_DIR="${_WORK_D}${_YESTERDAY}" _BACKUP_DIR="${_WORK_D}"`date -d yesterday +"%Y%m"` _BASE_NAME="" _FULL_NAME="${_BASE_NAME}.${_YESTERDAY}" _HEADER='date +"%F %T"' _LOG_TMP='' ###func### #print #echo strings in a more confertable way print(){ string=$1 echo "`eval ${_HEADER}` ${string} " } #help #print help info help() { cat <<EOF -d mandetory,conflict with [-D] option,specify the dir that hold the files to be merged. -D mandetory,conflict with [-d] option,use the default dir, -c optional,compress the file mergeed. -m optional,move the file mergeed to backup dir,and the dir given(or default dir) will be delete. -h print this info. EOF } #error_handler #exit function for the script error_handler(){ error_code=$1 error_detail=$2 level=$3 print "error_code: ${error_code}" print "error_detail: ${error_detail}" if [[ ${level} -gt 3 ]];then pmail "${error_detail}" "${_LOG_TMP}" print "Sended excution log to Admin." fi if [[ ${level} -eq 2 ]];then print "Serious error,excution will be abord." exit ${quit_or_not} fi } #targz #use tar -czvf to compress a file targz(){ file=$1 dir=`pwd` cd `dirname ${file}` print "Compressing file:${file}" tar -czvf `basename ${file}`.tar.gz `basename ${file}` > /dev/null rm -rf `basename ${file}` print 'done.' cd ${dir} } #merge_file #pass in a dirname merge_file(){ dir=$1 _pwd=`pwd` cd ${dir} print "Creating file:${_FULL_NAME}" touch ${_FULL_NAME} count=`ls ${_BASE_NAME}*.txt|wc -l` if [[ ${count} -gt 0 ]];then ls ${_BASE_NAME}*.txt|while read file do if [[ "${file}" != "EOF" ]];then print "Hanlering file:${file}" cat ${file} >> ${_FULL_NAME} rm -rf ${file} fi done print "File created." if [[ ${_COMPRESS} ]];then targz "${_FULL_NAME}" fi if [[ ${_MOVE_TO_BACKUP_DIR} ]];then mv "${_FULL_NAME}.tar.gz" ${_BACKUP_DIR} if [[ $? -eq 0 ]];then export _DELETE_DIR=true fi fi else print "There is no required file in dir:${_DIR}" error_handler "002" "no_required_file" 2 fi cd ${_pwd} } ###Main### #parsing args while getopts "d:cmD" arg do case ${arg} in d) export _DIR="${OPTARG}" export _USE_DIR=true ;; c) export _COMPRESS=true ;; m) export _MOVE_TO_BACKUP_DIR=true ;; D) export _USE_DEFAULT=true ;; h) help exit 0 ;; ?) error_handler 001 "param_error" 2 ;; esac done print "MergeFile start..." if [[ ${_USE_DIR} ]];then print "Using dir:${_DIR}" if [[ -d ${_DIR} ]];then name=`basename ${_DIR}` export _FULL_NAME=${_BASE_NAME}.${name} export _BACKUP_DIR=${_WORK_D}${name:0:6} #Create backup dir if not exists. if [[ ! -d ${_BACKUP_DIR} ]];then mkdir -p ${_BACKUP_DIR} fi merge_file ${_DIR} if [[ ${_DELETE_DIR} ]];then print "Deleting dir:${_DIR}" rm -rf ${_DIR} fi else print "DIR not exists,pls check." error_handler "003" "dir_not_exists." 2 fi elif [[ ${_USE_DEFAULT} ]];then print "USing Default dir:${_DEFAULT_DIR}" #Create backup dir if not exists. if [[ ! -d ${_BACKUP_DIR} ]];then mkdir -p ${_BACKUP_DIR} fi if [[ -d ${_DEFAULT_DIR} ]];then merge_file ${_DEFAULT_DIR} if [[ ${_DELETE_DIR} ]];then print "Deleting dir:${_DEFAULT_DIR}" rm -rf ${_DEFAULT_DIR} fi else print "Default dir not exists,pls check." error_handler "004" "default_dir_not_exists" 3 fi else error_handler 005 "param_error" 2 fi
试试其它关键字
混合文件
同语言下
.
判断用户输入的是否为IP地址
.
根据web访问日志,封禁请求量异常的IP,如IP在半小时
.
iptables自动屏蔽访问网站频繁的IP
.
批量修改服务器用户密码
.
监控httpd的进程数,根据监控情况做相应处理
.
创建10个用户,并分别设置密码,密码要求10位且包含大
.
Expect实现SSH免交互执行命令
.
输入数字运行相应命令
.
用shell打印示例语句中字母数小于6的单词
.
扫描主机端口状态
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Castle.liu
贡献的其它代码
(
1
)
.
混合一个文件夹下的文件
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3