You may on occassion feel the need to automate certain processes or proceudres on your linux based computer. These maybe simple processes like ensuring your packages are updated daily or even creating a useful utility to install libraries onto many computers.
These are occassions when writing and executing
BASH files are extremely useful, to help reduce effort required. In this post I will provide a short tutorial on how to write and execute a bash script on ubuntu.
What is a BASH script
A Bash script is a ASCII (plain) text file which contains a series of commands. BASH is an acronym for “Bourne-Again Shell”, and is widely distributed as the shell for the GNU Operating system and is the default shell on Linux and OS X. Shell, is a program that takes your commands from the keyboard and passes them to the operating system to perform.
BASH commands can consist of multiple commands we would normally type ourselves using the terminal window, that you would like to execute together. You don’t need a complicated tool to create BASH file, any text editor will suffice i.e. Gedit – the text editor shipped with ubuntu.
For a great reference to the Bourne-Again Shell check out The Bourne -Again Shell
How to write a BASH script
Instead of starting off with a regular “Hello World” sample, I’ll provide with you a useful script you can run everyday to keep you ubuntu computer. If you’ve ever used the apt-get commands to do this process, you’ve partly written the contents of this BASH script.
To develop your BASH script simply open Gedit, your text editor shipped with Ubuntu and enter the following code snippet.
This script should be fairly familiar to anyone who has frequently used to terminal to update their computer to using the latest packages. This is a very simple bash script, starting off at line 1 with what is called a Shebang, what this does is tell the shell to interpret this file as a script and use the BASH. The following lines simply update your package repositories, upgrade any packages that require updating and then update your distribution packages. The last line simply removes any outdated packages.
To save your bash script, simply File –> Save As … –> update.sh
There is a convention to give Bash Scripts a .sh file extensions. Although Linux is an extensionless system, this extension is primarily for the Users benefit to enable them to identify bash files in a directory.
How to execute bash scripts
This tutorial assumes you have saved your file to your Home Directory. If you haven’t saved the file to your Home directory then you will need to change to the directory you have saved your file to by using
We’ll just need to change permissions on the file to allow execution by users.
There are two methods to execute a bash script, we could execute the file by explicitly telling the system to use BASH to execute the file. You can use this method if you have omitted #!/bin/bash at the begining of the file:
Our script still need Super User permission to execute because we are executing Super User functions. We can execute our script without the bash extension.
Our script is now available to execute anytime we would like to update our system. We can now also further improve our script with additional steps we need as time goes on. Below is a sample of the script I execute regularly on my machines to keep them up to date incorporating removal of all unused Linux kernel headers, images and modules from ubuntu boot
This script essentially does the same as above, but with additional step of removing any outdated linux kernels, free-ing up disk space, once again I run the update procedure and finally we check whether we should reboot the machine.
Although this is a very simple example of a bash script, it does serve to illustrate how to use bash scripts to automate mundane and time consuming tasks. As a programmer, you’re trained to identify and automate, improve or eliminate inefficient processes or tasks and to implement efficiencies and improve productivity, bash scripts are a tool to help you quickly and easily implement changes quickly.
A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.