To prove what you have said when you were young! ——Alexy Young

Follow Your Heart

PM/ACP/PRINCE2……

   :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一、问题由来
  看了几页SHELL书,发现脚本非常有意思。自己写了一个,调试了下还是可以的。经常做一些压缩啊、统计的“搬运工”,如果不想点办法估计得这样笨死了。脚本是一个非常好的作用,修改部分参数实现日常处理的效率提高,很值得一试。如前所属,压缩某些有特殊时间表情的日志文件的命令组合:
“ls |grep $messages|grep -v gz|grep -v `date +%Y%m%d`|xargs -l -t gzip”
我们可以很好的利用这个命令,做一些简单的判断用户读取,进而执行用户需要脚本来处理压缩。

二、小实验

#!/bin/sh
#set -xv

#DEFINE SOME LISTS                     ###预定义一些常用的变量,特别是messages是后续每个月需要压缩的,也是每个月需要修改的;
month=`date -d last-month +%Y%m`          
year=`date +%Y%m`
messages=messages.log.2013030
messages_ls=`ls -lh|grep $messages`
messages_num=`ls -lh|grep $messages|wc -l`
messages_cmp=`ls -lh|grep $messages|grep gz`

##SESSION 1:Conpress messages logs
##
cd /var/log/bak
#Echo the file need which one need to compress
echo "The $month log file"
echo "                                      "
echo "They are $messages_num messages logs need to compress"
echo "+++++++++++++++++++++++ As below here +++++++++++++++++++++++++++++++"
echo "$messages_ls"
echo "+++++++++++++++++++++++ As below here +++++++++++++++++++++++++++++++"
echo "                                      "
echo "                                      "

#Fetch the operation
echo "Are you sure to compress?"
echo "                                      "
echo "Please input you choose,Y/N"
read answer                                                    ####读取用户的输入
answer=`echo $answer | tr [a-z] [A-Z]`                         ####显示用户的输入
if [ $answer = Y ]                                             ####判断用户输入
then
ls |grep $messages|grep -v gz|grep -v `date +%Y%m%d`|xargs -l -t gzip
echo "You input the $answer,So listed of file hava been compressed"
echo "$messages_cmp"
echo "You have susscessful"
else
echo "You input the No,So I need to exit"
fi

echo "If any question,please send a email to.Information as below"
echo "E-mail:jackenjun@163.com"
echo "QQ:305625159"

三、不足和疑问

1、没有把时间显示和用户要操作的时间关联,暂时未想到好的方法;

2、不能让用户感觉像在选择,界面不爱好;

3、效率问题。这个脚本只能压一种,或者一类文件;如果多类,多种呢?多功能叠加还不太会。

四、SHELL的魅力

1、SHELL是效率的体现,不会写shell必定被淘汰(累死)

2、写好shell能提高效率,增加回头率

3、继续学习,努力实践

posted on 2013-07-02 17:55  Alexy Young  阅读(595)  评论(0编辑  收藏  举报

Alexy Young CHASEDREAM