Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
GNU-ize Mac OS X El Capitan
#!/bin/bash
# Install required packages from Homebrew
brew tap homebrew/dupes
brew install coreutils binutils diffutils ed findutils gawk gnu-indent gnu-sed \
gnu-tar gnu-which gnutls grep gzip screen watch wdiff wget bash gdb gpatch \
m4 make nano file-formula git less openssh python rsync svn unzip vim \
--default-names --with-default-names --with-gettext --override-system-vi \
--override-system-vim --custom-system-icons
brew cleanup
# Empty the .bash_path file that holds GNU paths
echo 'export PATH="/usr/local/sbin:$PATH"' > ~/.bash_path
# Build PATH variable script in ~/.bash_path
for i in /usr/local/Cellar/*/*/bin; do
echo 'export PATH="'$i':$PATH"' >> ~/.bash_path
done
for i in /usr/local/Cellar/*/*/libexec/gnubin; do
echo 'export PATH="'$i':$PATH"' >> ~/.bash_path
done
for i in /usr/local/Cellar/*/*/share/man; do
echo 'export MANPATH="'$i':$MANPATH"' >> ~/.bash_path
done
for i in /usr/local/Cellar/*/*/libexec/gnuman; do
echo 'export MANPATH="'$i':$MANPATH"' >> ~/.bash_path
done
# Check if .bash_path is being called from .bash_profile
PATCH=`grep "~/.bash_path" ~/.bash_profile`
if [ "$PATCH" == "" ]; then
# Add Ubuntu-style PS1 to .bash_profile
cat <<EOF > ~/.bash_profile
alias ll="ls -ahl --color=always"
export PS1="\[\033[1;32m\]\u@\h\[\033[0m\]:\[\033[1;34m\]\w\[\033[0m\]# "
EOF
# Add .bash_path to .bash_profile
echo "source ~/.bash_path" >> ~/.bash_profile
fi

TorgeH commented Apr 4, 2016

I have some issues with this code:

  1. you should be extra vigilant not to introduce empty entries in $PATH
  2. idem for MANPATH, but here it's needed inside the for-loops
  3. it is considered good practice to test for the existence of a file before sourcing it in .bash_profile
  4. your test weather .bash_profile is already patched is at the same time too specific and too fuzzy:
    1. it would match a line containing "~/Xbash_path" -- the dot being a placeholder in regexp
    2. it would not match a line conatining "$HOME/.bash_path"
  5. the variable storing the test-result if the .bash_profile is already patched is named counter-intuitively. "PATCHED" would be better.
  6. then again, there is no need to store the result in a variable at all, since "grep -q" can be used as condition to the "if"-block
  7. you could mention somewhere that your script rebuilds $HOME/.bash_profile from scratch without leaving a backup copy if it doesn't find the bash_path-line

HTH, just my 2 cents

ickc commented Oct 10, 2016

It currently will override my bash profile:

cat <<EOF > ~/.bash_profile

Can you changed it to

cat <<EOF >> ~/.bash_profile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment