In this tutorial we'll be writing our first bash script for Raspberry Pi. We'll create a directory to keep this and future scripts, write the actual script, and set it up as something that can be executed from the shell.
Gidday, Micheal from Core Electronics here, welcome back to the lab once again. Today we’re going to be writing our very first script for the Raspberry Pi’s. So, jumping over to the Pi, we need to create a directory to organise our scripts - this is often called the bin directory which is short for binaries. So lets create that now with mkdir bin and we can see the bin directory there so lets jump into it. Now to create our very first script, I already know what I want to call it. I’m going to call it first_script - so I can just call nano and the name that I want to call the script. Here we are in the nano environment once again. So the very first line of the script is called the hash bang line and it’s our way of telling the Raspberry Pi 3 what language we’re using and what interpreter is going to be executing our commands. This first line will look something like this - with a hash, an exclamation point and then bin, bash. So this tells the Raspberry Pi that we’re going to be using bash to interpret our commands. We can insert a comment to tell us what our script is going to do and that’s with a hash as well. So anything that isn’t that first line you can use a hash to insert a comment and this tells us, the programmer, what it is that the program is doing. It kind of helps us out in human readable form. For this first script, it’s very very simple so I’m just going to say - this is my first script this is a comment - and the only command we’re going to execute in this script is going to be the echo command.
This is just the way for the script to display text on the terminal for our - for user feedback. So I’m going to echo and the script we’re going to echo is Hello World. Its a very common first script. We can also insert in line comments so when there’s a line with code we can put in a hash and say this is another comment and this is what’s known as an inline comment. So this is our script essentially - all it’s doing is printing the words Hello World. We can Control X to exit - Y - yes we do want to save, and just strike enter because we’ve already named the script. Now if we execute ls with the l argument we can see our first script has been created. This text on the left here, the permissions for the script - permissions are to read - that’s the ‘r’, to write - that’s the ‘w’, and to execute and you’ll notice that there’s no ‘x’ for execute any where in these permissions. Permissions are arranged in three groups - the file owner, the owners user group - if you have many many users they can be arranged into groups, and everyone else who is not in that group.
So we can see that the file owner, which is us - Pi, we don’t even have permission to execute this script yet so we need to change that and we do that with the chmod command. So we execute chmod and the code to apply permissions for the file owner is u, so u - the file owner, we want to add which is a plus the execute permission which is an x. So we want to modify the file to give the file owner execute permissions. The file we want to apply this to is first script. So if we execute ls l once again we can see that an x has appeared, thats our execute permission and in fact the most noticeable thing is that the colour coding for the file has changed - this it telling us that it IS an executable file. So to run the script we need to run it from this folder so that it’s a dot and a slash to say in this folder I want to run first script and there it is! There’s our Hello World So that’s about it for this tutorial and just as a bit of a side note if we run nano again and we open up first script we can see that it’s applied some nice pretty colour coding to our script. This is just like syntax highlighting - so because this script is executable and it’s recognised as such as a bash executable nano has been polite enough to colour code all the syntax for us so that when we come back to edit this script it’s a little easier for us to see what key words are what. In this case we’ve got green for comments, blue for the echo command, and this pinky text here for the string Hello World.
So that is just to get you started on how to set up a script, a tutorial on how to actually write scripts and scripting will follow shortly.