Ok so I forget to update my package managers all the time – and I made a mess of some dependencies. So I put a stop to it by writing this shell script.
First off I started out just having iTerm do it automatically by giving the commands to update on start. This was pretty long winded and I didn’t want all that going off every time I opened up my terminal to do some work, which is getting to be all the time now.
So what I needed was something to remind me to update and not automatically do it every time.
This is for Mac (you could do something similar on Linux), my set up isn’t too complicated I use Oh-my-Zsh for ease (it’s super useful, and pretty) and iTerm2 because regular terminal is boring.
You don’t need either of these for what I’m going to show you but well, you should take a look at them if you’re not using them already.
cd ~/usr/local/binnote: If this doesn't work you can put it in the main /bin folder but you might have permissions issues either way
Create a new file here, no extension, with your preferred text editor. If you use emacs or vim you’re probably not going to be reading this tutorial so we’ll assume nano, or subl or code.
Name this file whatever you want to update command to be.
$ sudo nano update_pkgs
and then write out something like this:
$ #!/usr/local/bin/sh echo "Do you want to update your package managers? (Y/n)" read ANSWER if [ "$ANSWER" == "y" ] then echo "******Updating npm*******" npm update echo "******Updating Ruby Gems*******" sudo gem update echo "******Updating Homebrew*******" brew update echo "================ DONE =================" else echo "Not Updated..." fi
The #!/bin/sh denotes the language.
Then there’s a Y/n question. Yes I’d like to update my packages. Or No, I’m busy and I don’t want to mess with those files right now.
Then an if statement, if you answer Y it will go ahead and update your chosen package managers, and echo out what it’s doing as it goes, with an finish message at the end.
You could even put an ascii cat in at the end.
/\_/\ ( o.o )
Save that out. Now set the permissions with
sudo chmod +x update_pkgs(or whatever you named the file)
To get this to run when you open your terminal, if you’re using iTerm, open your user profiles with cmd+o and where it says “send text at start” enter in ‘update_pkgs’ or whatever your command is called. You can always add this to your aliases and run it when needed if not
No iTerm? Find your .bash_profile or just .profile and paste in your command, should run it on start.
This is a basic shells script, but I didn’t know much about them before, other than how to use them - so this was a good exercise.