我试图了解 sed 命令在 Linux 中的用法,特别是当它在 Makefile 中使用时。我已经在下面包含了我试图解释的命令。到目前为止,我的解释是 sed 正在替换文本并在 inittab 文件内工作,但除了各种符号的含义之外,我真的无法准确理解 sed 正在寻找什么以及正在替换什么。我最终想 1) 了解这是如何工作的,2) 编辑它以将第二行文本添加到替换中(现在,我相信只有一行通过 sed 发送)。
对于上下文,我试图理解和编辑的这段代码片段来自 Busybox 的 busybox.mk。对于 sed 和 makefile,我是个新手,所以我很感谢您提供的任何指导!
$(SED) '/# GENERIC_SERIAL$$/s~^.*#~$(SYSTEM_GETTY_PORT)::respawn:/sbin/getty -L $(SYSTEM_GETTY_OPTIONS) $(SYSTEM_GETTY_PORT) $(SYSTEM_GETTY_BAUDRATE) $(SYSTEM_GETTY_TERM) #~' \$(TARGET_DIR)/etc/inittab
答案1
它看起来很神秘,但我认为它做了以下事情:
/# GENERIC_SERIAL$$/ -> Only apply the subsequent substitution when
the line matches that pattern. And since this
is a Makefile, you need to write `$$`
to have a literal `$`.
s~^.*# -> Match anything (`.*`) zero or more characters, but
it should include the `#` symbol at the end.
This uses `~` as a separator instead of the
most common `/`
~$(SYSTEM_GETTY_PORT)...
$(SYSTEM_GETTY_TERM) #~ -> and replace it with this horrific line
including Makefile variables that should be defined
elsewhere or passed as flags.
\$(TARGET_DIR)/etc/inittab -> obviously, this is the file in which the
previous substitution should be applied.
总之,sed /<pattern>/s~<match>~<replacement>~ <file>