• 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

  • 相关阅读:
    Codeforces Round #368 Div. 2
    TXT文件去除多余空行
    #4247. 串
    #4322. 字符串游戏(strgame)
    #4214. 谢特
    #4155. 咱们去烧菜吧
    #4350. 「十二省联考 2019」字符串问题
    #4349. 「十二省联考 2019」异或粽子
    #4303. 跳蚤
    #4302. 魔法咒语
  • 原文地址:https://www.cnblogs.com/yalong/p/9036087.html
Copyright © 2020-2023  润新知