Skip to content

Instantly share code, notes, and snippets.

@wayneeseguin
Created December 18, 2010 19:07
Show Gist options
  • Save wayneeseguin/746767 to your computer and use it in GitHub Desktop.
Save wayneeseguin/746767 to your computer and use it in GitHub Desktop.
I wrote a bash git-install script. Toward the end, I do:
echo "Edit ~/.bash_profile to load ~/.git-completioin.bash on Terminal launch"
echo "source ~/.git-completion.bash" >> ~/.bash_profile
The problem is, if you run the script more than once, you end up appending this line multiple times to ~/.bash_profile. How do I use bash scripting with grep or sed (or another option you may recommend) to only add the line if it doesn't yet exist in the file. Also, I want to add the line to ~/.profile if that file exists and ~/.bash_profile doesn't exist, otherwise just add it to ~/.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment