一些 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 11
和22 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"