Bash Shell Scripting Part 3 – Execution

Bash Shell ScriptingAt long last we have reached the third part in the bash shell scripting series, and can tackle the greatest achievement of all; execution of the script.

In order to execute a script, you have to have a script file to execute, so let’s start with a few words on the topic of creating that file.

Naming Conventions

We have talked about file naming conventions in the past, and many of the same rules apply when naming shell scripts.

Shell script names should be concise, descriptive, and not repetitive (assuming all three can be accomplished at the same time).

Shell script names should not include spaces.

One very important consideration, when naming a shell script, is to not give it the same name as an existing command. A shell script with the same name as a command will not only create confusion, but also has the potential to cause devastating loss and/or be used maliciously, as previously mentioned.

So, before naming a shell script, make use of the which command to find out whether a shell command with the selected name already exists. The which command “shows the full path of (shell) commands”, and so will return either the path of the command if it exists, or the equivalent of “no command in …” if it does not exist. In the case of the latter, you are free to name your script with the chosen name.

There are a number of opinions as to whether shell scripts should include a file extension (usually .sh), but suffice it to say that since it is optional, you can at some point make up your own mind about the necessity (or lack of). In the meantime, you may see a good deal of scripts both with and without file extensions, that execute without problems.

I choose to name my scripts with the .sh file extension simply because I tend to identify files by their extensions, and leaving it off makes them look unfinished.

Shell Script Creation

In keeping with the command line theme, we’re not going to be using a GUI text editor (Kate, etc.) to create shell scripts. Although there are no actual rules on the matter, using a GUI text editor just seems to defeat the purpose of writing command line scripts in the first place, doesn’t it?

On that note, we’re going to be using the VI/VIM command line text editor to create and edit scripts.

To begin, open up a shell and navigate to the directory where you want your script to reside (or move it later). Then, type in vi ScriptName.sh (except name your script whatever you want).

Type in your script (don’t forget the necessary components!) or copy and paste my example script, if you don’t mind a bit of sanctioned plagiarism:

#!/bin/bash
# My First Bash Shell Script
clear
ls -l
echo "Can you see me?"
echo "Yes? Ok, let's move on."
exit 0

Save your file with Esc and :wq (for example), and your script is created!

Executable Permissions

Now, there is one final hurdle to clear before you can cross the finish line; permissions.

Specifically, a script needs to have proper permissions to be executed.

You may remember some details from previous lessons about read/write/execute permissions. The user needs execute permission to execute a script.

Using chmod, you can give your script the necessary permissions several different ways.

Examples: chmod u+x GwenScript01.sh and chmod 755 GwenScript01.sh

Execution

At long last, the actual execution is a simple matter of typing ./GwenScript01.sh into the command line (assuming that the script is in your current directory) and seeing any results returned — or at least a lack of errors.

(See my dot slash post for more on the dot slash subject, and when/why it is necessary.)

Alternatively, you can execute a script by running it as a bash argument, in which case it would not need executable permissions: bash /home/gwen/GwenScript01.sh

Background Execution

Remember that the ampersand & can be used to start a process (in this case a script) in the background.

Example: ./program &

This can be useful in many instances, so is good to keep in mind.

Summary

Now that you have seen a shell script, broken down its components, and learned how to execute it, we can move on to bigger and better things.

In the coming weeks we’ll be learning about using variables, tests (conditions), and loops, as well as accepting command line and user arguments in shell scripts.

Leave a Reply

You have to agree to the comment policy.