【问题标题】:Can't execute a string as a shell command in elisp无法在 elisp 中将字符串作为 shell 命令执行
【发布时间】:2013-07-11 14:37:56
【问题描述】:
(shell-command "\"C:\\Documents and Settings\\ggustafson\\Desktop\\stuff\\ctags58\\ctags.exe\" -eR -f \"C:\\Documents and Settings\\ggustafson\\Desktop\\BlueTooth_7020\\TAGS\" \"C:\\Documents and Settings\\ggustafson\\Desktop\\BlueTooth_7020\"")

(message "\"C:\\Documents and Settings\\ggustafson\\Desktop\\stuff\\ctags58\\ctags.exe\" -eR -f \"C:\\Documents and Settings\\ggustafson\\Desktop\\BlueTooth_7020\\TAGS\" \"C:\\Documents and Settings\\ggustafson\\Desktop\\BlueTooth_7020\"")

执行最上面的表单给了我错误'C:\Documents' is not recognized as an internal or external command, operable program or batch file.。执行输出,第二种形式放入*Messages* 缓冲区按预期工作(创建一个标签文件)。

为什么我使用这两种技术没有得到相同的结果? shell-command 在将字符串发送到 shell 之前是否会更改字符串?如何使用 elisp 来执行一个字符串,就像我将它粘贴到命令提示符中一样?

shell-quote-argument 也不起作用,因为它会生成一个无法用任何一种方法执行的字符串:

(message (shell-quote-argument "\"C:\\Documents and Settings\\ggustafson\\Desktop\\stuff\\ctags58\\ctags.exe\" -eR -f \"C:\\Documents and Settings\\ggustafson\\Desktop\\BlueTooth_7020\\TAGS\" \"C:\\Documents and Settings\\ggustafson\\Desktop\\BlueTooth_7020\""))
"^\"\\^\"C:\\Documents and Settings\\ggustafson\\Desktop\\stuff\\ctags58\\ctags.exe\\^\" -eR -f \\^\"C:\\Documents and Settings\\ggustafson\\Desktop\\BlueTooth_7020\\TAGS\\^\" \\^\"C:\\Documents and Settings\\ggustafson\\Desktop\\BlueTooth_7020\\^\"^\""

【问题讨论】:

  • 您可能使用了不同版本的ctags.exe。您可以尝试使用完整路径吗?
  • 查找C-h f shell-quote-argument
  • @slitvinov 我一直在使用完整路径,所以 AFAIK 这是 emacs 而不是 ctags 的问题。编辑问题以澄清这一点。
  • @phils 我有,看到问题,但我看不到它如何解决我的问题。

标签: shell emacs elisp


【解决方案1】:

shell-quote-argument 用于将单个 参数 引用到 shell 命令(因此得名),而不是整个 shell 命令。

鉴于您看到的错误表明某处缺少引用,使用内置命令来处理引用似乎是个好主意,而不是手动编写它并假设您有正确的语法。

另一种选择是使用 M-! 以交互方式执行命令(或类似命令),找出被接受的内容,然后使用 Cx M-:获取elisp形式。

【讨论】:

  • +1 用于使用C-x M-: 获取最后一条命令的elisp 形式。出于某种原因,我需要put the quotes around only the directory with a space in it 而不是整个路径。这是在另一台机器上工作的方法:(message "c:\\Users\\Username\\Desktop\\\"ctags 58\"\\ctags.exe -f \"c:\\Users\\Username\\Desktop\\TAGS\" \"c:\\Users\\Username\\Desktop\\ctags 58\\\"")
【解决方案2】:

如果执行使用相同的路径,因此我建议您仔细检查两者,因此相同ctags.exe。我认为这可能是问题所在。您可能需要使用ctags.exe 的完整路径来确保这一点。

如果我在普通 shell 上使用 -Re 选项,我的 ctags (Linux) 会给我同样的错误;这就是让我这么想的原因。

【讨论】:

  • 在两次测试中我都在与ctags.exe 相同的目录中。编辑问题以更好地阐明问题的来源。 :)
  • Answer 解决了您所说的原始问题(在您编辑问题之前)。而且我认为您遇到的问题甚至可能相关...... shell-command 使用的 shell 和您手动粘贴命令的 shell 是否是同一个 shell?我不确定在 Windows 中是否可以通过(shell-command "echo $0") 进行验证。
  • 我一直在使用 shell 命令生成的缓冲区从 emacs 中执行命令。我相信这和shell-command 都在使用 emac 的 cmdproxy.exe,但我正在寻找一种方法来仔细检查。
【解决方案3】:

我强烈建议你在这里使用call-program 而不是shell-command,因为你不使用shell 的任何功能,而且它只是碍事,迫使你引用体操来解释到shell你的意思。

【讨论】:

    猜你喜欢
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 2020-05-19
    • 2022-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多