The full Raspberry Pi Workshop in step-by-step format can be found here http://coreelec.io/piworkshop In this section we will be investigating Variables and Decision making logic in Shell Scripts

Transcript

This time around we're going to work with some variables and run a simple loop, this should feel pretty similar to programming in Python, because of course, the logic is, all the same, it's just the syntax that is different. So on the desktop environment, this time, I'm going to edit graphically so there's my chapter 3 directory and there's the argument script that we wrote in the last section, we can see that it's being given this gear symbol to indicate that it is a script. So I'm just going to right click and create the new empty file and I'm going to call this decision, or decision, we can see that because I've just created a file it doesn't have that icon, but when I make it executable it'll become an icon just like arguments. So if we open that up, this might just be a little bit easier to write out the script in. So, of course, we have to start off with the #! and I'm just going to put in a comment saying what the script does, "Demo of Decision Making" that will do, so let's first create some variables and to do this we can just assign them, so we can say A=5 and B=2, don't put a space in between the equal sign in the two or the B and the equal sign that will be interpreted as if-if it was a command. So when we're doing arithmetic like this we shouldn't have spaces.

Going down to the next line we could now do some arithmetic with them, we can say C, now to access a variable we use the dollar symbol so we could say C equals A times B, but that wouldn't be quite correct just yet we would have to actually graph that in two brackets, and then put a dollar symbol out front again, which is just it's just hard to look at, so there's an equivalent and much nicer looking statement called Let and we can say Let C = A * B no dollar symbols, no brackets, just exactly the arithmetic that we want. I've just shown you this first one just in case you come across that and it looks quite intimidating there's a lot of syntax and a lot of ugly characters going on but it's just equivalent to saying let C = A * B, that C should be 10. So now we can do some logic with our variables, let's do an if statement so we can say if, and now an if statement looks like this we have an if, a space, and we have some square brackets that has a space next to them, so everything's padded out. And we can say let's check if a is greater than B so we can say, a -gt b, so you need this space right here and this space right here and the spaces in between. that is very syntax sensitive. So we've got our If, the next line we can say then, and this is where we come in a bit and we can say do something and then the if statement is finished off with a fi, so if and fi, I guess that's just a nice mirroring of if to say that's the end of the if statement.

So let's put some logic inside this let's put a command inside this if statement, let's just echo the result, echo a -gt b, that's fine. I'd like to put in a few other commands, we can say then let C = 1, and we're not necessarily done yet, we can also put in an else so we can put in an else statement here and this will execute whenever this expression is not true. So if you decide to change these variables we will get some different behaviour. We could say echo a -lt b, and we can set C = something else, okay so that's our if Statement which has a sequence of commands to do if true and a sequences can have to do if false, now that we've modified C, we can write a loop that uses C and that way the-the behavior of the entire program will change as you play with those initial conditions for A, B and C so we could say loop ten times, and I've chosen 10 because I've got C is equal to 1 here so that's going to be my counter for the loop I'm going to count C from one to ten and we'll use a while loop so just like an if statement it's a very similar expression. While and while loops will execute so long as the expression is true, which means that if we want to loop 10 times for C, we can say while c is less than or equal to 10, so you can put in a literal number there, and I'll show this in a different way, rather than going onto the new line and saying do we can put in a semicolon and enter do and now we're inside the loop so we can echo, and we're going to echo just the count number of how many times we've looped, and of course so this loop doesn't go on forever, we have to actually increment C. So we could say let c = c + 1 that's that's fine a much shorter way to write that is to say let c++ and that plus-plus syntax just means increment the variable C and then we finish off our loop with done. 

So again we can chmod u +x to add owner execute permissions, and then we can apply it to a decision and I'm not going to type it all out I'm just going to use a wildcard there. So if we -ls -l again decision is now executable so let's have a run of decisions. Okay, that looks reasonable, so of course a is greater than B because a is 5 and b is two and then we have a loop of 10. So let's have a quick look perhaps what happens when a is not greater than b, let's make B = 200, well I'm just going to save that so you can get a file save or i just pressed the ctrl s to save. Let's run that again and see what happens, okay, so this time around we've gone from five to ten instead of one to ten and of course that has to do with our if statement we entered the else condition and that set C equal to five and we were still using C for the counter in our Loop, so C is now starting at five and it's counting up to 10. That completes our workshop on decision making we will write a bit of a more useful script in the next video.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.