bash 如何运行 fswatch 来调用带有静态参数的程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25689589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run fswatch to call a program with static arguments?
提问by Guy Bowden
I used to use fswatch v0.0.2 like so (in this instance to run django test suit when a file changed)
我曾经像这样使用 fswatch v0.0.2(在这种情况下,当文件更改时运行 django 测试套件)
$>fswatch . 'python manage.py test'
$>fswatch . 'python manage.py test'
this works fine.
这工作正常。
I wanted to exclude some files that were causing the test to run more than once per save (Sublime text was saving a .tmp file, and I suspect .pyc files were also causing this)
我想排除一些导致测试在每次保存时运行多次的文件(Sublime text 正在保存一个 .tmp 文件,我怀疑 .pyc 文件也导致了这个)
So I upgraded fswatch to enable the -e mode.
所以我升级了 fswatch 以启用 -e 模式。
However the way fswatch has changed which is causing me troubles - it now accepts a pipe argument like so:
然而,fswatch 的方式发生了变化,这给我带来了麻烦——它现在接受一个管道参数,如下所示:
$>fswatch . | xargs -n1 program
$>fswatch . | xargs -n1 program
I can't figure out how to pass in arguments to the program here. e.g. this does not work:
我不知道如何在这里向程序传递参数。例如这不起作用:
$>fswatch . | xargs -n1 python manage.py test
$>fswatch . | xargs -n1 python manage.py test
nor does this:
这也不是:
$>fswatch . | xargs -n1 'python manage.py test'
$>fswatch . | xargs -n1 'python manage.py test'
how can I do this without packaging up my command in a bash script?
如何在不将命令打包到 bash 脚本中的情况下执行此操作?
回答by Enrico M. Crisostomo
fswatch
documentation (either the Texinfo manual, or the wiki, or README
) have examples of how this is done:
fswatch
文档(Texinfo 手册或wiki或README
)有如何完成的示例:
$ fswatch [opts] -0 path ... | xargs -0 -n1 -I{} your full command goes here
Pitfalls:
陷阱:
-0
: use it to make sure paths with newlines are interpreted correctly.-o
: use it to havefswatch
"bubble" all the events in the set into a single one printing only the number of records in the set.-I{}
: specifying a placeholder is the trick you missed forxargs
interpreting correctly your command arguments in those cases where you donot want the record (in this case, since-o
was used, the number of records in the set) to be passed down to the command being executed.
-0
:使用它来确保正确解释带有换行符的路径。-o
:使用它可以fswatch
将集合中的所有事件“冒泡”成一个,只打印集合中的记录数。-I{}
:指定占位符是xargs
您在不希望将记录(在这种情况下,由于-o
使用了集合中的记录数)传递给正在执行的命令的情况下正确解释命令参数时错过的技巧执行。
回答by Guy Bowden
Alternative answer not fighting xargs' default reason for being - passing on the output as arguments to the command to be run.
替代答案不与 xargs 的默认原因作斗争 - 将输出作为参数传递给要运行的命令。
fswatch . | (while read; do python manage.py test; done)
fswatch . | (while read; do python manage.py test; done)
Which is still a bit wordy/syntaxy, so I have created a super simple bash script fswatch-do
that simplifies things for me:
这仍然有点罗嗦/语法,所以我创建了一个超级简单的 bash 脚本fswatch-do
,为我简化了事情:
#!/bin/bash
(while read; do $@; done)
usage:
用法:
fswatch -r -o -e 'pyc' somepath | fswatch-do python manage.py test someapp.SomeAppTestCase
fswatch -r -o -e 'pyc' somepath | fswatch-do python manage.py test someapp.SomeAppTestCase