inotify 和 bash
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,
inotifywait -e CREATE
的文本输出格式为
watched_filename
代表/home/inventory/initcsv
和event_filename< /code> 代表新文件的名称。
因此,在
while inotifywait -e ...
行中输入:,并在
sed
行中使用$F
作为文件名
。请注意,$(...)
构造是 posix 兼容的进程替换形式(通常使用反引号完成),并且${RES#pattern}
结果等于$RES
删除了最短的模式匹配前缀。请注意,模式的最后一个字符是空白。 [参见更新 2]更新 1 要处理可能包含空格的文件名,请在 sed 行中使用
"$F"
而不是$F
。也就是说,对F
值的引用使用双引号。RES=...
和F=...
定义不需要使用双引号,但如果您愿意,也可以使用它们;例如:F=${RES#?*CREATE }
和F="${RES#?*CREATE }"
在处理包含空格的文件名时都可以正常工作。更新 2 正如 Daan 的评论中所述,
inotifywait
有一个--format
参数来控制其输出的形式。 命令在一个终端中运行
并在另一个终端中运行命令时,第一个终端中出现以下输出:
By default, the text output from
inotifywait -e CREATE
is of formwhere
watched_filename
represents/home/inventory/initcsv
andevent_filename
represents the name of the new file.So, in place of your
while inotifywait -e ...
line, put:and in your
sed
line use$F
as theFilein
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 ofF
.The
RES=...
andF=...
definitions do not need to use double quotes, but it is ok to use them if you like; for example:F=${RES#?*CREATE }
andF="${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 commandrunning in one terminal and command
running in another terminal, the following output appeared in the first terminal:
inotifywait
的输出格式为:如果您的文件名可以包含空格和逗号,这很难解析。如果它只包含“正常”的文件名,那么你可以这样做:
The output from
inotifywait
is of the form: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:
引用inotifywait的手册页:
换句话说,它将文件名打印到标准输出。因此,您需要从标准输出中读取它们并对它们进行操作以执行您想要执行的操作。
Quoting the man page of inotifywait:
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.