1

一些 Emacs 包(例如 AUCTeX)通过以下方式将用户指定的 shell 命令传递给 shell:

(call-process shell-file-name nil 0 nil shell-command-switch the-user-specified-command)

在 MS Windows 的情况下,这通常相当于:

(call-process "cmdproxy" nil 0 nil "-c" the-user-specified-command)

如果我想传递这个 shell 命令,我应该如何转义字符:

"print arg.py" "11 11" "22 22"

它运行一个print arg.py带有两个参数11 1122 22

在接下来的调用中,cmdproxy 只是说Unable to initialize device PRN.

(call-process "cmdproxy"
              nil "foo" nil
              "-c"
              "\"print arg.py\" \"11 11\" \"22 22\"")

对于以下电话,它说error: no program name specified.

(call-process "cmdproxy"
              nil "foo" nil
              "-c"
              "\"\"print arg.py\"\" \"\"11 11\"\" \"\"22 22\"\"")

对于以下内容,它说'\"print arg.py\"' is not recognized as an internal or external command, operable program or batch file.

(call-process "cmdproxy"
              nil "foo" nil
              "-c"
              "\\\"print arg.py\\\" \\\"11 11\\\" \\\"22 22\\\"")

另一方面,以下调用成功地运行带有参数11 11和的脚本22 22

(setenv "ABC" "\"print arg.py\" \"11 11\" \"22 22\"")
(call-process "cmdproxy"
              nil "foo" nil
              "-c"
              "%ABC%")

这可能是一个很好的解决方法,但它并没有说明如何转义字符。

脚本文件的内容print arg.py,供想要测试的人使用:

import sys

if __name__ == "__main__":
    print "start"
    for arg in sys.argv[1:]:
        print "(" + arg + ")"
    print "end"
4

2 回答 2

0

看来您不需要外壳:只需执行(call-process "print arg.py" nil "foo" nil "11 11" "22 22").

于 2013-06-27T17:38:41.773 回答
0

用反斜杠转义命令名称中的空格并将参数放在单引号中:

(call-process "cmdproxy"
          nil "foo" nil
          "-c"
          "print\ arg.py '11 11' '22 22'")
于 2013-06-28T13:43:28.057 回答