User Tools

Site Tools


Lesson 3 - while and loop

Storm Howdy. We have a lot to cover tonight,, so let's start out with variables.

storm variables contain information in the program. They are like the command line arguments but these you don't pass into the program.

storm the way you assign a variable is variablename=number or variableName="string" Here are some examples


Storm Spaces are not allowed between the variable name, the equals, and the value. this will fail

x = 2

Storm This will also fail:

fruit = "apples"

Storm Setting variables inside the program is all fine and well, but you also need to get information from the user. This is where the read command comes in.

read answer

Storm This will stop and wait for user input and save that input to the variable $answer

Storm So, for a good example, change to your intro-to-bash directory and open

Storm Basically we are going to rewrite the whole file. Remove everything between the #!/bin/bash and exit then add in these lines.

echo "what is your name?"
read name
echo "Hello $name."

exit 0

Storm now save and exit the file, when you execute it with:


Storm you should get:

What is your name?

Storm at this prompt enter your name, when you press enter you should get:

Hello Storm.

Storm Next, we should do a little math. Variables are great for math, you can do calculations and store them in a variable. But first, we need to learn the math. There are 2 ways I know for doing math.

Storm The first is with the expr command. I really don't like this command myself because it can be so tricky, but to use it, you can do:

expr 2 + 2

Storm if you do this at your prompt you should get 4, if you do:

expr 2 * 2

Storm You get an error, you have to do it like this:

expr 2 '*' 2

Storm the * has to be inside single quotes. I am always forgetting that and getting errors, which is why I prefer the second way I am going to show you:

echo "$((2 + 2))"

Storm gives 4 just like the expr command.

echo "$((2 * 2))"

Storm will also give 4 without the '' around the *, so now, you can do pretty much any math. Just remember that division by 0 will give an error. If you use the % you will get the remainder of division.

echo "$((5 % 3))"

Storm Will give an answer of 2. So, let's play with variables in a script.

Storm If you will create the file and enter:

thirdNumber="$(($firstNumber + $secondNumber))"
echo "The sum of $firstNumber and $secondNumber is $thirdNumber"
firstWord="3 headed chicken"
echo "even though the variable firstWord has a number in the beginning it is a string because of the word and the quotes surrounding it. the variable contains $firstWord."
exit 0

Storm Now, if you remember from our first lesson, you have to change permission of the file before executing it.

chmod 700

Storm then execute with:


Storm Now, as we all know, sometimes you have to check and find out if things are equal to, less than, or greater than something else. In bash there are 2 very different ways to do this.

Storm If you have had any experience with programming in pretty much any other language you will find the way you compare strings in bash familiar.

"apples" == "oranges"

Storm means are apples equal to oranges.

"cat" == "car"

Storm Of course this is not true, well maybe sometimes it is true, but that is usually messy and never pleasant for the cat. The cat is usually a whole lot happier with this statement.

"cat" != "car"

Storm Cat is not equal to car.

Storm With numbers though the syntax is quite a bit different but is still very easy.

3 -eq 3

Storm Does 3 equal 3

3 -gt 5

Storm is 3 greater than 5

3 -lt 5

Storm is 3 less than 5

3 -ne 5

Storm is 3 not equal to 5

Storm The way you use these are in loops and in the if statement. Here is an example of an if statement:

if [ "cat" == "car" ] ; then
echo "awww man, I kind of liked that cat."

Storm first comes the if, and inside the brackets, which are also known as test, comes the statement we want to test.

Storm In this case is the word cat equal to the word car.

Storm This is not true, so nothing will happen.

Storm the word fi, which is if spelled backwards, closes the if statement.

Storm But what if we wanted to have a reaction if cat does not equal car. That's where the else statement comes in handy.

if [ "cat" == "car" ] ; then
echo "Awww man, I kinda liked that cat."
echo "Wow, that was a close one."

Storm Now, this if statement is more prepared. It tests to see if cat is equal to car. The test will fail, and we'll get the text

Wow, that was a close one.

Storm Now, For the final part of this lesson, the while loop. Combined all we have so far, and we are ready for the while loop. It is similar in syntax to the if statement but does something quite different. Take this program for example

while [ $i -lt 10 ] ; do
echo "$i"
i=$(($i + 1))
exit 0

Storm Does anyone wanna take a guess at how high this will count and why? It counts from 0 to 9 because the variable starts out as 0 and increments each time until it reaches 10.

Storm but the condition of the loop is to only increment while it is less than 10. So it is finished and does not echo the number 10.

Storm You can make it equal to 10 with one simple change. change -lt to -le

Storm -le is less than or equal to

Jeff got it

Storm We have covered quite a lot this time. Are there any questions?

Storm For next time, see if you can write two programs.

Storm the first should count from 2 to 20 in lots of 2s

Storm the second should count from 20 to 0 by 2s. Go backwards.

Storm actually, let's add a third.

Jeff what have you in mind for the third?

Storm The third will accept 3 arguments from the command line. The first is up or down, the second is the starting number, the third is the ending number. The program will count up or down from the number. to the second number. Extra points if you can do it with 2 arguments only.

Jeff I'll see what I can do!

Storm jeff: for counting backwards you will want the - which is a minus.

$((2 - 1)) is 2 minus 1

Storm I also hang out on #vinux on, so I can answer questions there too.

bash_class3.txt · Last modified: 2017/11/11 22:47 (external edit)