inotify 和 bash

发布于 2024-12-06 11:37:05 字数 331 浏览 1 评论 0原文

我正在尝试使用 inotify-tools 制作一个 bash 脚本,该脚本将监视目录并通过删除包含“EE”的行来更改所有新文件。一旦更改,它将把文件移动到另一个目录

    #!/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

请帮忙?

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

Please help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掀纱窥君容 2024-12-13 11:37:05

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

     watched_filename CREATE event_filename

watched_filename 代表 /home/inventory/initcsvevent_filename< /code> 代表新文件的名称。

因此,在 while inotifywait -e ... 行中输入:

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

,并在 sed 行中使用 $F 作为 文件名。请注意,$(...) 构造是 posix 兼容的进程替换形式(通常使用反引号完成),并且 ${RES#pattern} 结果等于$RES 删除了最短的模式匹配前缀。请注意,模式的最后一个字符是空白。 [参见更新 2]

更新 1 要处理可能包含空格的文件名,请在 sed 行中使用 "$F" 而不是 $F。也就是说,对 F 值的引用使用双引号。

RES=...F=... 定义不需要使用双引号,但如果您愿意,也可以使用它们;例如: F=${RES#?*CREATE }F="${RES#?*CREATE }" 在处理包含空格的文件名时都可以正常工作。

更新 2 正如 Daan 的评论中所述,inotifywait 有一个 --format 参数来控制其输出的形式。 命令

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

并在另一个终端中运行命令时,第一个终端中出现以下输出:

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.

By default, the text output from inotifywait -e CREATE is of form

     watched_filename CREATE event_filename

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

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

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

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]

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.

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.

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

running in one terminal and command

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.
攒眉千度 2024-12-13 11:37:05

inotifywait 的输出格式为:

filename eventlist [eventfilename]

如果您的文件名可以包含空格和逗号,这很难解析。如果它只包含“正常”的文件名,那么你可以这样做:

srcdir=/home/inventory/initcsv
tgtdir=/home/inventory/csvstorage
inotifywait -m -e create "$directory" |
while read filename eventlist eventfile
do
    sed '/^"EE/d'/' "$srcdir/$eventfile" > "$tgtdir/$eventfile" &&
    rm -f "$srcdir/$eventfile
done

The output from inotifywait is of the form:

filename eventlist [eventfilename]

If your file names can contain spaces and commas, this gets tricky to parse. If it only contains 'sane' file names, then you can do:

srcdir=/home/inventory/initcsv
tgtdir=/home/inventory/csvstorage
inotifywait -m -e create "$directory" |
while read filename eventlist eventfile
do
    sed '/^"EE/d'/' "$srcdir/$eventfile" > "$tgtdir/$eventfile" &&
    rm -f "$srcdir/$eventfile
done
鼻尖触碰 2024-12-13 11:37:05

引用inotifywait的手册页:

inotifywait will output diagnostic information on standard error and event information  on
   standard  output.  The event output can be configured, but by default it consists of lines
   of the following form:

   watched_filename EVENT_NAMES event_filename

   watched_filename
          is the name of the file on which the event occurred.  If the file is a directory, a
          trailing slash is output.

换句话说,它将文件名打印到标准输出。因此,您需要从标准输出中读取它们并对它们进行操作以执行您想要执行的操作。

Quoting the man page of inotifywait:

inotifywait will output diagnostic information on standard error and event information  on
   standard  output.  The event output can be configured, but by default it consists of lines
   of the following form:

   watched_filename EVENT_NAMES event_filename

   watched_filename
          is the name of the file on which the event occurred.  If the file is a directory, a
          trailing slash is output.

In other words, it prints the names of the files to standard output. So, you need to read them from standard output and operate on them to do what you want to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文