我需要使用bash脚本在 zshrc 文件中添加新插件,为此我搜索包含 plugins =(sometext)
的行语法
plugin_text=$(grep "^[^#;]" zshrc | grep -n "plugins=(.*)")
直接在终端中运行我得到:
$ grep "^[^;]" zshrc | grep -n "plugins=(.*)"
38:# Example format: plugins=(rails git textmate ruby lighthouse)
40:plugins=(git python pip)
40是正确的行但是当我执行我的bash脚本时,我得到:
$ ./config-minimal
3:plugins=(git python pip)
我需要更改40行插入新插件。例如:
前
plugins=(git python pip)
后
plugins=(git python pip zsh-autosuggestions zsh-syntax-highlighting)
如何通过简单的方式获取此行并替换文本?
我的剧本
function install_zsh {
# aptitude install zsh
# sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
# Install zsh highlighting
# cd ~/.oh-my-zsh/custom
# git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
# Install zsh auto suggestions
# git clone git://github.com/zsh-users/zsh-autosuggestions
# TODO: Add options in plugins
cd ~
plugin_text=$(grep "^[^#;]" .zshrc | grep -n "plugins=(.*)")
new_plugins=${plugin_text/)/ zsh-autosuggestions zsh-syntax-highlighting)}
line_number=${plugin_text/:plugins*/ }
sed "$(line_number)s/plugin_text/new_plugins/" .zshrc
}
答案 0 :(得分:1)
您可以使用简单的sed 's/^plugins=(\(.*\)/plugins=(zsh-autosuggestions zsh-syntax-highlighting \1/' .zshrc
:
sed 's/\(^plugins=([^)]*\)/\1 zsh-autosuggestions zsh-syntax-highlighting/' .zshrc
或(thx @ 123):
-i
将function install_zsh {
# aptitude install zsh
# sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
# Install zsh highlighting
# cd ~/.oh-my-zsh/custom
# git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
# Install zsh auto suggestions
# git clone git://github.com/zsh-users/zsh-autosuggestions
# TODO: Add options in plugins
sed -i.bak 's/^plugins=(\(.*\)/plugins=(zsh-autosuggestions zsh-syntax-highlighting \1/' ~/.zshrc
}
标志添加到infile replacement。
{{1}}