Module #09 - Bash Scripting II
This module is the 2nd half of our introduction to Bash Scripting. This time we are going to focus more specifically on control flow with conditional statements and loops.
This will allow you to recombine all the tools we studied so far, and learned to insert in a script in the last module, with the control you are used to in any programming language.
Topics Studied in this Module
- T1 - Testing Conditions
- Most scripts will need you to test things. Does the filename you were given as parameter actually exist? Did that grep pipeline we just executed succeeded or failed? This topic will show you how to implement such tests in Bash scripts.
- T2 - Repetitive Tasks
- We are going to add to our scripts the ability to repeat groups of instructions multiple times. In programming, this is refered to as iteration. We are going to learn to iterate a fixed number of times, until a condition is met, or simply over a list of words resulting from the expansion of a Bash variable; e.g. $*
- T3 - Interactive Scripts
- While you may write many scripts in order to just automate some tasks based on a few parameters, there are times where your scripts will need to either get information interactively from the user while they run, or read it from other files. Guess what we are going to study in this topic?
Learning Activities
This module features the following learning activities; (see announcements for details)
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
We are going to, as with the previous module, leverage one of Ryan Chadwick Linux tutorials.
T1 - Testing Conditions
- Reading Assignment
- URL Link: Ryans Chadwick's Bash Scripting Tutorial
- The following sections are assigned for this module;
- If Statements
- File Link: YouTube
- This video explores the different available syntaxes for if statements in Bash scripts. It also introduces the essential concept that all if statements work by executing commands and looking at their returned error codes that they interpret as truth values. Once you understand this aspect of Bash, the syntax we will use in the next videos will make much more sense.
- Test syntaxes
- File Link: YouTube
- We then look at the test builtin command itself, and its more natural syntax using [...].
- Case statement
- File Link: YouTube
- We conclude with a brief overview of the case statement.
T2 - Repetitive Tasks
Quick note about the videos below; Do not be alarmed if the audio seems absent, it is indeed absent :) I am trying my hand at a different type of live coding videos where the narration is provided in written as comments to the code. I have seen it done with courseware related videos and figured it would give a break to some of you from my accent :)
Let me know if you like it or not on the forums! I know preference on such matters is highly subjective so I would not be surprised by a 50% like 50% dislike result but it always help to get feedback from students to realize what works and what does not.
- Reading Assignment
- URL Link: Ryans Chadwick's Bash Scripting Tutorial
- The following sections are assigned for this module;
- While & Until Loops
- File Link: YouTube
- We start our exploration of Bash loops with the while and until syntaxes.
- For Loops with Ranges & Lists
- File Link: YouTube
- We then explore the for loops with both ranges and lists.
T3 - Interactive Scripts
- Reading Assignment
- URL Link: Ryans Chadwick's Bash Scripting Tutorial
- The following sections are assigned for this module;
We continue our hands-on practice with more exercises focusing on more advanced features
Exercise #1 - howmany.sh - version #1
Let us start with a script which will define a variable containing a small integer of your choice. This value will represent the number of positional parameters the script expects to be passed. We want our script to display a message stating whether the expected number of parameters was passed or not.
Here is an example of how it would be used;
tux@LinuxBox > ./howmany.sh one two three Sorry, you passed 3 parameters but I expected 5 parameters tux@LinuxBox > ./howmany one two three four five Master gave Dobby the right number of parameters!
Exercise #2 - howmany.sh - version #2
Let us improve our howmany.sh script by enabling it to tell us whether we provided too many or too few parameters.
Here is an example of how it would be used;
tux@LinuxBox > ./howmany.sh one two three Too few parameters! You gave me 3 parameters but I expected 5 parameters tux@LinuxBox > ./howmany one two three four five six seven eight nine Too many parameters! You gave me 9 parameters but I expected 5 parameters tux@LinuxBox > ./howmany one two three four five Master gave Dobby the right number of parameters!
It would be silly to have the same message in two different echo statements, right? Make sure that the part of the two messages telling you how many parameters were given vs. how many were expected is not typed twice in your script. There are different ways to get this done, explore a bit
Exercise #3 - paramsloop-v1.sh
Write and test a Bash Script which will displays all of its parameters by iterating over them with a for loop based on expanding the list of parameters first.
Here is an example of how it would be used;
tux@LinuxBox > ./paramsloop.sh one two three one two three
You will use different ways to expand the list of parameters;
- using the $* syntax
- using the $@ syntax
- using the "$*" syntax
- using the "$@" syntax
What are the differences you observe? Which of the above syntaxes are equivalent and which are necessary in some scenarios?
Exercise #4 - paramsloop-v2.sh
Write and test a Bash Script which will displays all of its parameters by iterating over them by using a while loop with shift.
Here is an example of how it would be used;
tux@LinuxBox > ./paramsloop.sh one two three one two three
Exercise #5 - paramsloop-v3.sh
Write and test a Bash Script which will displays all of its parameters by iterating over them by using a until loop with shift.
Here is an example of how it would be used;
tux@LinuxBox > ./paramsloop.sh one two three one two three
Exercise #6 - whatisthis.sh - version #1
Write and test a Bash Script which, given a single parameter, a file name, will display one of the following messages
- "Java Source File" if the file suffix is .java
- "C Source File" for .c
- "C++ Source File" for .cpp
- "Ada Source File" for .ada
- "Python Source File" for .py
- If the file doesn't have one of these suffix, your script will simply display "Unknown Source File"
Here is an example of how it would be used;
tux@LinuxBox > ./whatisthis.sh something.diff Unknown File Suffix tux@LinuxBox > ./whatisthis.sh Something.java Java Source File
Please note:
- Use the CASE statement
- Do not verify that the parameter given to your script is the name of a file that actually exists. For now, just take the name given to us and look at its suffix.
- In order to get the file suffix out of a string containing the whole filename, you might want to read section 10.1 Manipulating Strings of the Advanced Bash Scripting Guide.
Exercise #7 - whatisthis.sh - version #2
The next version of our whatisthis.sh script will also verify whether the file actually exists and display a message accordingly. In addition, if the file exists, it will display the number of lines it contains
Here is an example of how it would be used;
tux@LinuxBox > ./whatisthis.sh something.diff File not found, Unknown File Suffix tux@LinuxBox > ./whatisthis.sh Something.java File not found, Java Source File tux@LinuxBox > ./whatisthis.sh Something.java File exists! Java Source File with 23 lines
Exercise #8 - repeat.sh
We want a script to repeat a given command an arbitrary number of times.
Here is an example of how it would be used;
tux@LinuxBox > ./repeat.sh 5 "echo something" something something something something something
To write this script, you will have to do a bit of research on the eval bash syntax as it is not covered in the assigned readings.
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