加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读