What is a script?
You can think of it in the context of a play, television show, or movie. The actor takes the script, and executes each and every line. The script tells them what they can and cannot say. In the context of shell scripts, our computer is the one reading and following the directions.
A shell script, in essence, is a plain text file which contains a series of instructions that are then carried out by the operating system. It is also known as a program. These instructions are usually commands we type ourselves, like find, ls, or touch. There are times however, when we need to automate a series of large, tedious commands. Never fear, for there is a large device sitting in front of us that is incredibly good at such mundane tasks. This is where the power of shell scripting comes into play.
Shell scripts are generally created for useful tasks that may need to be used in repetition, but require the same sets of commands each time. Not only that, but we also create shell scripts for commands that would normally not be inputted manually on to the command line. These include if/else/switch, conditionals, variables, and functions.
When you create a plain text file and enter a list of commands, you have created a shell script. The computer will proceed to read the script and execute it sequentially. This can be done via the command line. Let’s create a simple shell script together.
During this tutorial, any parenthesis shown are meant to represent html tags.
Create a Basic Shell Script
In the command line, create a new file in the present working directory and name it anything you’d like.
Now that we have a new file, let’s populate it with instructions! The first line we need to put tells the current active shell to execute the script inside the Bash environment. Without it, bash would have no clue what to do with our script. Here is the line below.
Now that we’ve established a working relationship with the Bash environment, we can begin doing whatever we want with the script.
Remember, anything that you would normally put on the command line can be put into a shell script.
Executing our Shell Script
Before we are able to run our script, we need to give it adequate permission, otherwise you will get an error saying that permission has been denied. To circumvent this, we simply use a single command that tells the operating system our script is safe to run. The chmod command allows you to change the permissions of files and folders, and the +x flag grants our script the permission required to execute successfully.
Now that we’ve cleared up the permission conflicts, we can proceed to run our script! To run it, simply type:
If the script has run successfully, you will see the commands being executed. This may be harder to detect visually depending on the nature of the script.
As you execute a shell script, you can add additionals arguments to the end of the line. These arguments can be used within your script to deliver dynamic outputs. The example below shows a quick example of how paramters might be used in a basic script.
Here, we have a script called webify-name that takes in an argument and outputs it to a html document in h1 headers. I use parenthesis on the “h1” instead of tags because WordPress freaks out with the formatting.
The program will look for the $1 parameter, and use it to execute the script. Let’s try running it!
The program sees that the string benjamin is passed in as the $1 parameter position. Now, every single $1 in our script is essentially replaced by the string benjamin. We have made a dynamic program!
As we’ve learned, $1 represents the first paramter position. Below is the format for adding in additional parameters.
Using these parameters, we can create cool little commands that carry out useful actions and save lots of time!
Alright! Let’s take what we’ve learned so far to create a bash script that sets up a basic website folder template.
Though there is a maximum number of shell script parameters acceptable, it is a very large number, so you will never have to worry about reaching a limit.