 Okay. Welcome back, day two, for those who didn't participate the previous day, so once again the overview of the setup. So you can ask your questions and you can be interactive on the notes. You should have received already email from Enrico with all the links over there. Then I will use my own send box. Here it's my own laptop and then on top of that there is another one screen which is just echoes what I'm typing over here so that you see the exact commands and the syntax. On top of that again there will be all the commands on my web page with the directory shell. So you can go there and see pretty much everything what happens in this demo space. So it's kind of exact copy of the demo space. So all this binary is ready to go over there and then the bash history. Bash history for today is 17th so basically you click over here and if you want to get the command out of here so you will get it out. Anyway, so let's get started. Short overview of yesterday's. No, not tomorrow's. Yes, of yesterday's. So we went through the basics how to create a hello kind of hello world script. So you know how to make those scripts. We went through the functions. We now know what's the variables and what's this magical functions with you put the variables inside the curly brackets and what you can do with them. So we also touched a little bit redirections in piping and we did quite a lot of exercises to go through all the subjects for today's targets. We will go through the conditionals and we will also go through the loops. So it's my plan but let's see how we'll go through and how intense if it will be. So if any question, please keep asking on the notes. There will be someone also replying to you online while I'm speaking. I'm in kind of speaking head over here but then there is a team which is behind this and doing the the the work on the notes as well. So conditionals as we told you already so scripting language is can be considered as a programming language and it has actually all the features of the programming language in this sense and one of them of course how you do the if else etc and how you go for the conditionals even if if else is presented and we will now touch it just in a few minutes. I have to say that the driven force of this checking and verifying something from the command line. It's this double square brackets expression. So important here you put the double square brackets on top on on the left on the right and then you have the expression and important you have the spaces in between. Otherwise if no space, a bash will not recognize the syntax and will be given as an error. So everything what happens within the expressions if it's executed and the exit code is true or success then from bash perspective it's zero and if it's something which is not one which is not zero it could be one two three or etc to two hundred fifty six it's considered to be fails. So bash and shell and if bash is speciality with respect to the conditionals is it has a number of operators that you can use for instance for easy checking file existence, directory existence, whether the variables is empty or not alike. So I have several selected examples over here but in order to proceed so I want to go to the demo right away so I go to my bin directory and let me demonstrate you let me show you for instance that was our previous file which was the archiving stuff. Now I want to go further and I want to actually create something which will be doing the more complicated stuff. So what I want to check I have the file already so if you want to check this out please do and the file is called no it's not that one the file is called tar it and here is a little bit more than one single line and let me explain through line by line what that means. So now you know that we are picking up here the argument which is given as the argument from the prompt when you execute the script. So I assign this argument to the variable dir and then I will start using if. So if and else is there and actually if and else very practical and very normal if you have been using some other languages. So the only difference over here that every single if block need to be closed with the fi that ends that's just the norm of the syntax but otherwise if you are using if you can use if and then you can use else. So the thing about this syntax over here is that this then it could go to the other line or it could just stay on the same line but then you need to have the delimiter because from the syntax from bar syntax perspective it's considered to be two different commands and if you omit this semicolon then you will get the syntax error. Okay, so let me use it right away in this noise saying this and again this expression in the double square brackets it will give you either zero or none zero output. So it's either succeeded or not. And so what happens after it's up to you. So what do you put this within this block of this if conditionals but condition over here we will come to this a bit later can be basically anything anything what returns an exit code or anything what gives you some kind of if it's even an empty command can give you something which is kind of care from the bar's perspective can be considered as a condition. But first of all let me go with the example of combination if else if else and then these operators that you can use within the double square brackets. So my first example if I'm trying that whether the directory which is given here exists it's essentially for me it's if and then the operator within this double square brackets minus D and the directory name and then I delimit it because it's a standalone comment and then comes another comment you can easily put it type like this. It's just a matter of your choice. I prefer more compact way so I put them on the same line and might be it's also more readable. So what I'm doing next my final target is to actually make the tar as far as your member and so what I need for the tar I first I need the archive name we already went through this tomorrow so I'm using here in number of common substitutions so I'm doing the base name I'm checking I'm getting out of the deer the name of the directory itself and then I'm getting also the date so my kind of new name which I'm will be exploding over here is unique or less next what I'm checking okay if this given to me directory does not exist so it's not actually checking the whether the director itself exist or not it's only checking whether this variable is empty or not so if you go back to the back to the online material so this minus Z and then something usually it comes the variable doesn't really check whether the variable is defined or not it's only checking whether this zero or it has some kind of length so let me show I will show you just in a second actually when we come back to this examples but here you see what's happening so if this argument is not given at all from the bar's perspective perspective if nor argument is giving this dollar one is does not exist it's not defined and there will be just simply empty and emptiness from the bash logic that also means that directory that this variable does not exist and so here I'm checking if it's that does not exist then what I'm doing then I'm saying that actually dear supposed to be my current directory so dot from the bash expression that means that it's the that stands for the current directory and then I'm already forming the base name the archive name based on the my pwd command so pwd it's the one which gives you the your current working directory and so I'm taking the name of my current working directory and then I'm taking the date again and here is the just extension for the for the archive name finally my final conditional logic would say that if no one of this true so directory is either argument is either given by directory does not exist or it has never been given then what will happen it will happen that actually I should say that this directory does not exist or it's simply empty actually it's not yet correct we will come to this just in a second and then I'm introducing you another one comment which you can use within the script it's called exit what will happen if you run exit within the script is that you just leave the script whatever comes after exit doesn't matter from the bash perspective so you will exit the code and nothing happens after so that's enough for you it's a kind of just quite in Python or I don't know it's when you just don't want to be I want to just close the execution of your of your script and another one important thing is that you put the exit code exit code as I told you it could be zero if you want to say that the exit code is successful so it's important for from the point of view that if you want to be if your programming part also want to be sophisticated and you want to be able to use your script for the for instance for something else and then you want to be able that you want to make sure that actually script fails then the if it piped to something else then the other commands would know that okay this has failed and then actually could react accordingly so here you are providing the exit code exit code can be as I told you zero then that would mean it's succeeded but then in our case actually I wouldn't consider that this has proceeded so that I provide the exit code one it's just normal that means that actually something went wrong and it has failed you can provide 206 if you want to and usually people would do is that they have a number of exit codes somewhere documented if it's complicated and really huge scripts then for every single code there is a description that what has happened and what how this can be interpreted from the plugin point of view and then when I'm done with my if condition then I just close the whole the the whole statement again the indentation is not important over here I do it simply because I want to be code readable but this is not an error at all from the bash syntax point of view so it's just to for a sake of the work so to say for clarity in this case and then finally if I came to this back to down to this line so that means that something has happened already so and I'm expecting by coming to this line that at least archive is defined and there is defined and what will happen is that my directory will have the name which is will be the this variable archive and the directory that will be compressed and archived and that's gonna be the one so that's the logic and now let's try it now let's try it so I am in the bin directory that IT has the X bit so in my case I can easily run it as is you remember I've told you already previously is that because we have been in the path yeah this the last one this demo space been so in this situation I don't need to put anything I can't run the script just like that it's executable it's part of the path and bash will find it for me and run it so let me see what I will get over here so now I should have get something which is called bin and this is exactly the directory of mine I mean the archive the directory of this my current one okay I don't want this but say I want to try it out in my and in the root of my workspace and then if I want to run something like our IT and provide the directory name so let it be that another one story what will happen actually it happened exactly what's supposed to happen so I've got the archive of that story okay so it seems that it works I don't need of course the archive of that directory so I just remove it when up after myself but here is what we've got already so my next step would be going through the back to the actual actual square brackets and let me do it on the online so you see like that it's pretty much working syntax so if I even have this like that so that's gonna be syntax error but then if I I don't know if I'm putting here checking var yeah whatever so it's executed and something has happened in the background what has happened let's try to realize so we can always try to see what was the exit code of the last command and in our case this double break double square brackets it's actually in kind of a command execution the only thing that this execution it comes to the down to the checking the whether this condition is true or fails and so let's see what has happened var is it defined or not let us check echo var there is some value over there but let's start it from the very beginning so I am un-setting this one so no value behind this so var does not exist and so let's see what's the z var will tell us in this case so it's 0 so it does not exist is exactly what that minus set is expecting so it's true if the length of the string is 0 so non-existing non-defined variable and empty variable are the same from the from the from the bash perspective so I'm assigning for instance to kind of proof of concept so I'm assigning a variable itself but it's empty and it's still gonna be 0 and only after I am saying some value over here it's gonna be said that okay it has actually failed so this directory this variable has been defined and it has some value be careful because in other language if you would say that var is equal 0 from the bash point of view the length of this variable is non-zero it's at least one byte so that means that this minus z will give you fails and again it doesn't matter what you put over here it will be still considered the only the length of this variable so if you want to make sure that you are kind of follow the logic of this operators then just just take it this into account so you're working with the not with the definition of the variable but actually the content of the variable itself saying that let me go further and see that how complicated this kind of expression could be so you have a number of the things so I have demonstrated you already minus d in the same way if you are checking the file you can check minus f and say that this doesn't file exist or not taken out on this because they're going to be the they're going to be the the exercise right on this then if you want to check this it's it's readable for you I express here it's for you so it's not readable in general but it's readable by you so you can push this minus r and then what else so all these kind of comparisons including integers and and just normal lexic so you can use them within this double square brackets but regarding the integers let's say that we will come back to this in yet today so when we will be considering the arithmetic so I would definitely say that please use for the integers some other so you can see that within the loops subject there will be the arithmetic and there will be introduced the other expression so just the normal round brackets over here and so that is way more powerful and so coming back to the conditionals I would say that square brackets is mainly for their strings even though you can put the integers over there and even though from the historical point of view actually and for a sake of compatibility even those double square brackets they have implemented all this equal not equal less than less or equal greater than and greater or equal etc. but again I don't get confused so you can use them both but I will suggest that you actually from now on you would start distinguish them and so these ones for the strings and then the round ones for the integers again a few examples we can use the examples with the logical and and logical or and then we will use negation and we will at some point may use also the grouping so you can have a list of here of working examples it's up to you how you use them we use them I mean those who are writing the scripts they are using quite often and then to demonstrate you that actually something when may happen and be quite more complex so I am going back to the star IT and see that for instance if I would like to implement and say that check that directory it's not only existing but it's also not empty so what I'm saying here is that I need another one condition so if actually can be nested so let me show you one the way to do that so you can also do it like this if and then puts another condition and say that here I'm testing that it's not empty so how to do that you can use known to you common substitution and you can say here L LA so what LS LA gives you it gives you a list of all the files and directories including the hidden ones in the current directory and if it happens that this directory empty then LS minus L capital A will return you empty list which from there from the F and expression perspective that would mean that it's just the felsing so what I'm saying here that okay let's put it like that let's put it like that and let's see the logic so if it's if directory exist and if it's not empty then we already do the setup for the archive and then we have to close this internal if condition in the same way it's probably more readable but in the same way if you are already good enough in programming with bash you can actually join those two conditions into one and using the logical end you are saying pretty much the same thing but just within one line like that it will work as well it's your choice I mean if you want to make readable if you have some colleagues which are not really capable of reading this scripting syntax it's probably makes sense to do it as a nested but if you did it for yourself and you are experienced enough then that should go this way okay you can test it out if you want to or you can just trust me that this one will work it's the how to use the complication of this of this operator and then negation so negation would say that okay if you put the explanation mark let's be let's make it more noisy so minus n would give you that directory variable is defined its success but if I want to still negate it even a half minus z so I would put the exclamation mark over here this will work also at some point you may want to use it as explanation mark for instance will be I don't know in the same way if you put it before the condition then that means from the conditional expression point of view so give me the value of that which is negated so if it's succeeded if this minus d operator has succeeded then we put it that it has fails or okay I think you've got the idea so sometimes you need this so now we have this done and I think I think I should be ready now to go for the first exercise so my first exercise of this day there will be conditionals and I will ask you to do next so I'm actually asking you to work with it at it so if you think about it where to get it to just go over here and you click it and you just download it to your own directory for a sake of playing with that but anyway so here what I want to what I what I want to want you to try so first of all try to check whether any argument is given so even though it's a little bit pointless from this programming point of view from the view from this for this kind of script but it's not pointless from the starting point of view so you will learn how to check the number of the arguments I have it in the material or you can say that if you remember this variable it's a system wide variable is existing by default when you run any kind of program any script in bash and it gives you just exactly the number of the the number of the the number of the given arguments so basically if you say that it's if it's equal 0 yeah then you should already come out and decide what to happen and before doing that so I'm cleaning it up before doing that actually I wanted to demonstrate you something which was say that if you want to compact view of this test operator so what will happen over here I mean the what will happen over here that if this exists you can use you can use this logical and and logical or operator on the command line and because bash is mostly about command line mostly about prompt so on the prompt you will not probably using something like if etc even if you can if then and even if you can do doing all this kind of things over here the most popular way to use the if to use this conditional operators on the command line directly is to using them with the mid-wise operator so with the bull ends and in my case if I'm checking that if it exists echo okay let's see if it doesn't I'm using negation it does not exist so now you know now you can use also this kind of technique if you want to make it compact but within the script I would say if is correct way to do that so it's more it gives more readability but when you do it from the command line you can simply do it and simplify it and simply do it like this so that's another one hint for the conditionals so let's say and then the last one that I'm expecting you to check that this archive which we're trying to make it does exist okay let's go and say that yes 45 it's something that we will come back to this and I will show you how to how to actually this can could be implemented okay several replies already there so let me let me demonstrate what actually you were supposed to be doing so I copy my car ID to the RID let it be version one so I'm not using git here otherwise that I would just make the comment that would keep adding but here just for the sake of clarity so that they can have both versions and open up all of them so I'm now let's me I'll make out of this original tar ID something which was requested to do so this dummy check for the command line argument so what I'm saying here is that I'm assigning the variable to the deer and now what I need so I need to make a check that this directory is not is given to me so it's once again minus z deer over here and what happens then if it's not correct so then we are archiving or let it say that we are putting minus n and so we're checking if it's actually exist does it actually exist is it existing so yes I can say that the victory exists archiving it okay in the other situation if this guy is not there then what I'm saying that are carving the current directory and then there was also request to put the name of that directory so the easiest would be to put the PDD and PWD so my current directory or if we want to have just the name in case we can do the thing with the base name like this that should work for the how do we call it arguments check arguments check it then the other one was to check the whether the file does exist so what will happen over here my point was that I don't really want to override already existing archive and so I need some how to check this I need some how to check the the archive itself and luckily we have this archive as a variable so there is nothing else to do than just putting this minus F and see whether this exists or not and so we can do it this in exactly the same way the only operator will be here minus F which says that check please that this file exists and now one hint for you so I always suggest that you put actually this variables which you put into the test conditional test operator so put them quoted why sometimes those are variables may have some special characters even the space is considered to be special characters and so if you all meet this kind of double quotes then what will happen that archive will put on this line for instance a stream which would have which will have a space and then it will be considered as a kind of additional variable or additional check or whatever and most probably I will just tell you about the syntax error so this is why just get started with the with their good practices and so use the archive use use the double quotes within this test construct so and then what I'm doing here that if archive if it exists I'm saying echo archive already exists okay and then important thing before I actually close in the whole block I need to exit because I don't want the whole script go down to that line so I'm saying here exit and I can put some exit parameter like to and now if you run this within some script and you are getting this kind of errors you already can get around with the exit court and say that okay this exit 2 is about this existence of the archive and so that we don't overwrite this so let's see if it works we need to check what we need to check our it shell so let's see first the archive of the current directory demo space okay that's correct demo space is there so we assume that at least this part is working and now what we need to check we need to check whether the directory is existing or not so basically if we are trying to oh sorry now I did the wrong thing so let it take see let it create it and now I'm trying to do the same thing and trying to overwrite exactly the same demo space dot blah blah blah so now let's see what will happen now it's actually has checked that this directory is already there this file this archive is already there and nothing has happened so it has not been overwritten it's exactly what we were up for and so let me clean up after myself once again and so that one is there I will also add the right answers to the to this note guys so that after what should be able to to come back to them okay they are there okay so on a show we should be good to go further now we are back to the back to the conditionals but now I want to tell you about something special which is called the matching operator so you have here one text so but basically matching operator this is the functionality which will work only within the only within the double square brackets so there are some historical notations also for the test operators like single square brackets so but this will work only with this modern one way and what it does it's supposed to it can it allow you to use the basically the in the most basic concept you will find whether the part of the stream that you are looking for it's a kind of such word is actually part of the stream which you are trying to match against and in the worst case you can go with the regular expressions let's see how to how what's a what what you can do so there are a number of selected operators over here we call it operators but actually from the regular expressions point of view it's the it's a mini language I mean if you ever met this if you ever try this with Python for instance or if you were tried it with a grip so every single setup it has somewhat special syntax but most of the things more or less are the same so the most of these operators most of these characters or most of the special characters that you use inside the regular expressions more or less the same I will not go deeply into this so it's a subject for the for the book it's a subject different for the different lecture my only point here that matching operators is existing and my point here that I will show you how you can use it within the bash with the several examples my first example would be for instance let's see that I want to verify that the email which is given to me by somebody okay so I go to my stream email is given to me by somebody is actually an email so now I have a variable and now let me check it so what will happen I first I need to create some kind of regular expression okay so let me put first these guys round brackets down so that should be perfectly enough if there is something before that had and then there is something after hat so this is the very simplest way to check whether this given one is the actual example so let's say that I am satisfied with these kind of verification so I'm not checking the domain names I'm not checking that the user existence etc that's already too complicated it's not for the for the script but then let's see what will happen whether this will be executed correctly or not so in my case yes it succeeded so the email was correct but then if I'm trying to something which is not looking which is not looking like email then let me try it once again now it's not succeeded so that's the way to use this kind of regular expressions in the same way the easiest way if for instance I still want to have this email in the previous format and I just want to check whether this variable has for instance auto inside for me would be enough if I just put the auto name so my stream which I'm matching against will be compared with this auto and this operator will try to find me every single entries of auto in the email so now it says that okay correct it has been found and so you've got the idea how to use this matching operator and let me show you one thing for instance you remember why we're playing with the app and with the how to make the path how to make the additions to the path variable so let me go to the bin functions and I will add another one function over here which we can use later we can make it more generic and we can later make it to work with any variable but at this point let it be path so my point is okay I can expand the path variable as many times as I want to but what I don't want to happen if that for instance I want to add yet another path to the bin directory but if it already exists there then we should skip it somehow so my point if I wouldn't be using the matching operator I wouldn't be using for instance something like echo grep okay that would work let me check it out so for instance if I know that my path is demo space bin yeah something like demo space bin or I can't put it even in such a sophisticated way so I know it's home demo space being this is what I have added on the very first day okay and now what I'm doing is that I want to check that this demo space bin is part of my variable without the matching operator I would use grep so what I would do I would echo echo original path and I would say that please grep for me that one that I have designed just recently okay and grep will tell me exactly this guy is there this guy exists and you can see that actually we can even say that even if we get rid of the output we can still get this output the exit code so the exit code in our case was succeeded okay nice but then how to do the same thing with the matching operator so let me do it the way we do it with the functions so that would be as simple as that so I need only the path itself then I need that matching operator over here and then I need the actual the string which I am comparing against that's it and the function will pretty much work only with the with bash functionality so we don't need any external program like grep or not even echo so we just do the like this so that's one of the demo how to use it already right away and that's also the way to show you that actually there are some there are different approaches how to solve it and you can find them also when you google differently for the all kind of bash solutions so you will find these different approaches just just be aware that all of them work and maybe the question of compatibility it's but also the question if if you want to implement everything with a bash or if you want to use these utilities which are around and then we come back to one thing to my first example email let me do it in such a way that I actually email is the one which is supposed to be correct but let me check my regular expression which I have used for the email and remember very beginning there were the round brackets in the regular expression round brackets would mean grouping so what that mean from the from perspective when you start using the matching operator matching operator will have such a rare which is called bash rematch so we don't know yet anything about arrays they are coming only the on this study the number three so it's going to be tomorrow but let me say that they are in the bash and so here is the first the first yeah the first touch of this one so what will happen over here when we assign this to email and regular expressions and then if we go and check it like this so what this matching operator will do for us it will find this one and this one and actually it will be able to distinguish them and it put them to the to the array and so we will have number of elements in this array elements zero of this bash rematch it's gonna be the whole sentence and then element one is gonna be the content of the first group and the element two is gonna be the content of the second group and vice versa I mean it will be as many elements as many groups you have over here let me show you what I mean so what I actually exactly want to hear to do here is that I want to output the bash so the easiest way to output the array is using this syntax so by now don't go deeply into this just copy paste it when you will be playing with that so let's see what will happen and now you will see that actually this has been done and those variables have been assigned and now you see that I've got three different fields over here and I can use for instance if I want to use the only the first one so I can say that okay give me not everything but only for instance the name of the guy so that's gonna be and then if I want only domain that's gonna be like that it's fantastic that it works actually so you definitely wouldn't expect this kind of from the bash functionality and if you're familiar with the regular expressions so that's the that's the basically that means that you're good very that you've got very kind of efficient tool in your hands I wanted to mention also one thing with them if and else what this thing is is that reminds it to you when we are talking about condition which comes over here when we are talking about condition which comes over here this condition can be basically anything what can bring you true or fails in this the same way like I'm using for instance test operator over here in the exact same way I can use I can use pink comment or any other command and if result of that command will bring me something then will be do if it succeeded right after this if it's not succeeded then it will be an Elsa so take away message that basically any condition anything which will bring you as a condition we will also we will use it later on for the for the for instance for the for the RF medic operators so we will also use if an RF medic operators and you will see that actually yes if can accept anything what you want so but here I think it's now time for the exercise session and now we will take this conditional operator and play with that now what I want from your side is that you will spend your next 15 minutes building a regular expression it's not easy not at all please use the slides over here and check them check the examples and check the examples that we have used also from the online and then try it out and even if it looks like it's very simple it may actually start bringing you the errors and it's maybe even painful because actually playing with a regular expression it has never been easy it's the it's the thingy itself so it's a kind of media language and understanding how that works is the thing to go okay so now we will have joint break and exercise so remember to stretch your legs or stretch something whatever you want to and then we go back to the earth 1332 so let's say that 15 minutes plus 10 minutes break 1332 and then the exercises coming over here otherwise you can start doing already this one so I'm talking about the star it that we keep developing with you okay now the floor is yours I am muting myself and if any question please don't hesitate to shoot them hopefully the break is over if you can hear me then just let me know that it can hear me somehow so shut me on the notes or somewhere so let's get back to this validation so now what was the problem the problem was that we had to make some kind of regular expression which would take which would make the validation of the directory path so I know that for the for the bush scripting it's usually not that this validation is not really that often having some people they really don't really care about this but as a former for instance I've been developing for the web quite a lot in some recent times so you can see that actually there are everything what comes out from the from the outside somehow it could be the argument it could be the given the input on the interactively I would suggest that you definitely do check because you never know which who will use the script where and how it will be used so whenever something comes so the hand especially if you want to remove something if you want to rename something if you want to move something then definitely make sense to check that is it actually the valid the valid path or the valid name or is it the valid thing at all so what we are doing here is one we need to create a regular expression so that wasn't easy at all so I must say that definitely if you have never played with the regular expression then having this layer something like this would definitely go with would take some time over here so my example over here ready to go that I have just put to the other so my regular expression is looking like this one and let me explain what's happening over here within the regular expression I'm saying that within this brackets that what comes that should be either a digit a letter a capital letter it could be a dot it could be on the score it could be slash so that's the kind of and these square brackets in this case they will just indicate that that's the any combination of these characters and then on top of that I'm saying that it should be plus so it should be at least one or many and then I'm limiting it so I'm saying that hat at very beginning that means that it should start with this and the dollar sign at very end that means that it should end with this so there should be nothing else except those ones okay that should be good enough already for checking out that that's the valid path and then how implement this so this what you do usually if you do the implementation something like this so we can of course increment it into some of some of this checking right here but then we can do a separate also so for instance let me do it actually let me just increment it into the incorporated into the existing one so here I'm checking that directory existing but actually before the thing I have to check that directory is correct so this can be taken off and then off then so here is my consequence of checking list so I'm checking that directory given to me is actually valid directory path so second I'm checking that the directory itself exists and third one I'm checking that directory is not empty so that should go like this and the regular expression over there said that was the that was the whole thing so let me let me copy paste it to you over here and so next time you know how to use it where am I on the arithmetic so the next will be for us the while I'm doing this I'm speaking also the next will be for us the arithmetic strictly speaking bash is not really going to be really proud of arithmetic so when you are talking with the arithmetic so like this yeah so let it be there for a sake of documentation so when we are speaking about arithmetic in general we are speaking only about integers so these are good for all kind of indexing it's good for making all kind of counters but nothing else so if you are looking something to work with a thought point then you definitely should go so for some specific math applications or at least look for the libraries for the Python by scooping and other numpy and others so but with respect to this plus one plus one one plus two or one hundred multiplied by something so that should be good enough to do at least this one if you want to count the line numbers if you want to count the chapter numbers if you want to make the indexing within the array so this bash is capable of doing and there is a special expression over here these are round brackets round brackets like this and everything what happens within the round brackets it's gonna be interpreted as the arithmetic expression so when I saying that n plus m bash would expect that there is a variable called n and there is the variable called m and I want to see the sum of those two variables okay and as comparing to the for instance this square brackets double there is no need for the space over here and also as comparing to the square brackets there is no need for the dollar sign you can put it it's not a syntax error but it's not a syntax error either if you omit them so just remember everything what is later over here it will be interpreted as a viable and then what will happen so let's say that I want to assign n equal to an m equal I don't know five if I do it like this you will see nothing why yes the arithmetic action actually has been done it has been counted but it has not been saved it to any variable or it has not been printed to the output so if you want to print the output of this action so you have to put the dollar sign and then do something like for instance echo and this way you get the output or if you want to assign another one or a variable like I don't know variable p or variable some summation so that's gonna be also like this then your echo s and you will see a call not ago it's gonna be like that and here is a number of selected operators what you can do so take a look at them and we will start using them right away and one of the particular example so you can do the incrementation plus minus exponent and then multiplication truncating and so if you're dividing for instance two numbers yeah so you will get the integer still so if you even think that okay and m is 5 and n is 2 then I can divide them so I can still see that actually there's gonna be the integer so they will be rounded to that or another direction so but you can also get the reminder over here this is that much about logic about this arithmetic since I did bash so I don't really think we still we need to tell the way more because what we need to what we need to do is that we need to be ready to go for the upcoming loops but before that I would like to use this section to introduce you something which is called a read command so let me do it in this way like I have a prepared comment in the bin which is called read SH let me try it out with you so what I'm doing here I'm saying that please read for me something which comes from the keyboard from you essentially into the variable E I and C1 and I'm also saying that my prompt before this I and C1 should be this one okay and then I'm just using that variable to echo this this is that simple but that's demonstration how to use it so it has nothing yet to do with the integer but but you will see shortly that it is useful because we will start doing now the exercises soon so what will happen over here give me an integer please so I give something like give me another integer okay still integer oh no so too early so you see something like that in a sense read is nothing specific I mean if you go to the manual page and see the rate it has very few options so it has a time limit for instance you can say that there's something like minus minus r in order to avoid some special characters you can put the minus t and say that that the time limit for the waiting would be 10 seconds or let it be even four and then if you can even say that you can hide everything what's been type if you are kind of curious and you want to request for some secrets so let's see what will happen if I do it like this give me an integer please so if I type there will be nothing and actually the timeout has been now has been now gone and so nothing has happened so read again so that was your integer is like that fine and then if I wait for four seconds it will just break me out and I'm done so we're pretty much now ready I think to go for the actually next example I want to make one demo for you I have a demo called Gaussian but there is a story behind that so the Gaussian you know this famous mathematician and he was a genius and so at some point in his classroom when he was kind of quick enough to answer all the questions the teacher was giving so the teacher told him that please count me all the numbers and summation of all the to give me some of all the numbers from one to 100 and the teacher was quite surprised that's when the kind of 10 years old boy was in couple of seconds saying the right the right answer so the problem over here is that the Gaussian actually find out how to make the summation and his method was that he was taking the 1 and 99 2 and 98 3 and 97 etc. and he have realized that actually there are 50 pairs that will give you 100 and then there is 50 which is just in the middle and so you easily can count that actually or something 1 to 100 will give you 5050 so but it's that's just the history but then actually we will use it for the sake of trying the arithmetic within the bash so what I'm doing here I'm starting to use the arithmetic right away in this script so first I'm checking that actually this variable which stands for the number of arguments so this is the special variable and I think we were talking about this already earlier and here I was mentioned this already so that's a number of selected variables and one of them is special one when you give this number of input parameters okay and so I will I'm saying here if this is exactly one then we take the n from the command line and if it's not the case if no arguments is given or if more than one argument is given or if not equal to one or whatever so what I'm saying here that please read this number from the from the prompt and let's make it interactive and then when it's done we already do the actual summation and you can see that actual summation happens within one line I'm printing and counting it just like that so in my case using this Gaussian approach I just using this formula so n and plus 1 divided by 2 and you can see that I can even use within this double round brackets the round brackets themselves so this is made to put the priorities to grouping so that this actually arithmetic action arithmetic will happen first and then after that will happen the rest so there will be multiplication and my end-division this is to tell you to show you that actually something still works and so let me try the Gauss you can try it on your own as well if you just copied from the webpage so give a positive number and let it be 100 and so so you are getting actual correct answers in that Gaussian boy was pretty genius yes at that time at his age in the same way that if I put the first to the line so let it be 101 whatever so you can see that actually I'm still getting the correct answer so now what I was about to demonstrate you along with the read command along with this check for the input parameters for the number of two input parameters I'm also actually demonstrated you quite nice techniques so you can both check the input parameters and if nothing has happened over there then you go and ask interactively okay so that's the demo nicely looking hopefully and now the one thing which I was thinking to tell you actually I was just made it tomorrow because I was developing one script and then realized that okay I never told the course participants how to check the syntax there is this minus and with bash so if you want just to check the syntax but to not execute the code so please make a note for yourself so use the bash minus n so let me for instance break it somehow yeah it's our own syntax and see what will happen with the minus so you will get the error without actually executing the code which is just nice and you even get the line number where the code has been where code is failing so you can already see what's exactly is happening over here on the line seven okay so just to make a note for the yourself bash minus n that will probably save your time somehow somewhere when you're doing these things already for the production now it's next session interactive session so let's try to look at the RF medics I don't think we really need that much time over here but I still give you 15 minutes or let it be 10 actually because so the only thing that's again if you are quick enough with the read commands and you should be probably quick enough because there you just use this read and do what's what it's supposed to do then if you are quick enough kind of advanced task for your for you would be check that given input is an integer so it's the continuation of my previous exercise so if you want to play with the regular expression then you can find out the way how to check that actually input which should get from the prompt is what you're expecting okay now the time is yours and now let's say that we give it 10 minutes and then we continue at 14.01 I guess yeah yes and we probably need a second break so let's continue then at 14.10 or let it be 10 or I'm muting myself but yes you know where to ask the questions if you want if you have any coming back online so I think the simplest would be just if I will show you how I have done it I will just copy paste it directly to the notes and they will be there so the script to implement this kind of arithmetic is pretty much straightforward I mean from the programming point of view and the point was here just to find out how to use the right syntax for the for the bar specifically so what I'm doing here I am go through the asking for the read ones so if I am asking with the read commands the two variables so one is integer one another is an integer two so you can name it the way you want to it's just the for a sake of convenience and then I do the comparison of using the if else if and else statements and inside of this I'm using the the the arithmetic expressions so here basically logic is pretty simple the more kind of advanced it was to check that the number is actually a number or the given input is actually a number so what I've done here is that I have prepared the the regular expression and was comparing that is what I receive from the user something which is contains all the digits or it could be also the negative one just to make sure and be more complete on this one and so I'm using this one condition so this would go somewhere right after the read and if it's okay then I will just continue to the rest of the code or if it's not okay I would just make the exit in the previous examples that was already there so these are two to these last ones now let's get to the one another one real part I mean taking if I count like conditionals like one is an essential part of any language then the loops is another essential part of any language and bash has several implementations of a loop it also has this while until loop and again remember that for a loop in bash is something specific it's not what you expect in the C style even if the C style is also there but since the bash has been initially a kind of scripting thing for the admin administrators and for the advanced users so what it can do the best it can do it can work with the files and the directories and you will see the example why this way and then it can also work directly with the with the input parameters so what I think I will just jump directly to the four examples so my first example will be go through the every single parameter on the line so if I tell you that for instance if I'm creating some file any file or example yeah I'm starting it pretty much with the same way like you would start any other any other bash file and then says for instance I want to go through the loop if I say for some index variable in the list and here I'm having a list list could be for instance item one item two etc and then I'm saying let's do that and then when it's to when it's done then just done for a sake of compactness I'm usually putting the do as well here it's in the same way like with the if statement side puts the then over there and here you do with the viable I so basically it becomes item one item two etc one by one but in case of bash you can simply omit this list one what will happen over here you remember we're telling you that this dollar sign and the number sign is the number of the arguments given to the from the command line and then those arguments are like one two three etc and there is another one variable which is called dollar sign and where do I have had it's here so which actually a list of all of those variables at once so these are the only three or these are the only differential variables which is supposed to know when you're working with the input parameters and what will happen actually here in this construct that this for loop in bash by default will simply go through all these for the variable this one so it simply go through one or two or three etc and so every single item over here will be like this so we have ready to go script let's try it so I say that we can even make it more for the sake of clarity so let's say that this no we can't really say anything else so let it be like that so what's happening here so I have so once again change mod plus x and I'm not in the bin so I have to put the essential explicitly path and let's see what will happen if no arguments then no output but if there is some argument two three one two three for instance you see I will get the arguments back a b c d I will get the arguments back in the same way if it's a b b b b d f g c r t y d y blah blah blah so I will get these arguments back to my line so that's already something so now you've got the proof that actually by default for will go through all these items and will do something which you think is reasonable so I have I think I have one example or SH so what am I doing here I'm pretending I'm somewhere in the directory where there are lots of shell files and so my next step will be actually to work with those files you saw in the previous step that if I omit completely these parts in and list then it's gonna be the input arguments given to the file given to the script which is running currently but another case if you give the argument if you give no the argument or actually even if you give the argument but then if you put the list explicitly then what will happen over here bash will consider that these are the file names so if you want to expand for instance and take this with a star and wildcat and see that actually all the names all the all the files was extension dot SH show will be listed over here so if they are not found you will get their message that nothing has been found but if they are found there will be proceeded one by one so what will happen next it will pick up the first file with the extension SH and will go for it so this small script does nothing except that it's just the grip out the empty lines like that and then it what will does further so it will just grip also the she bugs so basically what I'm trying to grab out of this SH files it's only the comments so let's see what will happen if I go to bean well if I will try it from here I will get their message and that was interesting so if I go to the bean yeah so actually I had one SH file so this is why I didn't get the error message so that was the just the recently created example but if I go to the bean and if I will run exactly the same file so you can see that actually my list of shelf files is already about the dozen of them so you can see that I've got the all the comments except the she bugs and then the something which has a real comments but it all this stuff which comes from there just one by one so that was a proof for you that yes by default bash will recognize this kind of list as a list of files it doesn't mean that their or loop will end like key like here like this like here so you can still work with the items and put the list to this for loop in any way so anything what can produce a list can be used with a with this notation so quite often you can use for instance find comment so you will find the files or directories and then you will do with them something quite often you can also use something like brace expansions I never mentioned this but there is a bracing expansion so for instance if I want a list from one to ten yeah what will happen to we here so bash will automatically generate me a list from one to ten if I want to list from a to set them not like this so bash will automatically generate the list of that so if I want to make something with this list for instance for items in the list and then do you can see that actually I can type also the four from the command line and it's just normal techniques and people do this do and then I will do something nothing specific so I still just echo them and I can say that even for instance the item is and then I put down so just remember if you put everything in one line just remember this command delimiters so you need to semi column at the end of every column at every but at the end of every at the end of every command so here you see that I have listed every single element separately if you want to do something else with those elements just do it just it's up to you so in the same way this list can be a a d d r d wire and blah blah blah so everything what you want to be on the list so it happened to be on the list it's gonna be on the list and you can proceed with that so that's the easiest part of this follow then as I promised you so there was another one approach to the for loops and this is called the C style approach C style is known to you so I'm pretty sure that if you've been working with the other languages so do you know what this stands for so this one is not that often actually used in bash it's there and actually I pretty much sure that even the advanced users maybe have never used that or have never heard about this C style loop and bash but I mean if you at some point need this then just feel free to use it the usage is pretty simple so there is the starting point first of all it's known to you arithmetic expression round round brackets then you start with the one when you put the condition when one till one is less or equal and and you have to define the end or you have to say it explicitly over here and then you do the normal incrementation and actually this is what will happen if you do the normal math and the C style loop has appeared also as part of bash when this double round brackets notation has appeared in the bash so we I don't think I want to tell you anymore on this I would rather prefer that you go to do the to do the exercises yeah one note so for just a normal command in a sense this one this construct is nothing else than just a command yeah and so I want to do everything what I want if I want to redirect the output of for loop I do it like that so if I want to redirect to file file for loop did I collect that it's gonna be there so now I can take a look like output of my follow-up has been redirected over there in the same way if I want to send it so as a pipe to another command then we can do as well this way in the same way you can work with the what I mean if you want to put back something to the loop so that's gonna be like that so pretty much normal command operators that can be used for any other command can be also applied to the for loops and then the other loops but now let's get back to the for loop and now let me print for you the next exercise so what I'm expecting from you right now is that you do a number of dummy files a number of dummy files and then actually rename them name them so the issue here is more or less copy pasted from one of the example in the material so you can find it out some way here I guess yes so we I didn't I've done this for the conversion and actually there is the move as well so what I want you to start running is that I want to create those files and then implement the script that will go through all these files one by one and make every naming so the original name should remain the same what only needs to be done that this extension which is originally in the capitals should become the small ones that should be quite simple so but still I don't think we need actually even 15 minutes just copy paste the example and play with that I think even the 10 minutes should be pretty much good stuff for that and then we will be well on time so let's say that we are back so the stuff 1438 yeah coming back over to the so the what we've been doing I guess it was rather quite simple or should be I expected that should be already quite simple for you so let me put the right answer and a little bit of explanation so what has happened here you see that I'm from from the command line I'm just running touch so I'm creating the number of files so from the bash perspective this brace condition brace expression will just give me files from one to five try it on the all and then the bash script itself so what I'm saying here that go through all the files with the extension with the capital stxt assign it to the f and do it one at a time so we move move the verify every single file name and here we are using already the bash syntax of the variables where I can easily remove one extension with the another one done you can use it anytime anywhere from command line or making scripts etc very useful when we are working for instance with the gpeg files and very useful when you're working with the well especially with the graphics I found that when I'm using this display or convert commands when I can just go through all the all the directories all of those files and do all of them at once easily okay so my next stop would be the while loop it's interesting I mean it's while it's unpill and the same thing that it's while the condition is returning zero status so why it's succeeded then we use while and this loop will continue and until is the same way but when the condition returns non-zero status so basically when it fails and one of the example and one of the example for instance let me try the gauss but I say real wire in a sense that I'm just making for you pretty much the same situation like we did with the original gauss simulation but except we don't really use the numbers we don't really use the formula by gauss but we are using the straightforward direct summation so what I'm doing here is that I'm expecting the n variable from the command line so I'm saying that my original one will be one so beginning and so I am going through the loop with the arithmetic so till my variable my count I mean is less than the this one number given to me from the prompt do the summation so here this formula says that s is actually well we can assign s equal zero and so we can say that from very beginning we just start the summation every single iteration of this loop what I'm doing is that I'm adding this i to the s and then I do the incrementation and this way I'm getting from n from one to n so that should be very simple the only thing that I have done something you here you can see that actually assigning the variables can be done within one line and even with no tillimeters from the syntax point of view it's completely fine so let's see what's going on change mode plus x it's in the bin so it's there and I want to see the 100 okay I want to see something like one million and now let's see that how much time it takes in Linux there is this utility called time which can tell you what's the execution time of the command so you can see that if we are go down to the million or something it's already 10 millions it will really quite take a lot of time I mean well I can't really wait for that so let it be just one million I'm saying you hear that actually different approaches and let me run exactly the same but you remember where we were using the formula by Gaussian and it's way faster what's the so what's the takeaway message from here that actually the algorithm that you are using really matters so the exactly the answer is exactly the same it's correct but then the execution time is way more different so one other thing that can be done with the while and this is where the while is used quite often here you see it's when you're working with the files so what usually is being done you send the file to the standard input of the while loop and then it comes to read common in action and with the read you can do lots of things I mean you can read it from the prompt as one integer as one single variable but you can also do the read the whole line or even make the line division so read is quite powerful and when you start working with the files and when you start implementing some kind of real programming stuff for the while you will realize it pretty much so what else I'm introducing here in this script it's the IFS IFS is nothing more than just the default telemeter it's installed it's used everywhere so by default it's just the backslash n so which means let me see can I even see it somehow yeah that means that in my case it's just the new line but you can redefine it and redefinition of this one will give you actually lots of abilities so let me see that I have prepared the file for you which is quite real these are the students okay so let me see what's there that's the list of students I picked up just .fi and so that's the list of students from 2018 per university so you see the number of universities and number of students male and female students so if I want to for instance do something with every single line but not only with every single line I can I want also to do something with every single field separately so what I do in this case I know that separator over here semicolon it's the standard syntax of CSV files in your case that could be something else but here I'm using this just an example so I'm redefining this variable and say here that actually my telemeter is gonna be the semicolon and what happens over here if I don't skip this backslash then syntax would be broken and the interval will be interpreted by bash that the end of the line and so that's gonna be the error so this is why I have to use this backslash and this works as a quote in the same way you can put of course the quotes over there but most in most examples you will see this backslash and then the quote so I'm just backslashing but actually the character it comes to the IFS IFS would be this semicolon and now it comes the turn of the read command and the read command what it does saying that this one is telemeter it grabs every single line and divide them one by one according to this telemeter and so we say here that this f1 it's gonna be the field one so in this case it's gonna be this one actually this one including the quotes so the whole field the f2 is going to be this one then f3 is going to be this one f4 is going to be because no f5 here f4 is going to be the rest of this file so whatever comes after f3 will be the f4 so that means that you have to know the format of your file and if you want to work with the format of your file then be specific and be precise actually check well in advance that all these fields are marked in the order and all these fields are actually the way you want and then after all you can use those variables separately one by one and you can skip some of them you can use some of them for the sake of summation etc so here i'm just printing them out and so one more touch here so please welcome the printf command so the printf command is nothing else than the c-style printf so if you know how to program in the it's in python in the same way except that in python you have to put it in the in the brackets but the c-style is working like this so you are defining the format of the output variable and then you define another one format i mean some text then format of that variable another text and format of that variable etc and then basically you just make the format it's written the way exactly you want it's really powerful i would say that most of the people all the time are using this echo which is completed for okay and it does the job for you have probably nine times of ten but sometimes you want to make it real nicely format it output so i go to the bin for and i was thinking that i still have some while example to show you so this while was actually from here except that i have added this f5 and so let's see what will happen so i'm expecting the file from the command line this says you this dollar one and then that will be submitted over here and given to the standard inputs so let's see that this students students file is here so what i'm saying that please use while shell in my case and then shell then this one students and let's see what has happened so you see that i have actually got the fields and but also there came some stuff which was originally in the file but i don't really wanted this because these are the these are the just normal comment for the csv file and these are the empty line and these are the heater and so there are several ways to get rid of them and now you will have 10 minutes to actually try to do that well i will stay here longer but we have official 10 minutes of the left of the exercise and so my exercise for you the last one for this day will be pick up my file wild dot sh and use it so that to count the total number of students so the total number of students if you can see that's the field number three so basically what i want you i want you to two steps get rid of this one empty line and the heater somehow the way you want i have introduced several approaches over here on the line and then grab this one and make the summation so now basically you already have the gauss you have the while sh so you know more or less where to grab this piece of programming and get them together and so let's say that the next one is for the 10 maybe 15 minutes but i will stay here for a bit longer so if you are still curious and still want to continue so i'll be here to give the answer otherwise i will just put the output the right answer just in 15 minutes online and so you will see the result okay the floor is yours and let's say we are running a little bit late but we will be but let's say 07 that's the end of this exercise and basically the end of the whole day and then we will continue tomorrow at the same time okay the final remark to anyone who has yet stand so if any question please ask but here i'm going to put the answers for the last exercise and otherwise i'll see you hopefully tomorrow so if you need an explanation here you go what i've done i've used this while script that i have already explained the only thing that i've done that i have got rid of the thing which i didn't like so basically i've checked that if the field one is not looking like this so i was expecting that there are on the numbers and only the quotes double quote what what so it could be there then everything else i was just making summation actually this is not correct so that should be the field number three okay not five and then when it's done i have just printed out i have also introduced here the another approach using the process substitution but actually just remember that i have never told you about this so you can just keep it or i keep it over here for those who are you as advanced but actually it's yet another way to do the the same thing okay otherwise thank you for the participant today if you have any feedback please leave it back where and then otherwise tomorrow at twelve o'clock we will continue with the how to work with the inputs how to work with the uh traps then i probably will have chance to tell you about the arrays and how to put the here documents but anyways see you tomorrow