 What is what is this? What is that? What does that question mean? Well you many different answers. Well, what does it show us? Well first it shows us student at Netlab 24 Tilda dollar, and I think you've been typing commands. You may have seen it change sometimes What does it information does it show us student is the username? I'm logged in as student At Netlab 24 is the host name the name of my computer. I'm actually logged into the computer at the back Yours would be Netlab one two or whatever the computer number is So that's the name of the computer Call on the Tilda represents your current directory Tilda remember here is your home directory slash home slash student. This is just a shortcut and Dollar means from now on everything beyond that you type. This is called the prompt Okay, and the prompt in this case can provide some useful information who I am What computer I'm on? What directory I'm in? Why do we care who I am? I could log in as someone else. I can actually I don't know if you can but I can switch to Instructor I Need the password you could probably guess it Now I'm logged in as instructor on this computer and now it shows me I'm instructor so that's useful information in that it Reminds us who we're logged in as This reminds us what we're computer. We're on you've already used secure shell to log into other computers at the start to run This max to view my terminal sometimes you forget you run a command You think you're doing it on your computer, but you're actually logged into another computer So this is a little bit of a reminder which computer are you logged into and? This is a reminder of where you currently are your path This is called the prompt and what we type beyond the dollar sign is what is executed It's log out of Instructor. We can change the prompt very quickly First The things that we're going to do Most of the commands that I've that we're going to demonstrate are on one of these two web pages And these are linked to from the Moodle workshop You go to the workshop the resources there's A list as prompts and scripting and W get PDFTK and so on those two websites I list most of the commands were about to do this afternoon, so It may be useful now to open the website and you may better even follow along there We will not do all of them. So this one on aliases prompts and scripting Has some information about manual pages man pages. We will not do that now. Have a read-through in your own time Then it has something on There's some more new information there aliases we may not do that at all We'll scroll down a lot Shell prompt okay, so the things I'm about to type some of them Most of them are on this website. So you have your own reference for later Echo we saw towards the end this morning that echo displays something on the screen What something the the the parameter echo. Hello prints. Hello on the screen, but in the shell Right, what is the shell when I say the shell? The language that this terminal Interprets we refer to as the shell So The commands that we have here And the the way it outputs things the shell is the piece of software that that manages all of this In there are different shell Programs the one we're using is called bash B. A. S. H. Born again shell, but there are others now The shell is like a language. It's almost like a programming language. We'll see some constructs later But we can have variables think of of them environment variables in Windows you may have set the path the path is an environment variable We can do the same thing in In Linux and other Unix operating systems So echo. Hello prints the string. Hello, we can create a variable Whatever I like to call it like this and we can echo The contents of that variable This is not about the prompt. This is just something about the shell. I can create a variable ABC By assigning it to some value in this case a string Steve and I can display or I can Refer to the value of that using dollar followed by the variable name We'll use them as we go through some examples a variable Sometimes we enclose the variable with Sort of the the full ways to enclose it with curly braces the same thing dollar followed by the variable name or dollar in Closed in curly braces the variable name Just two different syntax so There are some variables which are defined in the shell already. I just defined ABC and I can define others But there are some already in but already defined environment variables one of them is the format of the prompt and It's called ps1. So the variable name that defines the format of the prompt is ps1 some strange string Echo dollar ps1 some strange string Forget about this Debian ch root and so on Again, we don't have time to cover everything look at the last few characters It tells us the format at which the prompt should be printed on my screen and I've known I started before that Slash you means display the username at Slash h means display the host name the computer name colon slash W's display the working directory and Then slash dollar means show an actual dollar sign because it's a special character So these characters define how the prompt is displayed We can change them ps1 equal to I'll put it in quotes You can change it to whatever you like You can define it as just a string or you can use these special characters to get particular values So ps1 is just a variable which defines the structure of the prompt If you look through the man pages, you'll see the definitions of these special characters and many others That's not what I'm doing today. You can Find them in your own time. That's just a quick explanation of the prompt and let's change to maybe Something different. We need a dollar. I think we should do dash Okay, so change the prompt to whatever you like in this case. I've changed it to just display the username Followed by a dollar sign space and then what I type in That's the prompt in the website. There are a few more examples of the prompt which will not go through you can change the color So the terminal In the basic form is black and white or just two colors but most Computer systems support multi-colored terminals, so we see the different colors of the directories and files We can also change the color of the prompt I'll copy and paste one from the website so they don't have to Waste my time typing it. I think I've made a mistake on the website too What maybe not we can change the color okay? Amazing with this long set of commands Some of those strange characters define the color this slash e in brackets one colon 34m defines the color to be blue of the prompt Again, I copy that from the web page. Remember to copy and paste go to the web page select You don't need to right-click just select Go to your terminal and middle click now. We have a beautiful prompt Some things that this afternoon will go through quickly just to make you all aware of those things like you're aware that this is the prompt and You change it by setting ps1 Exactly how to do it what possible values you can set. I'll let you explore in your own time we We will not go into any more detail the website That web page has some more details and also will point you to where to find more information That's the prompt Can I go back to normal? What was it at the start I think it might have been this if you want to go back to the original one you can Copy it from the website or much easier exit Type exit the terminal will close when you start the terminal again. It will go back to the original So the changes only apply It's not permanent only applies during this terminal to make it permanent We'll see how later It's not too hard. We'll see it with something else So for you I will not do it, but you can exit and start a new terminal another variable When I run ls ls is a program It's an application. Where is it? Which ls tells me ls The executable file in windows. It would be an exe file ls.exe here. It's just ls is in the slash bin directory Why does my shell know when I type ls? Why does it know to execute ls from the bin directory? There may be a copy of ls in other directories It knows from what we call the path the path defines What directory should we search to find an executable? And it's just another variable If you echo dollar path Dollar path is the variable dollar refers to the value echo will print out that value shows A set of directories separated by colons to say that When you try and run a program the shell will look in These directories in order it will first look in home student bin and then I've got home student bin again. That's a mistake And then these other directories I set something up yesterday that Made home student bin repeat. So this is the path You can change it I don't suggest you do Now just make sure that you do have home student bin in your path And at the start some of you may not if I didn't set the computers up correctly. This is saying When I run a program The shell will look in this directory first to see if that program exists If it doesn't exist there, it will move to the second directory if not there It'll keep going if it doesn't exist in any of these directories it will return an error command not found Normally home student bin is not in your path I have added it because we have some software that we're going to create ourselves that We'll put in this bin directory that we want to use during this afternoon Anyone not have slash home student bin at the start of their path Not have it Who doesn't have it twice? Having it twice. It shouldn't be there twice. That's my mistake But it won't hurt If you want to add something to the path maybe Echo will take a Let's try I had a variable we'll do it something else. I had a abc Echo you can combine variables and strings so I have my variable abc which is defined to be steve If I try to echo dollar abc. Hello, it doesn't return anything it prints out empty space that didn't work But if I enclose abc in the curly braces echo abc followed by the string. Hello. It prints steve. Hello So it's very easy to combine Values together can concatenate the strings We can do the same with a path sorry Yeah echo dollar and that's why we should put things in curly braces If you had a new directory called new bin I'm just making it up. We could add it to the path That is we can concatenate the existing path And add on this other string at the end which would include a new directory and to set that We would say Path equals existing path plus The old path Plus the new directory Set the path To be equal to the existing value of path Combined with this new directory Which would effectively add that to the To the path Just a reminder Of course, you can look on here But you should be able to see what i'm typing via the terminal if you're using mux But if you shut it down or for some reason not working Anyone not have the terminal open? If you if you Okay, you can see it. All right. I think most people are okay Just make sure So instead of having to see up there mux is working. Okay, all right good Just to make sure Are you going to set your path? Press enter now and that second that directory will be added to your path I don't want it there. So i'm not going to press enter But you can do it. It won't hurt Okay, we'll do it Now my path is everything as it was before plus slash home slash student slash newbin Which means when I run a program what the shell does it searches through all of these directories to look for that program Ask questions as we go. Otherwise, we'll just quickly move on to the next Next thing is that a question at the back or is that a yawn? I think it's Okay Now what we're going to do is put the commands that we've been running Remember we run a command presenter run a command presenter put them inside a file and run that file a text file So I'll just clear we'll start again We use my text editor nano for you You may want to have multiple terminals open that is three terminals Two of them for your computer and one to see what i'm doing Sometimes it's useful to have two terminals maybe open an editor in one and execute in the second but I will not just for the limited screen space Let's create an example If I can spell No example example one remember file extensions mean nothing in Linux It doesn't matter what file Extension I add it doesn't matter if I don't include a file extension It doesn't have to be dot txt In this case. I'll just call it example one Nano is our text editor like notepad And now we can put commands in there What cd home ls ls minus l That is I want These three commands to be executed when I run this example one file Rather than having to type each command myself Put them in a file and run that file that will run those commands So I've just put three Commands as an example should take me home cd tilde means go to my home directory should ls and then ls in the long format Exit control x Save modified bust it buffer Yes, why for yes file name press enter You want to check if it's there cat Just shows you the context Now, how do we run it? I want to run the commands inside example dot one. Well, the name of our shell is called bash And the way to run one way to run This example dot one is to type bash example dot one Remember to use tab bash e tab auto complete Bash is the shell which will execute in this case should execute the commands inside this file Does it Yes, you see the output of those three commands The first command had no output. It just moved me into a directory The second command it's not so nice on my screen, but the second command was ls It shows these files and the third command ls minus l which shows those files in long format There's what's called a shell script a script Which allows you to automate tasks Any problems with shell scripting? Very easy Just put your commands inside a text file and run that text file Did it not work for someone anyone? Or maybe a better question who got it to work put your hands up? Yes, sure Okay, if you didn't get it to work Did it run yep, okay Very easy now Let's Cover a few more details about those shell scripts and to see a few different Things we can do inside them I'm going to open mine in my text editor again nano example one It's good practice So we can just include the commands But it's good practice to in fact include a special instruction at the top of the file to say Which shell program to use to execute this? And that's a very special syntax the first line should be hash exclamation mark And then the the exact path to the shell script the interpreter And we're going to use bash for all our examples If you don't know what this means don't worry just use it in the top of all your shell scripts It doesn't have to be bash. It can be others. In fact others may be better in some cases But we will use bash for simplicity in our case Hash is normally a comment in shell scripts Anything after a hash is usually a comment in this special case of the first line When it's combined with the exclamation mark. It's not a comment It's a special instruction saying If you execute this file use bash to execute it Don't use some other interpreter hash exclamation mark hash exclamation mark is called bang So this combination is called shebang hash bang shebang So include that at the start of your scripts When you execute it control x save yes enter Again Run it you get exactly the same nothing has changed with regards to the output Let's do something different let's move The example 1 to file into your bin directory Remember move We'll take this file And put it into another directory so it moves it doesn't copy We're doing this because Our path includes the bin directory What we want to do is be able to run this command Without Having to type bash example 1. I want to just type example 1 So we'll set that up move it into bin And let's cd into the bin directory There's already some programs in there from Some I've installed ffmpeg An example 1 is there Again something that you may not understand but you need to accept for today with limited time We need to make this program executable Currently example 1 We are allowed to read it We're allowed to write it We're not allowed to execute it We want to be able to execute it How do we make it Executable This magic command Change the mode such that the user can execute example 1 And now we see an x here All right again Permissions changing the mode is something that we may cover in the lab in semester 2 for those that haven't taken it Or in something like its 335 it security if you're an it student But we will not cover today You see the colors changed to green that's a nice sign Example 1 is now an executable file And because it's in the bin directory And the bin directories in my path I can just type example 1 And it executes Sorry So you should be able to just type You don't have to type bash example 1 just type example 1 If it works Change the mode now Where's the command sorry All right I need to scroll sorry Something that I did which we didn't explain much You must do this chmod u plus x example 1 make that program make that file executable Then you should better run example 1 Anyone else have problems So we now have our own program Example 1 Which actually just calls other programs ls and cd Let's do some more things with a script Let's copy example 1 to example 2 We'll go through a few examples A quick one Now let's edit example 2 nano example 2 Open up example 2 in your text editor And let's change it to be something more interesting Sorry, what did I do then? In nano to delete a line I can do control k cut control k You can delete normally Let's create a variable Don't copy me Set a variable to some value The first line here And then echo some string My name is followed by the value of that variable save And execute I'll leave it up there for a moment. You can save that execute exit nano and execute example 2 save A quick way to save is just exit control x save yes Enter Execute example 2 All right, so this is just our use of variables Same on the command line. We can use them in scripts Next The shell We've all we've done so far is run a command and We get the output Well, it has more complex syntax and it allows different programming constructs It allows what we'll use four loops if statements while loops and many other constructs So we'll just introduce four loops and then if statements and we'll do it inside the shell script I'll just keep copying in my example to a new one Just so I have a record of the old files copy example 2 to example 3 And then edit example 3 I don't want those lines. Let's make something more interesting Four loops the syntax. There's different types. There's different Uh types of syntax for four loops. Let's go through a few common ones four two Left braces Four for example i equal to one. This is similar to c syntax i less than three i plus plus where i is our variable semi colon to close Do and I'll use a tab just to indent. You don't have to echo dollar i done A simple for loop So we're just trying to introduce you to the syntax what you put inside here Well, it can be as complex or as simple as you like Here we're just echoing the value of i You could put in your own commands ls copy files. Whatever you want to do Inside that for loop So the syntax two Left parentheses i equal to one semi colon i less than equal to three semi colon i plus plus to increment i Yep, why would you not have two? How many do you want? Just one Why Why not a square bracket a square brace? Why not a hash? It's just the syntax Okay Now why did they choose so the designer of this language? Why did they choose this syntax? I don't know have to ask the designer But I suspect it's something to do Brackets are used a lot in shell for other purposes So I suspect using a single one may have been conflicting with other purposes of the bracket Okay In fact, I think this is a new addition relatively new compared to the early versions these this syntax That's why we're here Well, no, let's execute Save control x save and and run your example just to be sure you know what's happening example three magic one two three Once it's run Then edit again and we'll add a few more examples of the for loop syntax Anyone not get one two three Okay at the back All right Then open it again And we'll add some different syntax another syntax for Some variable in some list list of strings for example semi colon to close do Whatever you want to do Echo the name done So just a different syntax for the for uh the the loop conditions This is the typical C where you increment a counter for example here for name in this list of string values So in the first instance name will be steve and then it will be tuneruck and then bikini And I think we've got one more We can use similar syntax And this will introduce a new shell operator Instead of defining the list here steve tuneruck bikini Sometimes it's useful to define those lists of values in a file say a text file one per line and then Get the for loop to read that text file and one way to do that I'll write it I forgot to create the data Actually, let's go back and create the data I'll come back to that one Let's save our file If you've got that line there, that's okay, but I want to create the data first Save the file exit We'll come back to it in a moment And now let's create some fake data data one dot txt. I'll call it dot txt nano data one dot txt And let's put some data in it and you can either type it yourself or if you can go to the website I have an example I'll show you If you again the website is linux command line aliases prompts and scripting Scroll down Find our examples of shell scripting Example where'd we get to data one dot txt I'm going to copy and paste these three lines So if on the website if you don't want to type it you're lazy like me you go to the website copy Which is just select I'll go back to that and go to nano and middle clip paste So on the website under four loops, there's an example that says cat data one dot txt I'll zoom in under four loops This is the data I want in the file. It means nothing Okay, it's just something we use as an example If you can't copy it you have to type something yourself Just those three lines starting with one two three ending with ghi So your file data one looks like that save it Yes, just to check cat data one Okay data one contains those three lines of text. What are they? Just some random one or some some strings separated by commas We'll use those commas as field separators in a moment Three lines with three fields each Now go back to our example three and finish our for loop Open the example three in nano Press enter And we'll add one more for loop the third for loop And again if you're lazy you can go to the website and copy the for loop Just make sure everyone has that so far Okay All right, and then we'll go back to the the For loop again, if I Go too fast and you need to let me know or just ask one of your neighbors for a bit of quick help Back in my example three. Let's add the third for loop for line in cat Data one dot txt Semicolon do And we'll do what we want to do But let's first look at this syntax So line is just the variable we can call it what we like it doesn't have to be line It can be abc anything it's a variable And it's going to take the values of the output of this command note these These characters called the the back tick operator sometimes back tick so the the Apostrophe leaning backwards. It's not a normal apostrophe. It needs to be the correct one And cat data one dot txt. What does cat data one dot txt do it just displays the contents of that file Putting it inside these two back tick operators means that The output of this command those three lines of file will be taken as the The information for this for loop And it's read line by line So the effect will be and we'll see it is that Line will take the first line of this file Then in the next iteration the second line of the file and then the third line of the file There should be three iterations there one for each line of data one dot txt If line is confusing rename it, uh, I don't know l It doesn't have to be line Let's do something with each line Echo the line and pipe it into Cut cut take some string cuts it based upon some field delimiter Our field delimiter minus d is the comma and the field that I want is field number two Remember our our lines were delimited by commas Save and run and just make sure it does what you expect it to do Use the website to get the exact syntax if if i've gone too fast there you can get it from the website Did it work? Okay easy Okay, so we take L will take in the first iteration the first line And then the second iteration the second line and the third iteration the third line We'll echo a line And then we'll pipe that into cut Which if we delimit by commas will take field two So the cat data one dot txt means Execute cat data one dot txt and the output of that By using these two back tick operators these two Means the output of that will be the input or as if we typed the output of that here on the On the in the file We'll see another example of those back tick operators. I think shortly they're quite important Let me run mine You should see something like that depending on how you change the values three four loops The last one prints the second field of those Three lines of that file Of course inside your for loop for loop you put whatever you like We're just using simple things for this just to learn the syntax of the for loops Let's keep going example four Copy example three to example four Edit example four Uh And we'll show some cases of if statements The first one we'll do it inside a for loop Instead of printing one two and three inside the first loop. Let's include an if statement Let's introduce a variable. I'll call it cutoff two f's cutoff And set it to two I should be one two and three our cutoff is going to be two Let's introduce the syntax for if statements if And they're quite complex. Well, not obvious sometimes Compared to other programming languages if i dollar i is the value is less than minus l t less than the cutoff and That condition is enclosed in square brackets and they're important They tell the if statement to to test This statement to test if i is less than cutoff And I think you need the spacing correct here I think if you don't have a space after the left after the The left square bracket before the dollar it may not work then If that is true then Echo some message dollar i is less than cutoff else else if elif So else if Do another test dollar i equals cutoff then Echo some other message dollar i is Same as cutoff I'm typing it myself. Sometimes I make some typos and make mistakes. We'll find them later If you don't want to type it yourself, you've got two options copy it from the website You'll get everything or just copy from the terminal as I type All right If you don't want to type it then copy it from what you see in your other terminal But try and understand the syntax and else Echo something else dollar i Is not less than cutoff You don't need the semicolon at the end of these echoes Won't make any difference Close the if with fi What's the opposite of if fi And we have our first example of if statement. So this is one syntax where we're comparing in numbers So dollar i is an integer is a number And cutoff is a number So we can compare them with less than equal to and you guessed that there's gt for greater than there's le for less than and equal to or equal to and a few others Shall we run it run it just to make sure you've made no mistakes You can save control x Yes Example four, okay So i iterates from one two and three inside each iteration A foot the if statement tests the value of i against our cutoff It's just introducing the syntax Yeah, best to do it here There are other ways to do it, but this is just A quick way to get started Any questions? We work our press fg control x Yeah, the first three lines Oh dollar cutoff not percent cutoff If you if you make typos then sometimes you'll get syntax errors or or strange results So you just need to be careful as with any Programming to get the syntax correct Okay, let's quickly get a couple more if statements. Let's continue to edit that file example four That was one form of syntax where we compared numbers We can do string comparison In our second for loop where we listed the three names if and i'll enclose it in double quotes if name And a good practice is to include the curly braces If name here we use equals to do string comparison Then echo fi closes the if statement Okay, so there are no I don't know curly braces like in c to open and close we use fi to close then echo something and While we're here Let's add another one. So this one is just comparing strings so we can use equals Minus eq to compare numbers equals to compare strings And it's good practice to have everything in double quotes to make sure it is a string Because if dollar name was an integer Then we'd be comparing a number against a string which may be a problem So it should say you should iterate through the three names and when it gets to tanaraki or echo out a string last one Here we did What do we do? Here we did a cat on data one dot txt But what if the file did not exist if there was no such file data one dot txt Then this script would return an error if we try to cat a file that's not there So it's good practice to check and see if it exists before we use it And we can do an if statement if And minus e means if the file exists Then This is not very useful Because what if it doesn't exist But i'll just introduce the syntax then we'll fix it Minus e is a special operator is used for testing about files and directories Minus e means if the file exists So if data one dot txt is a file and it is present in the directory Then this will return true and we'll echo it exists If there's no such file then this conditional statement will return false Maybe we'd like to do not If it doesn't exist Then if the file is not there, but we want it to in the next for loop Then we should exit not the exclamation mark is a uh an Negation here. So if data one dot txt does not exist if I've got the syntax correct Then echo this error message and exit this script Don't continue That's what this exit means. It means we were not going to the for loop We shouldn't because the for loop relies on this file if it doesn't exist then we should stop I may have made a mistake with the syntax. We'll come back to it. Let's test and see if it works Run example four Tanarak is my boss and it shows those values And let's move data one to be data two Therefore data one would not exist Then it handles that case I'll show you the if statement in a moment, but just check if it does what you expect When you see syntax area something's gone wrong. Don't continue Look on open up the website And to get things clear copy and paste from there to overwrite. So you're back to a A working example So the idea is we check if a file exists Before we use it Everything works, but you're getting errors to me. That means not everything works Errors warnings and errors are not good. Okay What's it say? Control c Maybe a space here Try to use I'm not sure, but I think a space there Yeah That fix some minus e space before the minus e Make oh no, okay All right line nine so nano And control c will tell you the line Line three so you scroll down a very primitive Scroll down Try a space after elif I'm not sure. I think it's acceptable if Something's wrong Try different Again No, there's a problem at the start After the echo a space echo Yeah Save Still an error down a line 16. Okay. I think you've got two done For only the for loop needs are done So the second done How do you change Similar if you go to my the website, there's a example about the prompts and but I don't try it now. It's it's quite confusing But you you'll find it later Line 15 again. They're done Only one done You need one done for one do Okay, yep delete one Delete done Yep And here as well Yeah Yeah Okay Okay To find the line number inside nano you can use control c and it'll show you the line number So if you've got an error scroll down press control c scroll down scroll Okay control c Line 36 so The minus e in that conditional statement Was a special character a special Operator to say if a file exists. There are many others if a file if it's a directory if it's File of a particular type To find out all of those Operators these square brackets Are implementing a test Okay, and to find out about the syntax the program is actually called test. So man test will explain The syntax the square brackets is just a short form of using the program test And if you scroll through you'll see all the the different Operators that you have available and or string comparison integer comparison File checks Is the file a particular type does it exist? Is it a directory and different things so you can do checks on files Very useful if you want to automate tasks Tasks on files you want to check if it exists before you do something with it Q to quit do we have one more example shell script? One quick example to finish shell scripts because we want to move on I'm going to copy. I know if you didn't get it working we'll come around and Even if you if you still have trouble with Examples one to four maybe put your hand up and one of the others will come around and help while I try and present Just so we can keep moving Um Or I'll come around after I go through example five Let's do example five last thing. I'm going to copy example one our very first one to example five doesn't matter which one but Example one was the shortest And edit Sorry Edit example five our last shell script example We can use input parameters What's the case? So anything that starts with a dollar sign refers to a very the value of a variable, but there are some special cases dollar one refers to the first input parameter to this command dollar two the second input parameter These are called positional parameters So ls minus l dollar one So when I run this script I must enter in the parameter on the command line Piper into grep dollar two ls minus l dollar one pipe grep dollar two save and run it and we need to pass in two parameters Dollar one will be replaced by the first parameter and dollar two by the second parameter And I just for this example example five Had inside the script dollar one and dollar two they will be replaced with the first and second parameter on the command line And just for this example it was ls minus l sum directory user slash bin Piper into grep and search for a string the string i'm searching for is vlc which means Find me all files in that directory which have the name vl or have the string vlc in their name And it shows you c vlc n vlc and a few others So this is just an example of positional parameters or input parameters to a script Which means you can create a script that Takes parameters as input And then reuse that script and it can be more generic than if it's specific to The files and and directories and strings That's our last shell script Last example Before we move on to some other things questions The things our shell scripts did were not very interesting It's up to you to put the commands inside to make it do what what do you want it to do? We just introduce the syntax Everything okay now Yeah, easy You need two parameters where we're going 50 minutes, okay