如何使用sed内联正确地将~替换为$HOME?

huangapple go评论60阅读模式
英文:

How to replace ~ with $HOME properly using sed inline?

问题

以下是您要的翻译内容:

The idea is to transform this ~/.local/bin let's say to this /home/user1/.local/bin

so I have a Makefile that basically a try to get it to work but it doesn't

system: 
	sed -i 's/~/$HOME/g' myapp.desktop
[Desktop Entry]
Version=0.4
Type=Application

Name=Desktop App

Exec=~/.local/bin/desktop_app
Terminal=false
英文:

The idea is to transform this ~/.local/bin let's say to this /home/user1/.local/bin

so I have a Makefile that basically a try to get it to work but it doesn't

system: 
	sed -i 's/~/$HOME/g' myapp.desktop
	
[Desktop Entry]
Version=0.4
Type=Application

Name=Desktop App

Exec=~/.local/bin/desktop_app
Terminal=false

答案1

得分: 3

自从您自己的代码生成包含波浪符的伪路径,我猜您可以对其形式有信心。这很重要,因为一般来说,shell的波浪符扩展行为与您要执行的简单翻译不完全一致。shell的波浪符扩展至少有以下几种不同之处:

  • 它仅在波浪符是单词的第一个字符时应用
  • 它会影响主题单词的前缀,直到第一个斜杠(/)之前(如果有的话)
  • 它仅会影响主题单词中如上所述的前缀,而不会影响主题单词中的其他波浪符出现

像您的示例sed命令建议的简单替换将在某些可能的输入上与shell的波浪符扩展产生相同的效果,但在其他情况下效果不同,因此重要的是您能够控制输入的形式。

对于您的特定情况,您似乎希望shell扩展$HOME,但单引号会阻止它扩展。另一方面,您需要make 扩展$H(可能是为空),因此需要对make中的$进行转义。生成的规则可能如下所示:

system: 
        sed -i "s/~/$${HOME}/g" myapp.desktop

但是,这还存在其他问题,例如-i选项不具备可移植性。某些sed根本没有这个选项,而拥有该选项的sed之间的行为也各不相同(例如BSD风格的sed与GNU sed之间的差异)。


由于您通过代码生成器生成此桌面文件,然而,首先要考虑的问题是为什么您不首次生成所需的Exec路径呢?

对我来说唯一有意义的答案是您在与软件将被安装的上下文不同的地方生成桌面文件。但即便如此,下一个问题是**为什么您不利用用于处理桌面文件的标准工具呢?**特别是,使用desktop-file-install来将文件安装到用户的.local/share/applications中,并同时设置Exec键的正确值,将是合适的。示例:

INSTALL_BIN = "$${HOME}"/.local/bin
INSTALL_DESKTOP = "$${HOME}"/.local/share/applications
install:
        mkdir -p $(INSTALL_BIN)
        mkdir -p $(INSTALL_DESKTOP)
        install -m 0755 desktop-app $(INSTALL_BIN)
        desktop-file-install --dir=$(INSTALL_DESKTOP) --set-key=Exec --set-value=$(INSTALL_BIN)/desktop-app
英文:

Since your own code is generating the tilde-including pseudo-path, I guess you can be confident of the form. This is important, because in general, the shell's tilde expansion behavior is not wholly consistent with the simple translation you want to perform. The shell's tilde expansion differs in at least these ways:

  • it applies only when the tilde is the first character of the word
  • it affects a prefix of the subject word up to but excluding the first slash (/), if any
  • it affects only a prefix such as described above, not any other tilde appearances in the subject word

A simple substitution such as your example sed command suggests will have the same effect as the shell's tilde expansion on some possible inputs, but different effects on others, so again, it's important that you are controlling the form of the input.

For your particular case, you seem to want the shell to expand $HOME, but single-quoting it suppresses that. On the other hand, you need make to not expand $H (to nothing, probably), so you need to escape the $ from make. The resulting rule might look like this:

system: 
        sed -i "s/~/$${HOME}/g" myapp.desktop

There are other problems with that, though, such as that the -i option is non-portable. Some seds don't have it at all, and behavior varies among those that do have it (BSD-style sed vs GNU sed, for example).


Since you're generating this desktop file via a code generator, however, the first question that comes to mind is why are you not generating it with the wanted Exec path in the first place?

The only answer that make sense to me is that you are generating the desktop file in a separate context from the one(s) in which the software will be installed. In that case, however, the next question is why are you not taking advantage of the standard tools for working with desktop files? In particular, it would be appropos to use desktop-file-install to both install the file into the user's .local/share/applications, and, simultaneously, to set the correct value of the Exec key within. Example:

INSTALL_BIN = "$${HOME}"/.local/bin
INSTALL_DESKTOP = "$${HOME}"/.local/share/applications
install:
        mkdir -p $(INSTALL_BIN)
        mkdir -p $(INSTALL_DESKTOP)
        install -m 0755 desktop-app $(INSTALL_BIN)
        desktop-file-install --dir=$(INSTALL_DESKTOP) --set-key=Exec --set-value=$(INSTALL_BIN)/desktop-app

huangapple
  • 本文由 发表于 2023年3月12日 19:15:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定