MakefileのSed使用量を理解する

MakefileのSed使用量を理解する

私は特にMakefileで使用されているLinuxのsedコマンドの使い方を理解しようとしています。説明したいコマンドを以下に含めました。これまでの私の説明は、sedがテキストを置き換え、inittabファイル内で動作することです。しかし、さまざまなシンボルが意味するものに加えて、sedが何を探して何を置き換えるのかを正確に理解することはできません。私は最終的に1)これがどのように機能するかを理解し、2)それを編集して代替テキストに2番目のテキスト行を追加したいと思います(今はsedを介して送信される行が1行だけだと思います)。

文脈上、私が理解して編集したいこのコードスニペットは、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>

おすすめ記事