Shell Scripts: Save previous development time!

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.

touch is-pizza-good

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.

#!/usr/bin/env bash

Now that we’ve established a working relationship with the Bash environment, we can begin doing whatever we want with the script.

#!/usr/bin/env bash
echo "YES" > the-answer.txt
echo "The answer awaits you inside this directory!"

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.

chmod +x is-pizza-good

Now that we’ve cleared up the permission conflicts, we can proceed to run our script! To run it, simply type:

./is-pizza-good

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.

"The answer awaits you inside this directory!"

Parameters

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.

#!/usr/bin/env bash
#!/usr/bin/env bash
echo "Preparing to webify the name $1"
echo "(h1)$1(/h1)" > name.html
echo "The name $1 has been webified!"

The program will look for the $1 parameter, and use it to execute the script. Let’s try running it!

chmod +x webify-name
./webify-name benjamin

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.

./default-shell-script $1 $2 $3...

Using these parameters, we can create cool little commands that carry out useful actions and save lots of time!

#!/usr/bin/env bash
echo extracting $1 into $2/backup2…
mkdir $2/backup2
tar -xzf $1 -C $2/backup2
echo done!

Alright! Let’s take what we’ve learned so far to create a bash script that sets up a basic website folder template.

#!/usr/bin/env bash
#!/usr/bin/env bash
echo Creating a new file structure of a website at $1/src

mkdir $1/src
mkdir $1/src/js
mkdir $1/src/css
mkdir $1/src/images

touch $1/src/js/main.js
touch $1/src/css/style.css
touch $1/src/index.html

echo "(h2)$2(/h2)" >> $1/src/index.html
echo "h2 { color: gray; }" >> $1/src/css/style.css

echo Finished!

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s