Shell Scripting Setup

Shell Scripting Setup for Mac and Linux

Shell Scripting SetupYou can do everything with shell scripts! Anything you can do from your terminal, you can do with a shell script, and if you like you can enable a cronjob to schedule the execution of your shell scripts. In this post, I’m going to cover the basics on getting up and running with shell scripts on either Mac or Linux systems.

Setting up Your Shell Scripts Environment

First of all a quick reminder these instructions are valid only for OSX, Linux and/or UNIX based systems. Moving on without any further ado, let’s make sure we are in our home directory:

 cd 

It’s common practice to store shell scripts inside a bin folder under your home directory and most Linux systems will have this already setup by default. On Mac systems you won’t have a bin directory in your home directory by default but you can easily create one by issuing the following command:

 mkdir bin 

Now if your name was Joe Smith and your home directory was /Users/jsmith you will now have /Users/jsmith/bin setup and ready for you to place shell scripts in it. Your home directory on Mac will always be the first letter of your first name followed by your last name (unless you have customized it).

Adding Your Bin Directory to The System Path

By adding your new bin directory to your systems $PATH variable you can execute your scripts from anywhere. While still in your home directory issue the following command:

 nano .bash_profile 

Note: Nano is installed by default on OSX and on most Linux systems. If you do not have Nano installed you can always install it using your package manager or use Vi.

With the .bash_profile file open in Nano (or Vi) add the following line:

PATH=$PATH:$HOME/bin

In nano press Control + O then enter to write the changes then Control + X to exit Nano. If you are using Vi type :w to write the file and then :q or you can alternatively do a Shift + z +z to do both at the same time.

For the changes to take effect type the following command and press enter:

source .bash_profile

How-To Test Your Shell Scripting Setup

First of all we need to change to your bin directory. To do this issue the following command and press enter:

 cd ~/bin 

Now let’s create a couple test scripts, we will create one that has your Mac speak the current time and another that will easily flush your DNS cache.

Issue the following command to open the Nano editor for the current time script:

nano time.sh

Now enter the following line into this script:

say current time is `date +%A%B%m%l%p%M`

Press Control + O then enter to write the changes then Control + X to exit Nano. Now you must also make this script executable so it’s allowed to run, to do this type the following command and press enter:

chmod +x time.sh

Now let’s setup one more script for testing, type the following command to get started:

nano flushdns.sh

Now enter the following line into this script:

sudo killall -HUP mDNSResponder; sleep 2; echo macOS DNS Resolver Cache has now been flushed; say macOS DNS Resolver Cache has now been flushed

Press Control + O then enter to write the changes then Control + X to exit Nano. Now we will also need to make this script executable. To do this type the following command and press enter:

chmod +x flushdns.sh

Now let’s move back to our home directory to test out the setup:

 cd 

Make sure your audio is enabled and turned up loud enough for you to hear then type the following and press enter:

time.sh

Your system should speak the current time to you if you have done everything correctly. You can also type flushdns.sh and press enter and your system will clear your DNS cache then speak a confirmation message to you stating ‘Your DNS resolver cache has now been flushed’.

Now you can get creative and create scripts for all sorts of tasks. I will cover how to schedule the automated execution of your shell scripts using cronjobs in a follow-up post.

I hope you found this guide useful. Please feel free to leave your comments, feedback or suggestions below. Thanks for reading!

DIGITAL PCI © 2018