Simple Script Example! Shell Scripting – Part 2

Published on September 9, 2022 at 5:08 pm by LEW

Introduction

In this post we will be covering the various parts of, as well as creating, a simple shell script to do some fairly basic stuff. I will be explaining how I go about scripting. Just remember, that’s not the end all. Everyone will develop their own way of doing things that works for them.

Many years ago, one of the classes I took in college was titled “Unix Shell Scripting”. This was kind of ironic, because the entire class was basically about using the terminal to run commands. The closest we got to actual scripting was writing some awk commands. I am sure the instructor was of the opinion that nobody knew anything about Unix, and I can sort of see where he might have gotten that impression.

In this case I am assuming you either know something about Linux shell commands, or are motivated enough to learn about them. So while we will be using shell commands, some of the explanations may be a little short, or refer to other material.

This series of posts is about writing scripts, so that is what we are going to do.

Creating a Shell Script

Shell scripts are files, and to write a shell script implies we have a file. So if you are in a GUI, fire up your favorite text . Otherwise fire up a terminal.

Which brings us to our first decision point, what do we call the file, and where do we put it. What follows is what I do. If you have a system that works for you, feel free to use it.

I create a working directory for my script. This directory will not only contain various versions of the script I am writing, but any other information I think is relevant. I usually create sub folders for informational stuff, while the various versions of the script sit in the main folder. For this series of articles I have created a folder called bash-sripts.

Within this folder I create a script file named test_script-v1.sh. There are a couple of points to explore.

Since I am working in a terminal, and starting in my home directory, this is my command sequence.

mkdir bash-scripts
cd bash-scripts

touch test_script-v1.sh

ls -l
chmod u+x test_script-v1.sh
ls -l

vi test_script-v1.sh

First I created a folder in my home directory with mkdir. I used cd to move into that directory. I created my script file with the touch command. I used ls to see if it was executable (it was not). I made it executable with chmod. Finely I verified the permissions again with ls.

The last command opens the script in the vi text editor. Feel free to use whatever text editor you want. Since this is going to be fairly short at this point I just entered it using a terminal text editor.

Writing a Shell Script

In this shell script there are a few things I want to accomplish.

This is my initial script. Take a look at it before we discuss the various parts.

#!/bin/bash
# Name:   Test_script         Version: 1
# Author: Retired Techie      Date:    09 SEP 2022
#
# Description: this code is being used to test and learn various
# shell scripting methods and capabilities.

USERNAME=$(whoami) # defines variable based on the whoami command

echo -e $USERNAME  # print out variable to screen

exit

Starting with the first line, we see the shebang (identified by the #! on the first line). This tells the system what interpreter to use. Looking in my /bin directory, I saw that I had both bash and sh. At this point I could have used either one.

Another way to do the shebang is to use environmental variables. We could type it like this.

#!/usr/bin/env bash

This would search the environmental variables for the bash command. We can do it ourselves with this command.

env | grep bash

On my system this returns SHELL=/bin/bash. This is suppose to be more portable, as not all Distributions put bash in the same spot. However it can also cause a few problems with the shell environment. You can use whichever one you want, just be consistent.

After the shebang, we have several lines of comments (denoted by the single #). I highly recommend using header comments to remind yourself what the file is and is suppose to do. In fact I recommend using lots of comments in your scripts. The interpreter ignores them, and since this is a text file it does not take up much space. So go a little crazy. You can always clean up your comments when you get to the final version.

After that we have our first line of actual code. This code basically sets a variable equal to the output of the shell command “whoami”. You can type this command in your terminal to see how it responds.

Our second line of code simply echos the variable we just created to the screen. The “-e” option forces the echo command to send a linefeed before finishing.

This program is simple enough that we don’t really need an exit command, but I like to include it anyway.

Running the Script

This may seem a little unusual for the new folks.. To execute a bash script we need to supply either a absolute or relative path. Below are the two examples, first full absolute path, and second relative path. Chose which one is easier to type (hint: relative path)

/home/retiredtechie/bash-scripts/test_script-v1.sh

./test_script-v1.sh

Conclusion

There we have it, our first script. It may be trivial, but it introduces us to some important concepts in shell scripting. Concepts we will expand upon in the next post, when we discuss command line arguments.

Shell or Terminal? Shell Scripting – Part 1

Simple Script Example! Shell Scripting – Part 2

Command Line Arguments! Shell Scripting Part 3

User Input! Shell Scripting Part 4

Using Functions! Shell Scripting Part 5

Variable Odds and Ends! Shell Scripting Part 6

Add New Comment

Your email address will not be published. Required fields are marked *