的inotify和bash [英] inotify and bash

查看:117
本文介绍了的inotify和bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图与inotify工具,将监视一个目录,并删除含有EE线改变所有新文件的bash脚本。一旦改变,将文件移动到另一个目录

I am trying to make a bash script with inotify-tools that will monitor a directory and alter all new files by removing lines containing "EE". Once altered it will move the files to another directory

    #!/bin/sh
    while inotifywait -e create /home/inventory/initcsv; do
      sed '/^\"EE/d' Filein > fileout #how to capture File name?
      mv fileout /home/inventory/csvstorage
    fi
    done

请帮忙?

推荐答案

默认情况下, inotifywait -e输出的文本CREATE 是形式

     watched_filename CREATE event_filename

其中, watched_filename 重新presents /家庭/库存/ initcsv event_filename 重新presents新的文件名。

where watched_filename represents /home/inventory/initcsv and event_filename represents the name of the new file.

因此​​,在地方你的而inotifywait -e ... 行,放:

So, in place of your while inotifywait -e ... line, put:

    DIR=/home/inventory/initcsv
    while RES=$(inotifywait -e create $DIR); do
        F=${RES#?*CREATE }

和在 SED 在线使用 $ F FILEIN 名称。请注意,在 $(...)建设进程替换(使用反引号经常做)和 $ {#RES模式的POSIX兼容的形式} 结果等于 $ RES 去掉最短的模式匹配preFIX。注意,图案的最后一个字符是一个空白。 [见更新2]

and in your sed line use $F as the Filein name. Note, the $(...) construction is posix-compatible form of process substitution (often done using backticks) and the ${RES#pattern} result is equal to $RES with the shortest pattern-matching prefix removed. Note, the last character of the pattern is a blank. [See update 2]

更新1 :要处理的sed的线上使用$ F而不是可能包含空格的文件名, $ F 。也就是说,使用双引号围绕提及的˚F

Update 1 To handle file names that may contain whitespace, in the sed line use "$F" instead of $F. That is, use double quotes around the reference to value of F.

RES = ... F = ... 定义不需要使用双引号,但它是确定如果你喜欢使用它们;例如: F = $ {?RES#* CREATE} F =$ {?RES#* CREATE}处理包含空格的文件名都的时候将工作正常。

The RES=... and F=... definitions do not need to use double quotes, but it is ok to use them if you like; for example: F=${RES#?*CREATE } and F="${RES#?*CREATE }" both will work ok when handling filenames containing whitespace.

更新2 作为大安的评论指出, inotifywait 有一个 - 格式参数,控制其输出的形式。随着命令

Update 2 As noted in Daan's comment, inotifywait has a --format parameter that controls the form of its output. With command

while RES=$(inotifywait -e create $DIR --format %f .)
   do echo RES is $RES at `date`; done

在一个终端和运行命令

touch a aa; sleep 1; touch aaa;sleep 1; touch aaaa

在另一个终端上运行,下面的输出出现在第一终端

running in another terminal, the following output appeared in the first terminal:

Setting up watches.
Watches established.
RES is a at Tue Dec 31 11:37:20 MST 2013
Setting up watches.
Watches established.
RES is aaa at Tue Dec 31 11:37:21 MST 2013
Setting up watches.
Watches established.
RES is aaaa at Tue Dec 31 11:37:22 MST 2013
Setting up watches.
Watches established.

这篇关于的inotify和bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆