gpt4 book ai didi

bash - 使用 sed 取消注释代码

转载 作者:行者123 更新时间:2023-12-04 18:54:56 24 4
gpt4 key购买 nike

我正在编写一个脚本,它将取消注释文件中的几行代码。这就是我要取消注释的内容:

#export PATH="$HOME/.rbenv/bin:$PATH"
#eval "$(rbenv init -)"

所以我做了我的研究,发现这个命令可以满足我的要求:
sed -i '/<pattern>/s/^#//g' file

那么我将如何调整它来做我正在寻找的东西呢?我尝试将文本粘贴到命令中,尽我所能逃避所有引号,但它似乎没有用。

最佳答案

评论是为了评论,不是流量控制。

与其尝试修改文件来切换这些行,不如通过环境变量控制它们的执行。

if [[ -v enable_ruby ]]; then
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
fi

现在,如果变量 enable_ruby设置为环境中的任何值, PATH将被修改和 ruby配置。否则,这两行将被忽略。
$ bash myScript                # Don't do the ruby stuff
$ enable_ruby=1 bash myScript # Do the ruby stuff
$ export enable_ruby= # The empty string is sufficient
$ bash myScript # Do the ruby stuff
$ unset enable_ruby
$ bash myScript # Don't do the ruby stuff

关于bash - 使用 sed 取消注释代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44088604/

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