Lisp语句中的字符串插值
问题描述:
(insert "[" (shell-command-to-string "~/lombardi/http_fetch.sh") "]")
如何将参数传递给http_fetch.sh函数。这个论点是通过评估得出的(elfeed-entry-link entry)
Lisp语句中的字符串插值
我试着用'在前面,但结束了一个bash错误。
答
使用concat
追加字符串从主叫elfeed-entry-link
到外壳命令的末尾得到的:
(insert "["
(shell-command-to-string
(concat "~/lombardi/http_fetch.sh " (shell-quote-argument (elfeed-entry-link entry))))
"]")
的shell-quote-argument
保护的情况下,该命令的elfeed-entry-link
结果包含shell元字符或空格。 (编辑:删除手动报价原本建议在这里和使用shell-quote-argument
而是得益于@phils评论)
还要注意的shell-command-to-string
结果将极有可能包括最后的换行符,所以一定要strip it out如果你不不需要它。
答
您可以使用format
来插入字符串。
(insert "["
(shell-command-to-string
(format "~/lombardi/http_fetch.sh %s" (elfeed-entry-link entry)))
"]")
正如史蒂夫提到,你应该使用shell-quote-argument
,以确保您处理空格和引号正确,当你传递参数。
不要试图手动引用参数。在生成的shell命令的每个参数上调用'shell-quote-argument'。 (无论您认为需要或不需要,通常都是很好的做法。) – phils
谢谢!我编辑了答案,包括你的建议。 –