无法转义 Unix 命令中的某些字符

发布于 2024-12-16 21:32:57 字数 1221 浏览 1 评论 0原文

我正在尝试配置 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 技术交流群。

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

发布评论

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

评论(3

韬韬不绝 2024-12-23 21:32:57

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 of iwatch. 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.

单挑你×的.吻 2024-12-23 21:32:57

看起来命令字符串正在使用某种 XML 进行处理,并且在 XML 中,所有与号都需要转义。这是一个讨论:

http://lists.xml.org/archives/xml -dev/200106/msg00223.html

这有效:

iwatch -r -x '\.git' -t 'django\.po' -c 'echo git pull && whoami' -e modify ./myapplication/locale/

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:

iwatch -r -x '\.git' -t 'django\.po' -c 'echo git pull && whoami' -e modify ./myapplication/locale/
无妨# 2024-12-23 21:32:57

在 shell 中,单引号内没有特殊字符,第一个单引号(在起始单引号之后)结束单引号。这意味着您首先显示的调用并不完全符合您的想法:

iwatch -r -x '\.git' -t 'django\.po' -c 'echo ''Hello''' \
    -e modify ./myapplication/locale/

iwatch 命令实际上看起来与您键入的内容相同:

iwatch -r -x '\.git' -t 'django\.po' -c 'echo Hello' \
    -e modify ./myapplication/locale/

因此,shell 运行的内容实际上是:

echo Hello

和 没有

echo 'Hello'

什么区别输出;输入有差异。要发现差异,您需要在输入中添加空格,包括双空格,例如:

-c 'echo ''Hello  Double  Spaced  World'''

它将输出带有单空格的值。如果您确实想在 iwatch 中使用 'Hello' 引起单引号,则必须编写:

iwatch -r -x '\.git' -t 'django\.po' -c 'echo '\''Hello'\''' \
   -e modify ./myapplication/locale/

或:

iwatch -r -x '\.git' -t 'django\.po' -c 'echo '\''Hello'\' \
   -e modify ./myapplication/locale/

魔法咒语 '\'' 的意思是:

  • 关闭当前的单引号字符串
  • 包含一个单引号(即反斜杠和下一个单引号)
  • 重新启动单引号字符串

在第一个示例中,下一个字符再次是单引号字符串的末尾;第二个去掉了两个相邻的单引号。

您可以证明这适用于:

-c 'echo '\''Hello  Double  Spaced  World'\'

它将输出带有双空格的值(除非 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:

iwatch -r -x '\.git' -t 'django\.po' -c 'echo ''Hello''' \
    -e modify ./myapplication/locale/

The iwatch command actually sees the same as if you typed:

iwatch -r -x '\.git' -t 'django\.po' -c 'echo Hello' \
    -e modify ./myapplication/locale/

So, what the shell runs is actually:

echo Hello

and not

echo 'Hello'

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:

-c 'echo ''Hello  Double  Spaced  World'''

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:

iwatch -r -x '\.git' -t 'django\.po' -c 'echo '\''Hello'\''' \
   -e modify ./myapplication/locale/

or:

iwatch -r -x '\.git' -t 'django\.po' -c 'echo '\''Hello'\' \
   -e modify ./myapplication/locale/

The magic incantation '\'' means:

  • close the current single-quoted string
  • include a single quote (that's the backslash and the next single quote)
  • restart the single-quoted string

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:

-c 'echo '\''Hello  Double  Spaced  World'\'

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.

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