zoukankan      html  css  js  c++  java
  • shell遍历文件目录,监听文件变化,拼接字符串

    最近利用业余时间学习了shell 并做了个例子

    实现的功能是 : 监听demo文件夹下的文件,只要新增了  .js的文件就把对应的文件名重组,拼接, 最后写入到demo.js里面.

    文件结构如下 :

    demo.sh代码如下

    while : ;
    do
    if [ -f oldfiles.log ]
    then
            #按照时间排序 并取第一行 就是最新建的文件
            newfile=` ls -t | head -1 `
            #查找到最新建的文件 如果不存在 就抛出错误到dev/null(无底洞)
            cat oldfiles.log | grep $newfile >/dev/null
            #如果上一条命令执行后的结束代码不是0则执行下面的命令
            if [ $? -eq 1 ]
            then
                    echo "there is a new file: $newfile"
                    # 先把.js结尾的文件获取到  就是只监控js文件
                    if [ "${newfile##*.}"x = "js"x ]
                        then
                        #再判断是 class 开头的还是 route 开头的
                        newfilehead=`echo $newfile | cut -d . -f 1`
                        val_class='class'
                        val_route='route'
                            #如果是route开头的文件
                            #ecui.esr.loadRoute(‘smile.monkey’);
                            if [ "$newfilehead"x = "route"x ]
                                then
                                    # echo $newfile
                                    noHeadStr=`echo ${newfile#*.}`
                                    noTailStr=`echo ${noHeadStr%.*}`
                                    # echo "ecui.esr.loadRoute(‘"${noTailStr}"’);" >>demo.js
                                    echo "ecui.esr.loadRoute('"${noTailStr}"');" >>demo.js
                            fi
                            #如果是class开头的文件
                            #ecui.esr.loadClass(‘smile.monkey’);
                            if [ "$newfilehead"x = "class"x ]
                                then
                                    # echo $newfile
                                    noHeadStr=`echo ${newfile#*.}`
                                    # echo $noHeadStr
                                    noTailStr=`echo ${noHeadStr%.*}`
                                    echo $noTailStr
                                    cat demo.js >oldfiles.log
                                    echo "ecui.esr.loadClass('"${noTailStr}"');" >demo.js
                                    cat oldfiles.log | while read line
                                    do
                                       echo $line>>demo.js
                                    done
                                    rm oldfiles.log
                            fi
                    fi
                    echo $newfile >> oldfiles.log
            else
                    echo "there is no new files"
            fi
    else
    ls -t -r >  oldfiles.log
    echo "cache old files info"
    fi
    sleep 1; done;

    执行demo.sh之后 会无限循环执行 达到遍历的目的.

    最后总结:合理使用shell 能帮助我们自动化完成好多任务 ,提高工作效率

    如果没有shell基础的同学先看看一下链接 稍微学习下shell:

    shell 的菜鸟教程:   http://www.runoob.com/linux/linux-shell.html

    shell截取字符串: http://www.jb51.net/article/56563.htm

    shell 字符串比较:  https://www.cnblogs.com/wangkongming/p/4221503.html

  • 相关阅读:
    6.linux下指定项目使用特定jdk
    5.linux 执行shell报bad interpreter:No such file or directory错误
    定时任务基础版本
    同一台电脑安装两个jdk切换问题
    接口如何设计?安全如何保证?签名如何实现?防重如何实现?
    spring boot常见get 、post请求参数处理
    bat例子
    1.Volatile关键字详解
    1.linux目录
    解析xml报文,xml与map互转
  • 原文地址:https://www.cnblogs.com/yalong/p/9036087.html
Copyright © 2011-2022 走看看