Module #08 - Bash Scripting I
This module is going to integrate almost everything we learned so far in order to allow you to write bash shell scripts meant to automate command line tasks.
To get you started with Bash scripting, we will focus on very simple scripts in which you list all the commands that you want to execute, in the order you want them to be executed. We will also supplement this by looking at how we may use variables and perform some simple arithmetic operations in Bash.
Please note that, due to the nature of Bash scripting, this PA will be time-intensive so make sure to start working on these scripts early in the week so that you have time to ask for help.
Topics Studied in this Module
- T1 - First Scripts
- We are going to get you started in the world of Bash scripting with some small scripts which do not feature any intricate "control flow".
- T2 - Arithmetics
- We review different syntaxes allowing us to perform elementary arithmetics with Bash.
Learning Activities
This module features the following learning activities; DF, PA, GQ
Linux+ Exams Objectives
This module deals with material related to the following objectives from the CompTIA Linux+ and LPI certifications exams.
- Exam LX0-102 - Objective 105.2 - Customize or Write simple Scripts
This module is going to introduce you to Bash scripting. You will see how what you studied in the COP2512 pre-requisite may be readily leveraged to allow you to solve programming problems while also leveraging all the CLI tools we studied so far.
To this end, we are going to use one of Ryan Chadwick Linux tutorials.
T1 - First Bash Scripts
Let us get started with some very basic scripts that just allow us to sequentially execute a bunch of commands. We will also learn how to access the arguments passed as parameters on the command line to your scripts.
- Reading Assignment
- URL Link: Ryans Chadwick's Bash Scripting Tutorial
-
The following sections are assigned for this module;
- What is a Bash Script
- Variables(This one should be a review)
- First Scripts
- File Link: YouTube
- This video will guide you through the steps of writing and executing your fist bash scripts.
- Working with Parameters
- File Link: YouTube
- We now take a look at the syntax used by Bash to work with the parameters passed at the command line to your scripts.
T2 - Bash Integer Arithmetics
When writing Bash scripts, you will often find yourself in need of performing some rudimentary integer arithmetics operations. Fear not, Bash provides you different tools to do so.
- Reading Assignment
- URL Link: Ryans Chadwick's Bash Scripting Tutorial
- The following sections are assigned for this module;
- Bash Arithmetics with expr
- File Link: YouTube
- We get started with Bash integer arithmetics tools by exploring the syntax of the expr external command.
- Bash Arithmetics with let
- File Link: YouTube
- Next, we turn our attention to the let built-in command.
- Bash Arithmetics Substitutions
- File Link: YouTube
- Last but not least, we work with both the ((...)) and $((...)) arithmetic substitution syntaxes.
A long time ago, in this very same galaxy, system administrator had limited selection of scripting languages. Variants of shells such as Bash, Ksh, Zsh, offered some differences but all boiled down to the same core features.
Nowadays, a few interpreted languages have progressively made their way into the system admnistrator's toolkit. This Discussion Forum assignment will help you get an idea of which ones are job interview stapples and which ones are worth keeping an eye on.
Topics - Review of SysAdmin Scripting Languages
The idea of this DF is to have you discuss with one another the pro and cons of adopting a specific scripting language. You may use what you have learned about Bash and Bash scripting to compare a scripting language of your choice and present to your classmates an overview of its pros and cons. Put yourself in the shoes of an IT professional who has been asked by his team to prepare a brief summary of what is good and what is less good about a given scripting language. The underlying goal is to educate your colleagues / classmates on how the scripting language you picked might be a good language to learn for a new system administrator hired by your company.
To get you started, I will mention a few obvious candidates; Perl, Python, Ruby... but you should make an effort to explore lesser known scripting language which have recently shown some promise for system administrators.
Requirements for your Posts
Your post will establish, by comparison with Bash, how suitable for system administration scripting the language you considered is. Your post should, at the very minimum, address the following considerations;
- How easy is the scripting language to learn initially? How easy are the scripts to write and maintain? Be specific about the features facilitating either.
- Are there books, tutorials, or video tutorials focussed on how to leverage that scripting language specifically for system administration tasks?
- Are there specific libraries available in that scripting language that support system administration tasks? Are these integrated in the language or available as third party add ons?
- Does the scripting language feature the concept of modules in one form or another? Is there an easy way to install new libraries or modules in the form of a pseudo package manager? Are dependencies handled by hand or automatically?
- Are there any system administration software that was written using this language? This is generally a good illustration that the language is fit for these types of tasks.
This PA will have you practice your recently acquired Bash scripting skills. For each of the following exercises, implement and test the corresponding Bash script.
Please note that, unlike all other PAs, this one will be assigned as a group assignment.
Since this is a group assignment how you submit will be slightly different. Only the group leader should submit, submissions from other team members will be ignored. The PDF submitted will have to include the names of all the team members on the front page. Screenshots only need to include the name of one of your team members, there is no need for you to include the names of everyone in your group
Everyone in each group is expected to make significant contributions. However, how you go about working on the assignment will be up to you. If you are having trouble with a member of your group, you need to contact the TA immediately about it and clearly explain the problems you have been having and any steps you have taken to resolve them. If no such report is received, the TA will assume that everything went well and that your submission reflects the best effort of everyone in your group.
Exercise #1 - Warming up with Hello World
Write a bash script to echo the string “Hello World!”. Call the script hello.sh, an example execution is as follows:
tux@LinuxBox > ./hello.sh Hello World! tux@LinuxBox >
Submit both your code and a screenshot demonstrating that the script functions correctly. As usual, make sure your full name appears in the terminal for your screenshots.
Exercise #2 - parameters.sh - version #1
We need a bash script named parameters.sh able to display all the positional parameters that were passed to it. Here is an example of how it would be used;
tux@LinuxBox > ./parameters.sh one two three four five one two three four five tux@LinuxBox >
Notes;
- We want to get this done by using a single echo statement
- Try using both the $* and $@ bash special variables
Make sure that the output of your script exactly matches the example, otherwise you will lose points. Submit both your code and a screenshot demonstrating that the script functions correctly. As usual, make sure your full name appears in the terminal for your screenshots.
Exercise #3 - parameters.sh - version #2
This exercise is OPTIONAL, you do not have to submit anything for it.
Let us improve our parameters.sh script by allowing it to display a message stating how many positional parameters were received, followed by the list of parameters between parenthesis.
Here is an example of how it would be used;
tux@LinuxBox > ./parameters.sh one two three four five Received 5 parameters (one two three four five) tux@LinuxBox >
Note;
- You should still be able to get this done using a single echo statement
Exercise #4 - reverse.sh – version #1
This exercise is OPTIONAL, you do not have to submit anything for it.
We want a bash script named reverse.sh which is able, given two words as positional parameters, to display them in reverse order.
Here is an example of how it would be used;
tux@LinuxBox > ./reverse.sh first next next first tux@LinuxBox >
To do so, write and test a Bash Script which will;
- Assign its first parameter to a variable named FIRST
- Assign its second parameter to a variable named SECOND
- Displays these two variables one after the other, starting with SECOND
- Use two echo statements, one for displaying each of the variables
Exercise #5 - reverse.sh - version #2
We want to improve the above reverse.sh script by allowing it to display the two parameters, still in reverse order, but on the same line.
Here is an example of how it would be used;
tux@LinuxBox > ./reverse.sh first next next first tux@LinuxBox >
Please note the following;
- We want a space between the two parameters
- We do not want the next shell prompt to be on the same line than the parameters
- We could get this done by using a single echo statement but we already practiced this so avoid it here
- Instead, still use two echo statements, one to display each parameter, but search the manpage for details on how to use echo -n
Make sure that the output of your script exactly matches the example. Submit both your code and a screenshot demonstrating that the script functions correctly. As usual, make sure your full name appears in the terminal for your screenshots.
Exercise #6 - multigreps.sh - version #1
We are working on a task where we often find ourselves using grep to look for the lines of a file containing three specific words. Please do not ask what this task is :)
For example, let us say that we are working with a textfile named myfile.txt that contains the following;
This first line only contains an irrelevant sentence. This second line is really not much better but contains the word bananas. Now, the following line has an interesting question about a movie; Which dreamworks movie features fictive creatures obsessed with bananas for one hour and a half? That was it, no more interesting lines about dreamworks in this file...We are looking for lines containing the words "bananas", "dreamworks", and "movie", then we would get this done by doing something like;
grep bananas myfile.txt | grep dreamworks | grep movie
There are better ways to get this done with grep. However, we are going to spare ourselves the redundant syntax by writing a script
Here is an example of how it would be used;
tux@LinuxBox > ./multigreps.sh myfile.txt banana dreamworks movie Which dreamworks movie features fictive creatures obsessed with bananas for one hour and a half? tux@LinuxBox >
How to get it done?
- The first positional parameter must be the name of the file we want to grep, possibly with its full path
- The 2nd, 3rd and 4th positional parameters must be the three words we want to grep
- Your script must invoque the grep tool as illustrated above
Make sure that the output of your script exactly matches the example. Submit both your code and a screenshot demonstrating that the script functions correctly. As usual, make sure your full name appears in the terminal for your screenshots.
Exercise #7 - multigreps.sh - version #2
Let us improve our multigreps.sh script. After it is done displaying the results of the grep, we want it to now also display the exit status of the grep pipeline we just executed
Here is an example of how it would be used;
tux@LinuxBox > ./multigreps.sh myfile.txt banana dreamworks movie Which dreamworks movie features fictive creatures obsessed with bananas for one hour and a half? The search exited with status 0 tux@LinuxBox >
Out of curiosity, try using a data file and parameters which yield no results to see what the exit status value is in different scenarios
Exercise #8 - Using Variables
Write a bash script that takes 2 integer variables and outputs their sum, product, and difference. Name the script variables.sh, it would be used like this:
tux@LinuxBox> ./variables.sh 1 1 Sum = 2 Product = 1 Difference = 0
Assume that the user will always enter correct arguments. In other words, you do not have to worry about error checking. Submit both your code and a screenshot demonstrating that the script functions correctly. As usual, make sure your full name appears in the terminal for your screenshots.
This tab provides you with optional resources meant for those who want to learn a bit more about the topics covered in this module.
Machtelt Garrels' Bash Beginners Guide
If you are interested in getting a bit deeper into Bash scripting, we recommend you study Machtelt Garrels' Bash Beginners Guide. The following links will provide you with a local PDF copy and the URL of the website where you might find the latest version. Please note that this document is freely available as part of the TLDP - The Linux Documentation Project.
- Resource - Bash Beginners Guide
- File Link: Bash-Beginners-Guide.pdf URL: TLDP website