 So, my first touch will be actually right in the script, so that's what we are here all together. So my first script will be, let it be the Hello Bash, but before we go actually for writing the script, we have to decide, or you have to decide which editor you will use to write your scripts. So you have number of options over here. There is a very popular, like Emacs, if you are familiar with the Emacs, go with it. There is a very popular, another one, EIM, it's also the text editor, which is used by many people on Linux, but both of them and Emacs, let me put it this way, so it's the Emacs and VIM, they are quite complicated and they require some kind of fashion and experience. So when you are becoming the experienced users of Linux Shell, you will definitely, you will pick up one of them, or less experienced people, we suggest Nano. Let me run it and let me show how it works. I would suggest if you are not really experienced with any kind of text editor on the Linux Shell, then use that one, it will be the most intuitive, I would say. So when you type here something, type something, and then you want to exit the code, then you just press Ctrl X, it will ask you, do you want to save it, yes, and then it will ask you where you want to save it. And for instance, I want to save it to the Hello Bash, okay, and I'm now in my demo space, and I've got this Hello Bash dot shell. So that's the first script I have created. It's not really a script, I have demonstrated how to use Nano. It's up to you, which one do you prefer. I prefer VIM, and during this course I will use mostly VIM simply because it has really good highlightings, it has, and I'm just way more experienced with this one. So let's try to, I will delete this line, and then let's try to write your first script. Your first script ever will come with this cryptic line. It stands for, it's called the SheBank. SheBank is nothing else than the, it says to the program which runs, which interpreter is supposed to be used. So VIM Bash is path to the binary which will run the code which comes after. And so, and this number sign and exclamation mark is a special, two special characters which are widely used in Linux, especially for the, which are used in Linux for the SheBank mark. So in the same way, when you'll be writing some Python script, you will be putting this SheBank with the USRB in Python. If you will be writing some TCL or some other, something which is just normal text. So that will come into use as well. In the next, let's actually output something, which is the Hello Bash, let it be even correct way. So now I'm done. I'm also able to put here some comment. So let me say that it prints Hello Bash. That's that much you're supposed to know. So then another one about syntax. So this is the comment. This is the SheBank. That's the comment which comes and which outputs the Hello Bash. And then you can continue the stream, for instance, to another line. So just put the backslash like this one. If the line is pretty long and you want to make it more or less compact and readable, you can use it. We don't need it right now, but just this part of the syntax. And then also the indentation, if you are a Python user so that you know that you need to all these streams, especially all these blocks, they're supposed to be indented. But in case of Bash, it doesn't really matter. So you can put another one comment over here like this. Hello Bash again. Okay, it typings. Hello Bash again. And that will be completely okay. You can do it even more compact. You can do it with the delimiter. It will be also okay. You can do it this way. It will be also okay from the Bash syntax point of view. So Bash will not tell you anything about your syntax, about all these lines. So the empty lines, that will not be, that will be just omitted as usually. And this is pretty much everything else. This is pretty much it that you should supposed to know about the syntax. So we will come later to the blocks, we will come later to the if statements and these for loops, et cetera, and the other stuff. And you will see that the actually I will use indentation over there, but it's only for the sake of readability. So let's try to run the script. If I do it like this, first comment not found. Okay, so what happens over here? You can see that actually from the Linux perspective, you can check out what kind of permissions the script has. So even if it's the already something with the .shell, actually, the .shell is nothing else than just the extension. So extension can be any Bash script is not supposed to use .shell. I'm just using .shell because I want to make sure I want to make sure for myself that I know that that's the shell script and it's written by myself. So but only because it's text, it's supposed to be executable. This execution beat over here, it's missing. So they should be X. You remember from the previous course, they should be it's readable, it's writable, but it's not executable. So what first I need to do is I need to change mode and say that my script is going to be executable. Hello, Bash class X. So now you see I've got this plus X. Fine, it's already executable. If I still try to do it like this, hello, Bash, nothing is found. Why? It's because the Bash is actually to not pick up your local files by default. So it expects that your binaries, even including the script, they're somewhere in the directory. So you have to call them explicitly and call an explicitly saying that I need to put the path. In my case, it's just enough if I put dot and the slash. That means that in current directory, take the hello Bash and execute this. So now it has happened. Well, this is our first step. And now what's next? Next, I would like to use the good practice and create a directory where I will be putting all my stuff and not only will be keeping all my stuff in there, I will also make it this way that Bash will actually look at my directory when I want to execute something and the script will be executed without this dot slash. So how to do that? I suggest that you run makedir in your directory where you are bin. We will come to this during the exercise. So I have already been directory, but when you will need to run it just like this. So I have already been directory. I have even a couple of scripts over there, which we will be later use. No, just one. But then let me move that Bash. No, hello Bash only. Hello Bash over here. So now I have it in bin and it's not yet enough. So what next I need to do? I need to modify the environment variable called path. So if you look at the echo path, there should be some directories where your Bash will be looking at to find the binary that you are trying to run. So even the Bash itself, it's somewhere in the directory. So in our case, it's bin Bash. Yeah. But you see that this bin Bash has been already added by default. So it's already part of the default. So it's Bash user, bin Bash, et cetera. So it's part of the default and it's always there. But our local bin has never been added to the path. Path is modifiable. So you can edit this and let me do it this way. So I can use the command export. So now don't go deeply into this. Just type, you will just use it as is when we will be doing the exercise. Export and I will say that export my path and do the changes. So add to my path something which is called demo space, bin. What will happen after? Let's see. If I take a look at the Bash at the path once again, you will see that actually my directory where I have my scripts has been added to that path. And now it's executable. Now if I run something like hello Bash, it will immediately be executed as next. And I can tell you even more. This command type minus a, this is the utility within the Linux, which can always ready to tell you which binary will be run when you type with no absolute path. So in this case, it tells me exactly that hello Bash is like this. Okay. Then what else I was about to tell you? So the execution summary, a kind of executive summary is that you create the file. You make sure that she bank is there. Then you don't really care much about syntax. That's a takeaway message. Then if case, that should be enough already to get your running. But then in case if you want to be efficient and productive, so you will organize your own bin directory and you will add it and expert plates it to the path variable. So that information should be pretty much enough to go for the exercise. To go for the exercise, let it be first exercise because you are still will be playing with the editor. So let us say that it's going to be 10 minutes. Yeah. And now it's 1222 so that we will go till where was the exercise down. We will go first exercise till 32 of your local time. So I put here xx because I expected some people are not from Finland. So some people are from some Wales. So it's not, no, sorry, the vice-versa 30 or let it be three. So now it's your time. You try it. You may succeed. You may not. Not a big trouble. Just ask your question online. And when you're done, please put something like this over here. Or if you see that it's come, the time is coming. So press it over here like this. Or if you still don't really care, you're just listening. Write it over here. OK, so not 10 minutes is yours. And let's go. I'll mute myself for a second. Continue. Can you hear me? Hopefully, yes. I mean, if you can't hear me, then just tell me. OK, so the first exercise has been already done, has been already over. So your hands are already dirty, which is good for me and good for you. So welcome to the world of the bash. Even though that some of you have already written some scripts. Actually, I want to, even if this hello world is not so practical, but let me write another script. Let me go back to the prompt. So if you are a Python user, for instance, then you may think, OK, I'm writing Python. And I have a kind of sandbox where you can test it easily with just writing the command. And then, for instance, you do something like that. And you're just quite. Yeah, that's fairly easy. And you can think about how to do that with the bash. Well, you're already actually there. So this command line that you have, it is already the kind of ready to go prompt for you. You don't need to put bash. You don't need to put anything else. You just put your comments and everything what you want you can put over here. So in this way, bash is adapted for the interactive usage. So scripting is just the kind of fancy addition and the kind of value added to the bash. But bash itself is mostly about prompt. And most of the comments you can run as is just from the prompt. And this is what we will use over here. So some of the statements, some of the expressions I will just test it from the command line before actually adding them to the script. So scripts are made mostly for a sake of automation. Scripts are made mostly for a sake of if you have a long list of comments, you don't remember all these parameters, and you want to make out of this complex construct of some utilities. You want to make out of them some kind of one single comment. So this is what for you are using the scripts again. And see that, for instance, one example that I will show you, so for instance, I will be doing this archive in time of my demo space and saying that I want to compress it. And my file name, which I want to my archive name, will be like demo space targ.gizet. So it's the quite common in Linux, tar utility and the gz compression. And then I am saying that actually my directory, which I want to compress, that's the demo space. So I can just put one single dot. But for a sake of readability, let me put this way demo space. So I'm doing the compression. So these are warnings that you can skip them. So now you see that I have got the file over here. I'm not exactly kind of, I'm not exactly, it's my colleague over here. So I'm not exactly, maybe wants to do it time to time. So what I want to make over here, that I want to put it to the bin and make it one single command, shorter one and easier to one, and which will serve us for the whole course. So we will do the modification to that. So let me call it tar me, for instance. My shell script will be starting once again with the bin bash with the shebang. And then I'm putting here my comment over here. And as a good practice, remember to have some kind of comments and some kind of revision history. So what you've done and when you have done. So I'm saying yet nothing about the git over here. But I encourage you in your entire life after all, use git for a kind of versioning. Here we just skip this part and make it kind of more simple. And the git part will be covered already by the code refiner when you will be there. So but here I archive my directory. So that's the easy way. And then let's try once again, change mod plus x. And what's good about this, because if you put tarme.shell, you will see that actually it's immediately found from my bin directory, because my bin directory has been already added to the path. And I don't need to do anything else if I decide to go and make another archive. I just run tarme. And that's it. We will use this further to develop a bigger script. So this will be part of our exercises. It will be part of our demonstrations overall. So this is why I did this right over here. Sorry. Variables. So definitely what you want to know about when you start programming something, how to use the variables within your code. So the variables is nothing really specific. So let me start doing this in the prompt. And then we will move these variables to the file. So variables, it can be any. It can be just the letters and digits. And then minus, that could be hashed. So there is quite a number of the. There is some special characters if you go to the variables section of this material. So you will see there were somewhere a list of special characters. Yeah, sure, it's here. It's coordinate substitution. So these characters cannot be really used in the variables because they are part of the other syntax. But then everything else can be used as a variable. And so let's see what else. So variables, for instance, are signed in the variables. We just put the equal sign and say my first bash variable. It will be like this. It's already there. So in order to call that variable, it's enough if you put the dollar sign in the front of the name. And so you can see Echo will give you the output of that variable. In the same way, you can use this dollar sign variable name within any commands or within any part of your script. It's just will be calling. And it will be returning you the value of that variable. Variable, there is no kind of definition. There is no points. There is no strings, Booleans, integers, et cetera. So all the variables inside of the bash they will be used as is. If you are doing this as a string, they will be interpreted as a string. If you are doing it as something else, if you make an integer summation within the integer expression standard, it will be interpreted as an integer, et cetera. So bash is really flexible in terms of variables. So even if you are not defined it, but you still call it, it will just return you the empty variable and not the error. So be careful about this. This is actually quite common errors that you are misprinting or mistyping the variable and you call it later and then you are surprised that, OK, nothing comes out. Actually, bash doesn't really take care about it. It relies on you. And it still thinks that you are making your scripts pretty much robust in checking whether the variable is empty or does it exist at all. So here it comes, the place where the quotation also matters. Let me open once again the low script and I will use it as a sandbox for this kind of situation. So for instance, I want to assign a variable and let it be some text. And then if I go to the echo and want to output that variable, that should be enough. But then if my syntax or if my sentence is more or less complex, so if I want to say that var is, OK, I have to use already, well, not exactly. I have to use with the echo, but let's assume that we have to also use the echo. So I have to use the quotations. And here comes the difference between the quotes. So the echo with the single quotes will produce you exactly the sentence you have within the single quotes. It will not open for you the variables. But the double quote quotation will do it for you. So here, taking out quotation matters. So then there are a few other things. For instance, less complicated cases. So let's try it out. So now we will actually, we can use it. We can comment this once, just we don't need it. And use it as a sandbox. So it has been already added to the bin. It's executable by default. And now you can see the difference. The first line has been using the single quotes and the second line was using the double quotes. So yes, take it into account. Along with the variables which you kind of create yourself and define yourself, there is a bunch of other variables which have been already created for you by the environments, by the bash itself, and by the Linux itself. You will be surprised if you type this print and you will see a long list of all kind of predefined variables. Some of them read only, some of them are changeable, but most of them you don't really need to touch. Except with something which is kind of, I don't know, well, I don't know even the exact example when you need to change the variables. Well, sometimes you end up changing this, for instance. I've been doing this for the script, which is over here. But anyway, so and a few examples that you may find useful. The first one was already mentioned. That's the path that gives you the list of the directories where a bash will be looking for the binaries when you type it without the absolute path. Then home. It's exactly your home directory. Then, for instance, shell. That's exactly your bash shell, which is defined for you. So I said nothing about the other shells, but that's probably the good point to do that. So bash is just one of the shells. We were talking about this, about this on the first part of the course. And there are other ones, which are perfectly fine to use as a shell or something else, like current shell. It's up to you. But this course is focused on the bash, mainly because bash is the most popular nowadays. And this is also the shell number one for us, because we are running the big cluster installation Triton and so on. Triton, we expect everyone used to bash. So this is one. And because I'm part of the team, so we pushing people to use bash and the whole course is about bash. How to change your shell? It's the change shell command. If you want to do it on your local desktop, it's easy to do. You do the change shell, and you just put the bin bash and your next session will be started with the bin bash. OK. Said that, I think we can now jump to the exercise number two. So I call it variables. And I will give you another 10 minutes to do what first you need to adapt your hello bash shell and assign the hello bash to a variable and print the variable to the output. So I've done it already for you during the demo. So now try to use your memory. Don't look yet at my hello bash, but try to use your memory and try it out. Then the next one, which haven't been done yet, but now you have to implement it on your own. So your script, which will be placed also in the bin and call it whatever you want. I call it here print vars, which will print for us the home shell and path one per line. So now time is yours, and we are 48. So let's say that already 49. So let's go this way. We will have both and the how to call it break. Let's call it joint break and exercise sessions. So we will come back to the screen at 13.09. So 10 minutes for the break, 10 minutes for the exercise, and you decide how you use in which order. I mute myself, and I will add it to the material. So my next stop was actually tell you even more about the variables. This is something which is very specific to the bash. This is what you probably will not see in most of the languages. So when you can do something with the variables on the fly, the constructions looks like this circular brackets column, and then some kind of cryptic syntax which will do something to the variable as a string. So what can be done with the string? So you can make it, for instance, all capital, all small. You can replace some part of the variable value. Then you can, for instance, check whether this variable exists or not. You can redesign it, redefine it with some default value. So there are quite a number of things that you can be done. So I'm not really put everything to this material, but the selectives of them are over here. So you can check them out. And then on top of that, you can also actually forgot to say. So if you're ever thinking about syntax, if you're ever thinking about some particular commands which have been used within this bash scripting, so all the internal logic is written in the bash manual. If you go to the for-demand bash, you will see the very long, I mean, it's at least 100 pages if you will print it out in your normal font size. But you will see all the details about what else and what kind of options, what kind of arguments are given by every single command which I use in general. And then what all these magics, all these extensions, all these practical aspects of the bash programming. So the magic I was thinking about is that, for instance, I know that the echo var already exists because I have it designed. Actually, let me tell you one thing. You can unset the variable. So if you don't really want this variable to exist anymore, it will exist there as long as your session is there. But if you don't want to exist it anymore, you just can do it with the unset var. And now if you see that I am trying to address var, it doesn't exist anymore. Bash is not complaining anything about calling the undefined variable, but it's just returning the empty line, which is fine, which is not an error. That's just a feature. So let me say that I want to define it somehow. And for instance, it's still undefined, but I want to check it out. So my approach would be here, checking out, for instance, this one is pretty simple for the sake of checking out the existence of this variable. So you see what will happen. So echo or bash on behind of echo, it will check that variable exists or not, and then give this error message which comes after the question mark. So here I'm saying that bash var not defined. So if you use it within the script or within your function, it's perfectly enough to make your script more or less robust. There is another one option. If you set up an exclamation mark, you put something like equal. That would check that variable. And if it does not exist, it will assign some special value, some default value. It could be anything. Then just put default value. It's up to you what you decide to do this. But then if you see from now on echo var, even if it has been undefined over here, you see you remember that. Now it's already has the value. And the thing to remember, with all these major constructions, with all these curly brackets, this column and the equal sign is the only operator which actually does something to the variable. It changes it. It assigns the default value or another value which you just put it over here. All the rest, all the rest which are listed here as the selected operators, they do really nothing to the variable. They just simply extract it, or they modify it on the fly and put it to the line, to the online, give it to the output, print it to the output. But then they actually do nothing with the variable itself. So a variable stays as is. So let me do the example, for instance. Here, I'm grabbing the F path. So I'm saying that that's my path to the file. And I'm naming that variable accordingly. So if you take a look at the F path, it's pretty much like that. So what I want to say that I want to first get the path, for instance. Here, this cryptic says that, OK, return me everything. What is before the last slash? So actually, everything except what is after the last slash. So now I'm getting the name of the file, actually. So you see that I've got rid of this one while the file itself, I mean the F path, has not been modified. It's only the modification which has happened on the fly and it has been outputted to the script. In order to make it modified, actually, you have to reassign it to another variable. So if I assign it to another variable and say let's call it F path 2, now it's going to be already something which will be remembered, which will be saved to the memory and which will be part of your global space. So you see. And then, for instance, another example. If I want to keep running this F path thing and want to return only the path, so that's the another approach. So I'm not going any more deeply into this. I want you actually to go this deeper into this thing. So learn this section and let's try it. Let's say that we go for another 10 minutes playing with this variable magics. So what I want you to try, I wanted to try in this construct where you have the variable name and then the curly brackets and then something after the variable name. Lead on your own or actually if you will be ready, here is my, oh, did I put it over here already? So here is my suggestion for you. So you pick up once again your hello-bush and you change it in such a way that this variable hello-bush will be printed in capitals. That's the first. And the second one is that you will wrap once again that F path variable in the way you want to, but I mean you just copy paste from the course material and make your script to return file name only without the full path and extension. So the hint over here is that I've said you previously over here is that you will not be able to do it with one single iteration. You will have to do it with two iterations. And for that you will have to save one variable to another. So let's see how you will manage this to do. You will have now 10, maybe even 15 minutes because it's gonna be quite a challenge for you especially for the first time, but then let's go. So we will finish when at 13, 25, okay, we're gonna keep going. So let me demonstrate you with your techniques over here. So the first, I will also put it to the answers to this both exercises, to both task of this exercise will be on the notes, but then first let me go with demo. So quick demo for the first one, echo. And then we were expecting some modifications to the bar. So we need the curly brackets. And then we need to say what we want. So we put two hats over here. That should be enough. Let's see what we get out of this. So yeah, so you can see that actually the first one was from the previous version of the script and the second one was already the first, they said the other one. So regarding the F path, playing, so there was a request to do the, get the only the archive. That was somewhat complicated and complex. And I do hope that that place, some of you have got already some of this. So let me grab this one and say that I go to the bin and I will create my new script. I will call it some kind of F path. And I will play with that. And I will say that it's gonna be bash once again. And let's see what can be done. So I actually can use pretty much the variables, modifications, this magic from the learning materials. So, but here what I'm getting, I would get it only on the fly. I don't really want it on the fly. I want it to be another variable. But here I don't really care about the original F path. I can simply reassign it in order to make the modification, not on the fly, but save them to the variable. I can simply assign it this way. And you will see later that actually, so let's say that I wanna put the F path. And I can say here that's the phase one. It's gonna be the F path part, which will be, I will take off everything, which is behind this one, this slash. So the only thing which will be left to me, that's this one. Okay, and now that's the moment where I can already, if I don't care about what's the final result of the F path, this is where the moment where I can put something like this, but modify it a little bit. So let's say that here I put the comments, just to simply here, we get archive with extensions. Okay, you can check it out from the command line. I know that it's gonna be like this already because I already checked. So I just put it to my script already directly. So now, so this block is over here. Now the next step, get archive out of. So now we have to get rid of this too. So how to do that is that we can say that, okay, we don't need dots and we don't need anything out of these dots. Now the trick thing, because of this syntax, so this will allow me to get rid of the only the last one content of which comes after the dot. But what I want, I want everything after the dot. So in this case, I will have to use the true percentage. So that was the complication over here and that was kind of thing that you had to find it out from the manual page or just to try to fade with that. Okay, now let's see what's going on. Change mode, let it be, okay, okay, it should be plus X. And now I'm ready to run F path. And so in the phase one, we've got archive target. And after the second one, we're going to archive. So take a look once again. It's nowadays looking like this. So let me put it to the solution part where my 15 minutes functions, that's gonna be the next one, here is the, so here's for everyone who wants to take a look at this later on. Okay, now we can, if any question, please ask, if something is yet unclear. So I can take a read of this one and we just have actual the part which is, which we need. So saying that, I think I'm done with the variables and you know already pretty much a lot. So you know already how to use the variables, you know how to find them, how to call them. You should now by now know that the variable can be undefined. And then you also know this magics of this bash variables which you can do with the curly brackets and do some modifications on the fly in case you want to do this. So if you want to substitute this modified variable and put it to the command line, so that's the way to do with the curly brackets. So now the next one I wanted to try is functions. Functions, as everywhere, it's just piece of code which you can use repeatedly. Usually, I mean, if you know that within the same codes you can, you will use the same piece of the same snippets so you can just put them somewhere else to the function and then use them all the time within the code. This is quite normal, this is quite usually and this is not an exclusion in terms of bash. Functions are there, the only difference, the functionality of the functions is nothing really extraordinary. It's just piece of the code which will work in the normally global space. So basically you will have access to all the variables that you use within the script. You will have access to all the variables, the global space that would mean that you have access to all the variables that you have assigned it within the script itself. And then on top of that, every single modification of that variable that you do within the function will be seen to the end of the script. So just remember this. And this differs this functions functionality from the other languages. On top of that function does not really return result of the modification of this variable with the return command. It does have the return command, but return command does nothing except returning the exit code. The exit code, this is something which is, I already mentioned it once. And so every time, every time when you run some command for instance, if I run something like echo test, you can expect that this command has returned some exit code. The exit code is saved in the variable called dollar sign and then the question mark. Okay. And in case of bash, if it's zero, that means that the action has succeeded. And if it's non-zero, one, two, three, any other digit. So it means that the action actually has failed. So this thing to remember and to take a note if you are doing so. And let me get back to the functions. What I suggest to do is that I suggest to organize the file which I would call functions. Surprise, surprise. You don't need to put .shell or anything like this. It can be just functions. Another one, surprise that within these functions, those, the file where you want to define it, you don't really need to put the .shell back. I will tell you, I will show you how to call the functions later on. But just for now, it's enough if you just do it without it. So my first example will be for you. Is the usage of your space? So let me first get out of this and copy paste the command from the material. You can do it on the same way. So what the command does, it shows you the files in this current directory and sort them out by the size. So in my situation, I'm trying to look at the all the files, including the hidden ones. So the hidden ones, as you remember from the previous course material, that's the one which start with the dot. So by default, if you don't address them specifically, no one of the commands, neither LS, nor other commands will see them. So this is why they're called hidden even if they are not really hidden in such a way so. But you must say explicitly, and this is the expression how to address all the hidden files in the current directory. So because no one of the hidden files is found, this usage gives me the warning that nothing can be accessed. It's completely okay, it can be ignored. But the other files are over here. So I have tested this command and now I want to make a space usage out of this one. So let me write the function. Or did I have a functions? Okay, I probably haven't functions. I haven't, yeah, that was empty. So now let me go into that function and put the body of my function and then put the actually name, et cetera around. So the way you write the function, you'll open the curly brackets. So you put the name of the function. It should have this brackets, round brackets, and then you put the curly brackets and then you put actually the name of the command itself. So now it has been defined. Now it's in the file. And the function file does not need even the execution bit. So if you look at the LS minus L functions, you will see that it's nothing else than just normal text file. How to source it? So you can go to, you can just crop that file functions and type source. And you will have this functions part of your environment. How to check it? Again, you can use type minus A and see that what kind of function is behind the space usage. And you will get the list. So there are two signs here. First, space usage is part of your environment by now. And second sign, that the space usage is the second sign that you can see that's actually, it's supposed to work right away. So by now, because it's your part of your environment, you can run it pretty much like any other binary. So now you can see that after running space usage, there will be this kind of list exactly that I would have get out of this command. But this command is now has a kind of unique name, space usage. The name you come with, it's just up to you and actually Bash doesn't really care except that you are, if you are not trying to rename some real, some real binaries. And even for that, there is medicine. I don't tell you yet anything about this, but for this there is medicine as well. So let me say that I'm pretty happy with this already one, but there is something else that can be used with the functions. Functions can accept the input parameters and input parameters. So if let me say that if I call space usage like this and then if I say some arguments, number one, arguments, number two, arguments, number three and so on, they will immediately be within my function, they will be immediately recognize us dollar one, dollar, where's dollar, dollar two, dollar three, et cetera. And we can use it. Okay, so let's say that we expecting some input arguments, input parameters, and let's say that my space function will be actually expecting a directory so that I don't really go and check the current one, but I can check anyone. Now what to do? Now I know that my directory is going to be like this. Yeah? And so what kind of thing I can implement over here? So I can say that everything within this directory like this or even more, I can say that everything within this directory will be checked. It's already a good progress. So we've got already something to solve, but in case, in case, I want to make sure that actually this directory is provided, how to check it. And here where we can use our magic variables. And so we can implement it in a way that, okay, I keep it like this, but I put my curly brackets and I use this operator which will say if it's equal, if this one is not defined, then let it be my current directory. Current directory in the bash notations is dot. So in the same way, I can do it over here. If one is not defined, then I want to say that let it be dot. Now we are good enough to go with the new space usage. So now we already have created pretty robust code. Let me check it once again. Was it the minus or was it the, yeah. Okay, so here I'm doing one thing. So I don't want to reassign it, but I want to return it. So I'm using the minus operator. You see what's the difference? With the equal, I would reassign it. So I don't really want to change and I will not be able to change this special variable dollar one. But with the minus, I will be able to return the dot without changing the variable itself. That's the difference. Okay, now we are pretty much robust and now we can even put again return the space usage, the space usage, well return the space usage with disk usage command. Now let's try. Now we need to source it once again, functions. Now let's try type once again. Now you see that my script function which is now part of my environment has been modified. And now let's see how it works. First, I will use it with some variable. So I want to, for instance, check the, my demo space. Okay, let's see, it works. So once again, this one is just about the dot files. You can simply ignore this and then if I put no parameter, what it do for me. Okay, now I can see that actually it's trying to do it the same way like this. So which is pretty much what I have expected out of this function. Say it again. Now you have written the first function which can be used then within the code. So how to do that? Let's say that I want to, I will demonstrate it for a sake of demonstration over here. So now let me do the kind of dummy file. Let me copy functions to some space, whatever, space.shell. So I do it only for a sake of demonstration how to call the function within the code. So I'm now looking at the space here. I already need this shebang because I'm doing the actual scripting and snow again in the previous file, which was called functions, I was only defining the function was not executing them. In this situation, I want to define it first and execute it after. So and that's the way to space usage. And then even if I put some arguments, I can put the argument right away, space usage. And then for instance, let it be my home. Yeah, like that. Let's see what it gives us. Oh, I can do it with the more readable format so I can give it with the home. So let's see what we will get. Again, we will have to add the execution bit and then because we are already in the bin, we don't need anything else, space shell. So yeah, pretty much works. Okay, so now you've got also the idea of how to call the function within the script. And again, if you're kind of lost and you still can go to the bin and you can check it from here and see what's going on. So I don't really steal all this, these of course from you. So you have access to them. So what else I wanted to tell you about the, there are also options, how to use this space within these functions, how to use them with the local variables. We don't really wanna check it out. One thing that I wanted to introduce you, another one, beneficial part of the syntax, which is called the command substitution. So let me say that I want to define a function. I want to call this function me. Okay, so what I'm doing right here, while defining this function, I'm actually putting to the command line, not the command itself, but the outputs of this command into one single line. So command line substitution means that you actually want to substitute the output of that command into some part of the codes, into some part of the other command that you are typing. It's quite useful. You will see it's in the third examples that we will run. And now just one single to show you how it works. So I have created it. So I have it added to the functions. And now I can once again with the type minus AC that my function has been added. And if I run it, you can now see, okay, host name is not found. I didn't check it. Host name minus S change, well, whatever. So, okay, actually I know the problem over here. So it's good that we've got this error message. So what has happened just now? Again, this construction where you have the wrong brackets and the dollar sign at the very beginning, it gives you the output of the command and it just prints it to the prompt. So for instance, I want to check ID. Yeah, and I want to check ID with the, yeah, and so I'm just checking my username basically. If I do it like this, it's completely fine. It will return me the actual output of the command. But if I will try to do it like that without substitution it to the code, without substitution it to the prompt. So I'm getting the error message. Why? Because bash in the ground, it will execute this common substitution. It will put the output of the common substitution to the prompt and it will expect that it's going to be a name of the command, which is not correct. And so this is why you are getting the error. Let me fix that functions and say that actually what I'm looking for is the echo. So what I need, I need to put them all together and then output it to the screen. So let's see what will happen. I have to source it once again. Now I expect that it has been fixed. Yes, now you see that I've got the perfect information about myself, who am I? What's my group, what's my group? And then also what's my machine name? Cool. So once again, now you have at least two functions defined. You know how to do it, you know how to call it. You know how to call it function, both from the prompt and from within the script. And then on top of that, you also got some impression how to use the command line substitutions. I think we are done, but not yet fully. I still wanted to add you something with the script because I touched the command substitution. And so what I wanted to do, I have to, I wanted to demonstrate you some other commands. So let me introduce you some practical usage. Well, me function was also practical, but this is even more practical than it is. So you remember then TAR command, which I used just with the explicit, saying explicitly what's the name of the TAR archive is going to be, and then saying explicitly which exactly data I wanted to archive. So here what I'm doing in this command, let me explain that. So let me make it more readable. Here what I'm doing, I'm using the command substitution for a good. So first of all, I'm using date. Let me say that I save this command for you without executing right away. So I know what date will provide to me. This format of the date will give me a month and a day. Okay, that's what I want. Then what I want else, I want the current directory, but I want to have a current directory only the actual name one. So remember, we've done it with the variables. Now I introduce you another one way to do that. You can use the built-in utility called base name. Or maybe it's not built-in, maybe external, but anyway, it's part of the system. So now you see that I'm getting the bean. So out of this one, out of this nested command substitution, so first of all, the command substitution can be nested and Bash understands this kind of syntax. It's quite clever. So you will get bean, here you will give date and the rest will be just as normal. Okay, let's try it and let's see what will happen. That's correct. We have created some files inside the directory that we are archiving, so tar was given this warning. And now you can see that actually we've got the tar archive, which is named bean and then the date, which is dynamically perfectly generated name, which can be used for the scripts. So my suggestion that we will use it right away and actually we will go to this, well, was tar something, tarmi. And we will use it actually not as a function, but we will use it as a command. Okay, so pretty good, we're already on the functions. And I think we are ready yet for another 10 minutes exercise. So let me see, it's there, or maybe even 15 minutes, simply because it may take you 15 minutes. So now what I want you, this is the simple one, you can copy paste it as is, just create the bean functions and make sure that space is usage in me, both are there. And then on top of that, what I want you to ask is add yet another one function to that functions files and test it. So let it be called fast find function, just the FF. And what it's supposed to be, you provide with the fast find any search word, any. And it must return all the files and directories in the current folder, which name contains this word. And as I just let it be case in sensitive. So in case of find, you just put this minus I name so basically just use this one, put the search word, the what I mean, if you're not familiar with the find you still have some time to try man find or you can take a look at the course material from the previous part. So you can find some examples of find usage over here. And yeah, let's say that we get 15 minutes because this is I expected that you're back to the functions and you will read something maybe more and then you'll write your first function and make it to work. So 15 minutes, so that means that we are back to the at 14, 22. So we have left now already less than 30 minutes to go. It's almost 32. So I see that less and less people are actually in time with doing the exercises. So we will have a short also questionnaire at the end. Don't run away. We will ask how complicated was the material so on. You will actually your opinion will have an impact. So if you think that we are pretty decent for the intermediate level, your replies will only welcome if you think that it's too complicated or too intensive or et cetera, your replies even more welcome. So I will try to adapt by tomorrow the material if you decide to do so. So let's get back to the functions. So the space usage and me actually the easiest probably was just to go to this demo space which I have posted on my webpage. Copy it from there and there you would get the ready to go functions. So please do it if you haven't done yet. So, or if you were typing on your own then just consult these functions that this syntax is correct. So one question was why that was returning the error message that command does not exist. So this was explained in a way that common substitution will print to the prompt the result of the executed command. And that result will be already interpreted as a command. And so if you just put your login name on the prompt then of course there is no that comment with that name. And so basically you will get the error message that the command doesn't exist. Just a second, I close the room back online. The other one, fast found. So what I was trying to make you to do. So find function, where was it? Find name. So that should be already good enough to get started. The only thing that you need to substitute the search word. Like for instance, if I would like to be some kind of word, I don't know. Let's say that I, what kind of stuff I have here. Do we need store bin? Okay, let me find, for instance, the bash history. So now I want to I name and then I want to find something which has bashing name. That wouldn't be enough because it will be looking explicitly for something which is named bash. But we are looking for something which has bash in the name. So we need to put this stars just to make sure that actually we are looking for something which has bash. Now that should be good enough. And we found already bash history which is in the current directory. And we also have found the hello bash which is somewhere down there. Okay, now we need to create a function. We have already ready to go command. We have it tested from the prompt and we are ready to put it to the functions. So let me call it fast found. I open this this way and I close it this way and I put my name over here. So I also was told that this should pick up any search word, not only bash. Okay, so we can remove it from here and then we need to specify what we want. The good practice if we actually use some specific variable over here, even if you can use this dollar one and you expect that the variable exist, we usually, and the best practice I usually suggest, well, I'm not sure about we, I usually suggest that you reassign this dollar one to some other variable. It's made in the way that you do not occasionally actually would try to edit this or modify and otherwise you would get error message. So in this case, that should work perfectly. Yeah, I guess so. But let me see, the only error over here is that I don't want to use the quotes, single quotes. I need to use this double quotes. Why? Because I did the explanation at the very beginning of this session, double quotes make the bash to actually open the variables and substitute the variables. Single quotes will just take the word as is with the dollar sign and nothing would happen. So this will really something and let's say that I want to source it once again and I wanna see that type minus AFA and see it's there. And if I wanna try this again with a bash, see whether it works, it works. If I wanna try without bash, does it work? It does work because find will find you everything even if you provide empty stream. So basically it's just we're using this star to tell you that it has lots of file inside. Okay, we don't really want this kind of situation. So we truly want to make sure that actually this word is not empty. What to do in this case, we can use once again the logic and magic of this variable exchange. And then what we do here is that we are supposed to actually give out either the variable itself or the error message. And so that we can say here that what was a directory or search word. So sorry, we are search word is missing. Does it work for us? Yeah, I'm pretty sure this would be already good enough for us, but one thing to remember, we still don't want that this find will be executed. How to do that? And this where we come with another one thing which I didn't tell you yet, but I will tell you on the next session, but I will already use it over here just to make this kind of function complete. So let's say that I will execute it like this and let's see what's going on. So source. And then if I run ff, search word is missing. So it's already completely correct. And so we have just exited the bash. So we don't really need anything over there, but then if you wanna be completely sure what's going on, actually let's don't go that far and let's go to the next session and then I will do the explanation and do the modification to this code already right away. So to have it on the line and go in line with the traditional thing, I can put it as a place. Here you go, bash. So here you go and let's make it more compact like this. Good, so let's get back to the material. So my next step was actually touch a little bit the redirections. So I'm not going to the conditionals yet. So I wanted to tell you something about redirections because I wanna touch a few other things within these subjects. Redirections have been actually pretty much covered, I think, well, due to command line utilities, I think they have been, yeah, redirections have been covered pretty much in the same, in the previous ones. And then also we have, it's quite a lot of things covered also within this material. So that's a kind of advanced one. But let me say a few words because it's important even if you know this, it's still important. So every time, every time when you are running some kind of any command, when you are running any script, whatever program, a lot of happens actually under the hood and one of the thing which has happened is that you are getting along with your running file, several file descriptors open for you. Some of them are reserved and even enumerated. So that's the zero, zero, one and two. And they stands for the standard input, standard output and standard error output. And all of those guys, they actually can be controlled. So you can control where, by default, if you type something like echo or if you type any commands and hit output something, it goes to your standard output. And the standard output is the screen, the terminal of your terminal. So you just see it on your display basically. But you can control where it goes. And one thing to do is to actually do the redirection. And you can redefine actually all of them. But let's say that I want to do the redirection of something which I don't wanna see. I can use, as an example, pink command. So pink, one of the Google DNS servers. What it tells me that if pink goes through, that means I am online. So I don't want this to be interactive. I want to just one single packet which will be sent and then exit it. But I still don't want this one go to the screen. I want only the exit code. And we were talking with you already, the exit code. Even if you don't get any output of the command, you can always take a look at what kind of exit code was given by that command. And if it's succeeded, then the exit code will be zero. If it's not succeeded, it will be something else than zero. One, usually one, or it could be two. It could be in two, five, six, six. So that's kind of exit code. So I think maximum is 256. That's the maximum, but can be wrong. So not really sure. Usually from the programmer perspective, you can, by the number of this output, you can say what exactly has happened and what went wrong. Okay, but in this case, what I'm saying here, I'm used the redirection sign. This says me that I want to redirect the standard output to some other place. So you can do it and redirect it to a file. And that will be part of the exercise. But you can also do something else and you can use the special device called null. On Linux, that means that everything that goes to the dev null will disappear. It will not be seen anywhere. So let's see what will happen over here. Pink is okay. And then if you see that I can actually check the status which has been returned, it's zero. So it's correct. Let's emulate somehow the situation that we didn't reach the pink. So I will not be disconnected myself from here, but let me just come with something which does not exist and which will generate the error message. So in this situation, pink actually has returned the error message. It still came to the screen. And you see that actually the status, the exit code of this command was something which is non-zero, which is already from the point of view if you want to, for instance, run some kind of if or conditional statement. So this will be already good enough to say that, okay, if it's not succeeded, then do that and that. Okay, but we still have one other problem. If there is an error, it's still on the screen. So what we do by default, this redirection sign will redirect everything which goes to the standard output. But we also want to redirect everything which will go to the standard error output. So how to do that? And we can say explicitly here that please redirect the file descriptor number two. And here we put the redirection sign. To the one. So what will happen over here? So from the bash point of view, both standard output and standard error output will be joined together and redirected to the standard output, which means they will go directly to def null. What will happen here is that no error message will appear on the screen. And this exit status is the only one we get because we are just requesting explicitly. It's correct. But in the modern version of, so this is the most common syntax for this kind of notation. But in the modern version of the bash, you can also use the compact way. So you can do it the same, but probably the other way around, the other way around. Ah, yeah, that was the problem. Not this one. Yeah. So here you go. Here you actually you get the output, only the exit output. And what else I wanted to introduce you, but actually not the introduction, but the kind of remembering from the previous course is that this exit status can be used with the logical operands, that's the operands and the bash syntax. That's the logical end and logical or, so basically end says that if the exit code of the previous command was not zero, was zero, so if it succeeded, then do next. And we can say that echo, we are online like this, yeah? Let's put the quotes just to be more readable. Essentially, we are not. I mean, we are getting the error message. We are not getting the error message, we are online. And in the same way that if something went wrong, we can always say that echo, we are down. Okay, at the moment we are online, but then if something went wrong, whatever. So we will be getting the message that we are down. So this is my touch on the redirections and then these logical or, logical end and logical or. You can use it within the script easily. You can use it from the command line and this is the most often used, how it's most often used on the command line, but nothing stops you from using this on the screen. Then another one thing which I was about to tell you is that actually usage of the pipe. Pipe, this is something which stands for this one. So basically output of this command will be redirected from the standard output of one command to the standard input of the another command. And so let's see how it works. So we have had kind of couple of examples, like for instance, actually we have had already this discusage. So let's try it once again, what I did. So what's happening over here is that we are redirecting everything what they've got from the discusage command to the sort. And the tricky thing that if you want to redirect also the error from the command number first, you will also do it this way. Okay, maybe it's the other way around. So I will, yeah, that's the other way around. But actually if I want to get rid of, if I want to get rid of, for instance, this message, you can do it from this exactly situation. So I don't really want to get everything what comes as an error. I only wanna get something which comes to the standard output. And so you can see that our output is way more clear. Even if it's harmless, it's still, if you wanna make your code robust, you can do it this way. And also have demonstrated you how to do that the other way around. And I think we're pretty much what I was thinking about. So we have eight minutes left, but if you're up for the yet another one exercise, I was actually planning to finish right on this exercise. So we can try to do it right now. And then we will come back to this on Wednesday tomorrow at 12, I'll do the explanation. So let's say that the rest of the time you spent playing with the redirection and piping this exercise. Do as much as you want, as much as you can. I'll be here probably for the next 15 minutes at least. And we will continue otherwise tomorrow at 12. And in the RICO, if you want to get something like how complicated material is, that's also a good chance to do it right now. But otherwise, let's say that our last exercise for today is this one redirection and piping. So try to do it. That should be fairly easy, but except the last one. Last one will probably take you some time to get to the point, but it's also doable. There should be no something, no much of the troubles. Okay, let's try to do this. And then I will get back to the screen. We will be about three minutes late running today, but let's hope that's okay.