无法转义 Unix 命令中的某些字符
我正在尝试配置 iwatch
来监视目录中某些文件的更改。它似乎很简单,但我在转义命令字符串时遇到问题。这是一个示例:
iwatch -r -x '\.git' -t 'django\.po' -c 'echo ''Hello''' -e modify ./myapplication/locale/
它监视 /myapplication/locale/
中除 .git
目录之外的所有 *.po
文件以及文件何时被修改,它运行命令echo 'Hello'
。
...但是当我尝试使用稍微复杂一点的命令时,我收到错误:
iwatch -r -x '\.git' -t 'django\.po' -c 'echo git pull && whoami' -e modify ./myapplication/locale/
命令 git pull && whoami
是一个有效的 Unix 命令,但我想我需要以某种方式转义它才能工作。我在有 &&
的部分遇到错误,
关于我做错了什么的任何想法。
谢谢。
编辑#1:尝试迈克尔的建议:
iwatch -r -x '\.git' -t 'django\.po' -c "bash -c 'git pull && whoami'" -e modify ./giosg-chat/locale/
...这是错误。
:5: parser error : xmlParseEntityRef: no name
<path type="recursive" events="modify" exec="bash -c 'git pull && whoami'" a
^
:5: parser error : xmlParseEntityRef: no name
<path type="recursive" events="modify" exec="bash -c 'git pull && whoami'" a
I'm trying to coinfigure iwatch
to monitor a directory for changes to some files. It seems to be quote simple but I'm having issues escaping the command string. Here's an example:
iwatch -r -x '\.git' -t 'django\.po' -c 'echo ''Hello''' -e modify ./myapplication/locale/
This monitors the /myapplication/locale/
for all *.po
files excluding the .git
directories and when a file is modified, it runs the command echo 'Hello'
.
...but when I try using a command that is a slightly more complex, I get errors:
iwatch -r -x '\.git' -t 'django\.po' -c 'echo git pull && whoami' -e modify ./myapplication/locale/
The command git pull && whoami
is a valid Unix command but I think I need to escape it somehow for it to work. I get errors at the part where there's a the &&
Any ideas on what I'm doing wrong.
Thanks.
Edit #1: Trying Michael's suggestion:
iwatch -r -x '\.git' -t 'django\.po' -c "bash -c 'git pull && whoami'" -e modify ./giosg-chat/locale/
...and here's the error.
:5: parser error : xmlParseEntityRef: no name
<path type="recursive" events="modify" exec="bash -c 'git pull && whoami'" a
^
:5: parser error : xmlParseEntityRef: no name
<path type="recursive" events="modify" exec="bash -c 'git pull && whoami'" a
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
git pull && whoami
不是您所说的“有效的 UNIX 命令”。&&
是一个 shell 操作,几乎可以肯定iwatch
的-c
标志不支持该操作。解决方法可能是编写一个执行复合操作的脚本,然后将该脚本作为参数传递给-c
标志。git pull && whoami
is not a 'valid UNIX command' as you say it is. The&&
is a shell operation that is almost certainly not supported by the-c
flag ofiwatch
. A workaround might be to write a script that does the compound operation and then to pass that script as the argument to the-c
flag.看起来命令字符串正在使用某种 XML 进行处理,并且在 XML 中,所有与号都需要转义。这是一个讨论:
http://lists.xml.org/archives/xml -dev/200106/msg00223.html
这有效:
It seems that the command string is being processed using some sort of XML and in XML, all ampersands need to be escaped. Here's a discussion:
http://lists.xml.org/archives/xml-dev/200106/msg00223.html
This worked:
在 shell 中,单引号内没有特殊字符,第一个单引号(在起始单引号之后)结束单引号。这意味着您首先显示的调用并不完全符合您的想法:
iwatch
命令实际上看起来与您键入的内容相同:因此,shell 运行的内容实际上是:
和 没有
什么区别输出;输入有差异。要发现差异,您需要在输入中添加空格,包括双空格,例如:
它将输出带有单空格的值。如果您确实想在
iwatch
中使用 'Hello' 引起单引号,则必须编写:或:
魔法咒语
'\''
的意思是:在第一个示例中,下一个字符再次是单引号字符串的末尾;第二个去掉了两个相邻的单引号。
您可以证明这适用于:
它将输出带有双空格的值(除非
iwatch
中的某些内容破坏了系统)。既然您现在已经发现了问题 - 该命令被视为 XML,并且您必须使用
&
来将 & 符号发送到 shell - 我所说的主要具有学术重要性。In the shell, inside single quotes, there are no special characters and the first single quote (after the starting one) ends the single quotes. That means that your invocation shown first does not do quite what you think:
The
iwatch
command actually sees the same as if you typed:So, what the shell runs is actually:
and not
There's no difference in the output; there is a difference in the input. To spot the difference, you'd need to have spacing in the input including double spaces, such as:
which would output the value with single spaces. If you actually want to get single quotes around 'Hello' to
iwatch
, you'd have to write:or:
The magic incantation
'\''
means:In the first of the examples, the next character is the end of the single quoted string again; the second does away with the two adjacent single quotes.
You could demonstrate that this works with:
which would output the value with double spaces (unless something in
iwatch
subverted the system).Since you've now found the problem - that the command is treated as XML and you have to use
&
to get ampersands to the shell - what I said is mainly of academic importance.