Bash脚本:如果该行尚不存在,则仅对?/ .bash_profile回显一行
发布时间:2020-12-16 01:53:49 所属栏目:安全 来源:网络整理
导读:我写了一个 bash git-install script.到最后,我这样做: echo "Edit ~/.bash_profile to load ~/.git-completioin.bash on Terminal launch"echo "source ~/.git-completion.bash" ~/.bash_profile 问题是,如果您多次运行脚本,最终会多次将此行附加到?/ .bas
我写了一个
bash git-install script.到最后,我这样做:
echo "Edit ~/.bash_profile to load ~/.git-completioin.bash on Terminal launch" echo "source ~/.git-completion.bash" >> ~/.bash_profile 问题是,如果您多次运行脚本,最终会多次将此行附加到?/ .bash_profile.如何使用grep或sed(或您可能推荐的其他选项)使用bash脚本来仅添加文件中尚不存在的行.另外,如果该文件存在且?/ .bash_profile不存在,我想将行添加到?/ .profile,否则只需将其添加到?/ .bash_profile. 解决方法if [[ ! -s "$HOME/.bash_profile" && -s "$HOME/.profile" ]] ; then profile_file="$HOME/.profile" else profile_file="$HOME/.bash_profile" fi if ! grep -q 'git-completion.bash' "${profile_file}" ; then echo "Editing ${profile_file} to load ~/.git-completioin.bash on Terminal launch" echo "source "$HOME/.git-completion.bash"" >> "${profile_file}" fi (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |