emacs --eval '(ediff "$1" "$2")'如何将此行放入 shell 脚本中?

发布于 2024-12-26 08:08:47 字数 305 浏览 0 评论 0 原文

我想将以下行:

emacs --eval '(ediff "$1" "$2")'

放入shell脚本中,这样我就不需要每次都写这么长而复杂的行。但是,当我将其按原样放入 shell 脚本中时,出现错误:

ediff-find-file: File `$1' does not exist or is not readable

我猜这里存在某种 " 转义问题,我需要放入一些 \ 在这一行中我到底需要修复什么?

I want to put the following line:

emacs --eval '(ediff "$1" "$2")'

into a shell script, so I dont need to write this long and complicated line everytime. However, when I put it into my shell script as it is, I get an error:

ediff-find-file: File `$1' does not exist or is not readable

I guess there is some sort of " escaping problem here and I need to put some \ in this line. What exactly do I need to fix?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

_蜘蛛 2025-01-02 08:08:47

这是正常的,您的字符串用单引号括起来,并且变量不会在单引号内扩展。

您应该使用双引号,以便它们得到扩展,并转义字符串中现有的双引号:

emacs --eval "(ediff \"$1\" \"$2\")"

值得注意的是,在扩展变量时,只有外部引号重要。即:

$ A=foo
$ echo "'$A'"

将打印 'foo'。单引号包围 $A 的事实并不重要:外引号是双引号。

This is normal, your string is surrounded with single quotes, and variables are not expanded within single quotes.

You should use double quotes so that they get expanded, and escape existing double quotes in the string:

emacs --eval "(ediff \"$1\" \"$2\")"

It is worth noting that when expanding variables, only the outer quotes matter. That is:

$ A=foo
$ echo "'$A'"

will print 'foo'. The fact that single quotes surround $A here does not matter: the outer quotes are double quotes.

眼睛会笑 2025-01-02 08:08:47

您面临的直接问题是单引号阻止了 shell 的所有扩展。您需要双引号才能发生任何类型的扩展。但是,您不能只编写 emacs --eval "(ediff \"$1\" \"$2\")" 因为 Emacs 无法正确引用文件名,因此您的脚本将如果文件名包含 "\,则中断。

幸运的是,有一个非常简单的解决方案:

emacs --eval '(ediff command-line-args-left)' "$@"

如果存在现有的 Emacs 进程,最好通过调用emacsclient 不幸的是,对于 Emacsclient,--eval 将其所有参数视为 Lisp 表达式,因此您需要在 bash 或 ksh 中进行引用。使用 ${VARIABLE//PATTERN/REPLACMENT} 在双引号和反斜杠之前添加反斜杠;这有点痛苦,但它有效。

quoted1=${1//\\/\\\\}; quoted1=${quoted1//\"/\\\"}
quoted2=${2//\\/\\\\}; quoted2=${quoted2//\"/\\\"}
emacsclient -a '' --eval "(ediff \"$quoted1\" \"$quoted2\")"

Your immediate problem is that the single quotes prevent all expansion by the shell. You need double quotes for any kind of expansion to happen. However, you can't just write emacs --eval "(ediff \"$1\" \"$2\")" because the file names would not be properly quotes for Emacs, so your script would break if the file names contained " or \.

Fortunately, there's a very simple solution:

emacs --eval '(ediff command-line-args-left)' "$@"

It would be better to reuse an existing Emacs process if there is one, by calling emacsclient. Unfortunately, with Emacsclient, --eval treats all its arguments as Lisp expressions. So you need to do the quoting inside the shell. In bash or ksh, you can use ${VARIABLE//PATTERN/REPLACEMENT} to add backslashes before the double quotes and backslashes; it's a little painful but it works.

quoted1=${1//\\/\\\\}; quoted1=${quoted1//\"/\\\"}
quoted2=${2//\\/\\\\}; quoted2=${quoted2//\"/\\\"}
emacsclient -a '' --eval "(ediff \"$quoted1\" \"$quoted2\")"
り繁华旳梦境 2025-01-02 08:08:47

为了在我的 $HOME/.profile 中有一个名为“ediff”的简单函数,我将两个技巧结合在一起。来自 吉尔斯上面和来自权威博客。我为 emacsclient 和 emacs 添加了 -c 参数作为后备编辑器:

ediff () {
    if [ "X${2}" = "X" ]
    then
        echo "USAGE: ediff <FILE 1> <FILE 2>"
    else
        quoted1=${1//\\/\\\\};quoted1=${quoted1//\"/\\\"}
        quoted2=${2//\\/\\\\};quoted2=${quoted2//\"/\\\"}
        emacsclient -c -a emacs -e "(ediff \"$quoted1\" \"$quoted2\")"
    fi
}

To have an easy function called "ediff" in my $HOME/.profile I combined two tips together. The one from Gilles above and from the defunitive blog. I added the -c argument for emacsclient and emacs as the fallback editor:

ediff () {
    if [ "X${2}" = "X" ]
    then
        echo "USAGE: ediff <FILE 1> <FILE 2>"
    else
        quoted1=${1//\\/\\\\};quoted1=${quoted1//\"/\\\"}
        quoted2=${2//\\/\\\\};quoted2=${quoted2//\"/\\\"}
        emacsclient -c -a emacs -e "(ediff \"$quoted1\" \"$quoted2\")"
    fi
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文