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 how it looks when I open up iTerm now.
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 /bin note: It would probably make more sense to put this in /usr/local/bin
Create a new file here, no .txt 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. Name this file whatever you want to update command to be. Mine is ‘update_pkgs’.
sudo nano update_pkgs and then write out something like this:
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)
So 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.
No iTerm? Find your .bash_profile or just .profile and paste in your command, should run it on start.
This is basic stuff, but I didn’t know much about shell scripts before, and really only wanted to do this, and couldn’t find a guide that wasn’t TL;DR.
Credit: Thanks French Beardy Man