Automation with shell scripts
What is a shell?
Shell is a term to describe computer interfaces that we use to interact with our operating system. Shells can be graphical or text based using a command line. We are typically using a graphical user interface, in other words a graphical shell to control the computer. When we open a command line window, also called terminal**,** a text based shell is started for us in that window. Using this shell, we can control the computer by issuing text-based commands.
There are several command-line shells that we can use, but the most common and the most useful to know is Bourne Again SHell, known as Bash. This is a shell commonly associated with Linux, but it is also a shell that we will get by default on MacOS and which we can install on Windows as well, e.g. by using Ubuntu on Windows.
There are alternative command-line shells that we can use, but for most of our needs, Bash is more than enough.
What are shell scripts?
When we talk about shell scripts, we are typically talking about writing scripts for the command line. A shell script is a type of executable text file which contains instructions what to do when the file is run. Such file can be opened and edited in a text editor and run using the command line.
A shell script can be very simple, only executing one command, or group many commands together with conditions, loops and other control mechanisms. It is typically used as a set of instructions that we want to run together, without typing or remembering them.
We refer to the command-line environment as Bash, but we also use the same name to refer to its command language. Therefore we consider Bash to be a programming language and we say that we program in Bash.
How can shell scripts help us?
Shell scripts usually help us with automating tasks that we want to do more than once. Some examples include:
- Startup scripts that will be executed after the computer or server is started
- Installation and configuration scripts that will setup environment for programs to run
- Scheduled file backups
- To run a combination of commands or commands with complicated list of arguments so that we don’t have to remember or type it every time when we use a command line interface (also known as aliasing, that is creating an alias for a more complicated command)
- Any tasks where it is advantageous to use Unix/Linux utilities together as opposed to using another scripting language like Python.
Our first shell script
To create an executable file for Bash, we need to do two things:
- Tell Bash how to run our text file using a special first line
- Change the system permissions to make the file executable
So let’s create and open a blank text file and let’s start typing:
This first two special characters together (# and !) at the beginning are called shebang. Following the shebang, we specify what program will execute our file. In the case of typical shell scripts, it will be a path to Bash in the operating system. Without this first line, Bash wouldn’t know that we want to run this file with Bash itself.
To demonstrate that our script has been executed, let’s add a Bash command. A good example will be to use command called echo, which will print its arguments to the console.
echo "Output from first shell script"
Let’s save our first script as print.sh. We don’t have to specify the file extension, it doesn’t affect the ability of the file to be executed, but serves as a way to identify shell scripts among other files.
Before we run it, we need to make this text file executable. We will do it by opening a terminal and navigating to the folder where we created the file with command called cd (change directory) and then using a program called chmod:
chmod u+x print.sh
With “u+x”, we only allow this file to be executed by the owner of the file (which is us because we have created it), but in case that we will be create a script that will have to run under another user, then we can make the file executable by everyone with “+x”. Note that it is best to only allow permissions to the users or group that need it, so you might want to study the permission system more for more serious computing.
How to run a shell script?
Now we can easily run this file in the terminal (assuming we are still in the same directory in our open terminal):
Output from first shell script
We can see our string “Output from first shell script” printed to the console.
Congratulations! We have just created and executed a shell script!
Are there any alternatives to shell scripts?
The most typical alternative would be to use a scripting language like Python, Ruby or Perl and then either make an executable file the same way as we did for Bash (replacing /bin/bash with path to the language interpreter and echo with a function call from that language), or using the language’s specific command and passing the script file as an argument.
It might look like we haven’t achieved much, but be sure that this is very powerful and widely-used automation technique and definitely a staple in our programming toolbox.
Last updated on 6.7.2019.