gpt4 book ai didi

linux - 仅当文件不存在时才向文件追加一行

转载 作者:行者123 更新时间:2023-11-29 08:41:24 28 4
gpt4 key购买 nike

我需要将以下行添加到配置文件的末尾:

include "/configs/projectname.conf"

到一个名为 lighttpd.conf 的文件

我正在研究使用 sed 来执行此操作,但我不知道该怎么做。

如果该行尚不存在,我如何才能插入它?

最佳答案

保持简单:)

grep + echo 应该足够了:

grep -qxF 'include "/configs/projectname.conf"' foo.bar || echo 'include "/configs/projectname.conf"' >> foo.bar

编辑:合并了@cerin 和@thijs-wouters 的建议

关于linux - 仅当文件不存在时才向文件追加一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20267910/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com