Introduction to Bash Scripting
Chapters
If you are looking for a beginer's course for Bash scripting, This course is very well for you. I've covered some of the most important topics to get you started. Course contents include
- Welcome message [Free]
- What is Bash
- Command line shortcuts and tricks
- Getting started with Bash profiles
- Bash Expansions
- Command Redirection and Piping
- Using Echo and PrintF and how to preventing command expansion
- Understanding local and environment variables
- Bash script syntax
- A basic Bash script
- Working with numbers and operators
- Working with arrays
- Controlling output with awk, grep and cut commands. How to use basic bash color themes with your output.
- Working with here document and files
- Flow control statements and loops
- Introduction to functions
- Interacting with user
- Closing note
I hope that you will learn heaps from this course, please leave your feedback and improvement suggestions.
Author: Subject Coach
Added on: 23rd Jan 2015
Please get in touch with your teacher or tutor in case you have a question related to this lesson
None just yet!
In this chapter, we will learn how to use flow control statements, such as If and Case. Then we will explore looping. Many times we would want to show different output, based on if something is right or wrong. If and case commands does exactly that to help us out. Let's explore these commands and how we can utilize them in our scripts.
If statement takes an expression and base on if the condition is true or false, certain code within a condition will run. If statement is closed by FI at the end of the command logic. If expression is false you can execute another set of code with else option. We can add multiple condition checks by using else if! "ELSE IF 'expression' is executed if expression before it evaluates to false, and condition within else if expression evaluates to true.
Couple of example for if command are, if A equals A or if 0 equals 0. I will now open control.sh with nano, to show you how we can utilize if in actual script.
Let's start by declaring variable called count with value 80! In my If condition, I am check if count is equal to 10, if yes! then 10 is 10 will be printed, else the next condition will be evaluated, in next condition, I am checking if count is greater than 10, if yes! then count is greater than 10 will be printed. If above 2 conditions fails then default string count is less than 10 will be printed.
I will save this script and run it. It will correctly print 80 is greater that 10. This is how you use If statement. Remember! if you prefer, You can move then command to new line after if expression semi colon.
Let's use strings within If comparisons. I will open my control.sh script again with nano, and initialize a variable called mystring with value hello world. Inside If expression I will use equals negation symbol to tell interpreter that I am using regular expressions. Regular expressions is a big and powerful subject, I recommend that you should learn regular expressions, if you are not aware of what they are and how to use them. Here I am just check if characters ORL are present in our string or not. If yes! then print present message, else print not present message. I will save this script and run it. It correctly prints that ORL is present in hello world string.
Moving on let's explore case command. You open a case command with case, followed by word, followed by in keyword. Each case is an expression matching a pattern. The closing round bracket terminates a pattern list. Each case plus its according commands are called a clause. Each clause must be terminated with ";;". Each case statement is ended with the ESAC statement, which is reverse of word case. The commands under a matching clause are executed.
I have done this script to demo case command. In this script I have variable character with value one! Then I am feeding this character to case command and then logic jumps to clases. I have three clauses,
first! if character matches one, then character is one will be printed.
Second! if character matches two, then character is two will be printed.
Third! if character does not match any pattern, then logic fallbacks to default option, which is represented by star. If that happens then character should be one or two will be printed.
Let's save this script and run it. If correctly prints character is one. Now I will update value of character variable to two! Save my script and run it again. It correctly print character is two. I will modify my script again to put character value to 3. Running my script again produces the result I was expecting, which is the fallback option.
Flow is also control by different supported looping commands, such as, while, until and for.
The while executes a piece of code if the control expression is true, and only stops when it is false. can also explicitly break the loop. Here is a syntax for a while statement.
The until loop is almost equal to the while loop, except that the code is executed while the control expression evaluates to false. We can also explicitly break the loop. Here is a syntax for a until statement.
The for loop is a little bit different from other programming languages. Basically, it let's you iterate over a series of 'words' within a string. Here is a syntax for a for statement.
I will now open until.sh script with nano. Let's initialize a variable called counter with value 20. I will now use until statement to loop till counter is less than 5! I will decrement value of counter by one on every interation. If counter is not less than 5, then keep printing the current counter value. I will save this script and run it. I get the expected result. So, once the condition becomes true, control will exit from the loop.
I will now open while.sh script. While command is opposite to until as discussed before, here I will initialize variable counter with value one! and in my while statement, I will loop and echo current value of counter variable till value of counter is less than 15. For Each interation, I will increment value of counter. I will save this script and run it. We get the expected result.
I will now open for.sh script. For command can take output from another command. Here I am creating sequence from 1 to 10 with SEQ command. Loop will iterate 10 times and then control will exit the loop. Let's save and run this script. We get the expected result and values 1 till 10 gets printed.
This was just an introduction to flow control statements, but there is so much more to explore. You should keep reading and learning new techniques, and keep trying various combinations.
In next chapter, I will talk about how to unitlize functions.