• SHELL小脚本1——读取用户输入并作出反应


    一、问题由来
      看了几页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、继续学习,努力实践

  • 相关阅读:
    [MySQL] 联合索引最左前缀原则的原因
    [Go]字符串转int64数值型
    [日常] 浏览器前进后退与数据结构的思想
    [Go] go下实现md5加密
    [PHP] 判断两个数组是否相同
    [Go] goland开启自动格式化和开启go modules
    [PHP] 使用strace排查接口响应速度慢过程
    [Git] git version 2以上git add .和git add -A 一样
    [PHP] 504 Gateway Time-out处理流程
    [GO] go-fly客服系统0.2.2打包编译版下载
  • 原文地址:https://www.cnblogs.com/alexy/p/shell2.html
Copyright © 2020-2023  润新知