Using Shell Scripts To Automate Your Workflow


Photo by Markus Spiske on Unsplash

If you work with computers, there has probably been a time where you thought: “Wow, there has to be a way to automate this task!”.

A computer script is a list of commands designed to be executed by a program. They are used to automate tasks such as data analysis, webpage generation, and system administration. Because scripts can do so much, the ability to understand, create, and use them effectively is one of the most highly sought after skills in the IT world.

So, let’s dig in to the topic of scripts. A good place to start is a discussion of shell scripts – what they are and why you should use them. Next, we will discuss how to write and run a simple shell script on our own.

What Is A Shell Script?

A shell is an interpreter that takes in commands from the user and performs actions using operating system services.

For example, when you type the command ls into the interface, you will see a listing of the contents of the current directory. Gathering and displaying that information is an example of the shell interpreter at work.

The shell interpreter can also read commands from a file that the user has previously written and saved. This is called a “shell script” – a file composed of a list of shell commands.

How Are Scripts Used?

Instead of creating scripts, you might be thinking that it would be faster and easier to just use the interface directly. Why should you write and store scripts instead of simply typing in commands into the terminal?

The reason is that shell scripts, or any type of computer scripts, are very useful for managing complexities and for automating recurrent tasks.

If your commands involve multiple input parameters, or when the input of one command depends on the output of another, things tend to get complex very quickly. Handling these complexities manually is not only time consuming, it’s error-prone. The more things they do manually, the greater the chance of a programmer making a mistake.

On the other hand, you might have a list of commands that you need to execute repeatedly. Scripts are useful in these situations as well since they eliminate the need for you to type in the same commands over and over again. Instead, you can simply run a script and be done with it.

Writing Your First Script

Let’s get started with our first script! Open up your favorite text editor and follow along!

The first line

The first line of your script should be the “shebang” line. It starts with a hash character (#) and a bang character (!) and it declares the interpreter for the script. This allows the plain text file to be executed like a binary.

This first line indicates that we are using shell as our interpreter:

#!/bin/sh

Add commands!

For our first task, let’s make our program say “Hello” to the user.

In Unix, the command echo prints out its arguments to standard output. So, to make our program say “Hello!”, we add a line to our script:

#!/bin/sh

echo "Hello!"

Users can also provide input arguments in shell scripts. In shell syntax, $1 is the variable name for the first argument passed in, $2 is the second argument, and so on…… And $@ is the variable name for all arguments passed in.

When using arguments (and variables in general), it is important to surround them, or the string they are a part of, in double quotes. Otherwise it might break things, like spaces in file paths, or cause security issues.

Let’s say we want our script to customize its welcome, so we allow our users to pass in their names as arguments. We will then print out a custom welcome for them:

#!/bin/sh

echo "Hello $@!"

echo "Welcome to the script :) "

At this point, our simple script is complete! Save it in your current directory with the file name sayhi.sh. (The .sh extension is conventional for shell scripts.)

File permissions

For security purposes, most files are not executable by default. You can make the shell script executable by running the following command:

chmod +x sayhi.sh

The chmod command edits the permissions for a file, and +x indicates that we want to add the permission to execute for all users.

Executing the script

Now that the script has been created, it’s time to run it! You can do that by executing the following command. (Note that we are passing in Vickie as the first argument, and Jennifer as the second argument!)

./sayhi.sh Vickie Jennifer

You should see the output of the script printed out as:

Hello Vickie Jennifer!

Welcome to the script :)

Conclusion

That’s a quick, first look into the creation and use of shell scripts. Whether you’re a programmer, system architect, or system administrator, shell scripts are extremely useful in helping you to optimize your workflow.

Happy scripting!

Vickie Li