YTread Logo
YTread Logo

Bash Scripting Full Course 3 Hours

May 02, 2020
In that case the negative V showed the number of lines that did not contain the word windows, okay, so the number was 16, okay, eight lines, eight lines can have windows and we discovered it using the flag, we see a C negative and the number of lines that do not contain the word windows are 16 and Also, we discovered it using the negative V flag, so I hope you got the point, this is how you can basically use grep, you can use your own regular expressions that you want . I think we have a regular expression video on our channel like Well, go ahead and look, that would put the link of that video in the description or the I button, so there's a lot of things you can do with the grep command, so to check it, what you can do is you can go to man grap, okay, then you can go to the man pages and you can see that there are many options for grep and you can learn many options, you can practice them and you can see what are the results that you have . everything was good so let's move on to the next topic so our next topic is introduction to a wk so the a wk command is basically an extraction language that is used to manipulate data and generate reports so today in this What are we going to do? is that we are going to run this command inside our

bash

script file, but the idea is that a WK script is a completely different topic and you know it has its own different files with the extension dot to WK like we have it In impact in the case of

bash

we have the files with the extension dot a set and in the case of a WK script we have the files as dot a WK however it can also be used in the bash script and that is what we are going to do Let's go to use this WK in the case of bash scripts, so aw Kate, come on, you can basically say that it is a programming language and does not require compilation and allows other users to use variables, numeric functions, string functions and The logical operators are also okay, so a WK can basically be said to be a utility and allows a programmer to write small but effective programs in the form of statements and those statements basically define text patterns that should be searched or searched for. the document will be searched and the action that will be taken when a match is found within that file, okay, so you can say what can we do with awk, basically three things, number one, which can scan a file by line, is Well, number two.
bash scripting full course 3 hours
We can basically split each input line into different fields and we'll see how we can do that. We basically do it for dollar 1 or $2 $3 one represents field number one dollar two represents field number two and so on So there is an additional fourth operation that you can perform and that is basically you can perform actions on shredded lines. So another question you might ask is what is this useful way? It's a very valid question, so the idea is that it's basically a WK. It transforms data files and also produces formatted reports, if we talk about programming constructs, it gives you formatted output, it gives you the ability to perform arithmetic and string operations and also gives you the facility to use conditional statements and loops, so now let's go ahead, let's try to do some examples, so let's go to our hello script and here we try to run some things, ok, our bash script, so let's go here and delete this and what we can do is basically print a file using a WK, then, how do we do it?
bash scripting full course 3 hours

More Interesting Facts About,

bash scripting full course 3 hours...

First of all, let me know, use the above program, so I would write here, enter the file name to print from a wk, okay, here you will know the actual that inside a perform is here, so here we write a week and then we type single quotes and inside the single

course

we print ok, so any operation, any functionality that you're going to perform, you're going to perform a within these single

course

s and then we're going to type here the file name, which is essentially the name of the variable file , okay, that's it, we save it and we go to our terminal and we try to run our script, so we press Enter and it says: enter the file name to print from a week. let's say I type here, let's say the file was captured by grep dot txt, so press enter and you can see that it has printed all the stuff, okay, so don't get confused with the file name, it's just a name, it's okay, so no It doesn't necessarily make it a graph file, so let me change the name so I don't confuse you, so I'm going to name it like let's say, okay, okay, now what we can do is basically we can also print the lines. that contain, you know the word, basically, a specific word, so what we can do is take another input or you can just go here and type four slashes, four slashes and inside these four slashes, you can type here, let's say we draw eight Linux , so now what this program is going to do is print the log, display all the lines in the file that contain this word, which is essentially Linux, okay, inside our script inside hello. script we're going to write here let's say we write here windows windows okay, so we save it and we go ahead and try to run our script basically.
bash scripting full course 3 hours
I think we have closed our terminal by mistake, so we go to our desktop and run our script it says enter the file name to print from a week, so we write here the file name, which is a WK file, dot, txt, press Enter and you can see that it's basically printed on the lines that only contain windows, okay, look at that, so there's something else. what you can do, you can actually do what I was talking about dollar one dollar two, you can actually print specific fields, okay, so let's say, let me go here and delete all the lines here and keep the three lines and Also we add another fourth field, so let's say we type here, this is Linux 2000, this is Windows, this is Windows 3000 and this is Mac 4000.
bash scripting full course 3 hours
Okay, now let me copy it, paste it and save it, so let's go ahead and try to do that, so if we go ahead and try to type your mark and then we type here, let's say two dollars, it will print the second field, okay, and we go to the word term, you know, and we try to run our script and it says enter the file name, we write here a WK dot txt file press enter and you can see it has basically been printed okay so basically you have a search for windows the line that contains windows and from that line it has basically been printed. only this one is fine so if you want to print the fourth field then shine your dollar so it's fine so we go ahead in the program and write here instead of the polar two we can write here in another four if we save it come on . back and we try to run our script, enter finally, which is a WK dot txt file, press enter and you can see it is printing the number, okay, feel the fourth field that we have here, which is this one, okay and it is displayed for all the windows, there's another thing you can do and that's basically you can go here, let me go into the file, okay, I think it's already open, so you can print two fields or three fields simultaneously, so for example, If I want to print field one or field three, let me change it so we say the X, okay, so if I save this and go ahead and try to run my square in a WK dot txt file, you can see that.
It's basically printing clinics, the next two thousand, the next two thousand, the next two thousand, this is how you basically pull, this is how you're going to work with the aw create command, so we've seen some basic uses of it .maybe we will create an advanced course on this in the future. Basically you can create separate files for a WK in which you can write it using start and end clauses and you can also go to the terminal and you can basically check the man pages of a WK for more instructions so I think that's it of this topic, so now let's move on to the next topic which is the introduction to the said suite, which basically means a sequence editor and you can use it to manipulate text files and also for substitution purposes. the idea is that it can be used in many different ways with many complex files.
We are going to see a very basic use in this course. Well, let's go ahead and try to let me close this file and let's go to our script and see how we can run it, so what we're going to do is keep the program with the script the same, let's make a little change here and the name of the file, let's say replace , let's say we try to replace first, so enter the file name to print or enter the file name to replace using said ok, so now what we're going to do is first of all, let's go ahead and see what's in our file, so let me. rename this file, said file okay, so we press enter and we go into this file and you can see that this file contains all those things, okay, so what we can do is go back, let me close this file like okay, so we can come back here and we can start making changes using pipes, so let's say we write here cat and then we write here the file name, which is essentially set file dot txt and then we write here a pipe sign, okay and then after that we write here set, which is a set command that we are going to use and then we would put where and then we put double quotes and then we write here four bars four bars four bars and obviously then we will write here at the beginning we will write here s that represent the substitution and then we will write here, let's say if we go back to this file, let's say we want to replace I and we want to replace it with capital i, okay here like Okay, so what we're going to do is we're going to write here inside the little script because this is the one that already exists and then we will write here in capital letters.
That's okay because this is what we want to replace. Ok, we'll type here control, control, save it and we'll go back and try to run our script and see what happens, so it says intro filename to substitute using set, so you're right. here it says file dot txt and we press enter and basically you can see it has changed. I'm actually going to reduce the file size so you can see this very clearly, so I'll check and save. I'm going to go back and run my script again so you can see the change very clearly, so we type here and put the file name that says file dot txt, we hit enter and you can see basically our program has worked, our script worked, so yeah we go back to our script we wanted to replace the lowercase letter i with the uppercase I, so what happened is this lowercase letter I, the first one, was replaced by uppercase i, okay, but you can see. the second and the third are the same so to do that if you want to replace all the eyes what you have to do is go back to your script and you just have to write G here Ok, which means global, another thing you'll notice is you know what to use pipe is just a convention so even if you don't use it it will still work fine so I normally don't use it but it's just a convention so let's write here the name of the file that is being archived fine and save it and then what we're going to do is go back and run it, so now notice that we've added G which stands for global and now it's going to replace all the eyes in the file with the capital I, so we run our script and we name it of the file it says file dot txt and you can see that it has replaced all the eyes with the capital I.
I hope you got the point, so what you can do now is if you notice that these are the changes that have not been made within the file. Well, if you open the file, you can see that all of these letters are small, so this change. This has not been done inside the file, but it's just an output, okay, so what you can do is you can also make changes to the file, you can modify the file, but what I normally do is when you know what You know. When I'm working with complex files or even when you're working with complex files, what you have to do is it's good that if you create the backup, what you can do is just go to your script hello script dot SH and just type here any other file name create a new file new dot txt file then what is going to happen?
Basically, it will copy this into that new file if I run my script again and if I write here. side of the CID dot txt file and you will see that now it has not given any result, what you have done, you have created a new file and in this file we have redirected the result and this file contains the result, so now you can say what if I want modify the file that is already there so you can do it in two fundamental ways: you can redirect the fire, you know, rewrite this directly into the file as well or what you can do is go here and use a flag and that would be negative, so I make sure that when you do this negative, you really want to do it because it will modify your file toforever and there's no turning back, you can't undo it, okay?
So after doing it, what? you, what you're going to have, you're going to have that new thing that you're trying to have, okay, so let's go ahead and try to save it and let's try to run it and see what happens, then it says enter file in substitute using said, so we enter here the name of the file, which is essentially said dot txt file, we press enter and you will see that it has not given any results, which happened if we go back to that file and you will be able to see that changes have been made. okay in that file too so there's another thing you can do and that's basically you can replace whole words too so now what we've done we've replaced you know I think we've replaced just a single alphabet so you can replace whole words still, let's say we're not going to replace Linux with, let's say, your axe, okay, so if we save it and if we go back and try to run our script, let's see what happens, so enter the name of the files. we'll write your dot txt config file, press Enter and if we now go back to our config file, you can see that you haven't replaced it because the eye is capitalized, so make sure you take care of the case sensitivity or use whatever flag that really care about it and take care of the sensitivity too, so here Linux is fine this way, so we save it, we go ahead, we try to run our spirit, the name of the file that is set file dot txt, we press enter and if we reload it I can see that Linux has been replaced by Unix, okay you can replace the entire strings, so let's say I want to replace 2000 2000 and you want to replace it with 8000, let's say we save it, we try to run our script again, we set the dot txt file and now , if we go to reload our configured file, you can see that 8000 has been replaced by sorry, 2000 has been replaced by 8000, okay, so I hope you got the point, you have learned this, let's go for more things that you can learn . manage, you can see what those things are, what are other things that you can do with said also, okay, now let's move on to the next topic, which is essentially debugging a bash script, so the idea is that Bash offers extensive debugging functionality , so you can actually record your bash script and you know that if something doesn't go according to plan, then you can take a look at it and you know this is what's happening, so right now we have this hello script.
So let's say we intentionally create some kind of error like that, so if we remove the space, everyone knows it's obviously going to give some kind of error, so we run our script and give it a name, let's say set file dot txt. that was the program that we were running before, so you can see that it has given an error, so now it's pretty clear that the error is that we know that the error is in the if block right on line six, but in some of the Cases where you have long scripts like one thousand lines or two thousand and nine scripts, most of the time it is difficult to identify if there are multiple errors.
So for that, what you can do is debug your script and know what it is. happening in each and every step, okay, so there are basically three fundamental ways that you can debug your script, so the first way is to basically run your script this way, in fact, before of writing su before running your script, let me delete it first before. When you run your script, you type bash here, then type negative X here, and then type the script name here. Okay, now what this negative Bash X is going to do is debug your script, so we hit Enter and you can see that basically.
It is showing step by step what are the steps that are being executed, so the first step that has been executed is this, the echo command and then the step that has been executed is the read command. Well, now we would write the name here. which is essentially setting up the dot txt file so that you can see that it shows that this line has been executed and this line has been executed fine, so it basically shows you step by step which lines are being executed, so let's say if we had a For the loop here then it would show like the iterations of the for loop, like if this is the first iteration it would show the number and then it would show the code that was executed inside the for loop and then it would show the second one. iteration with number two and then it will show the same code which is running correctly, this is how it shows you the procedure step by step, so there is another way to do the same.
Basically you can put a negative and you can see that it's giving us the same results and telling us which lines are running fine, so there's a third and final way where you can basically select from which point to which point you want your script to run, like this So, for example, let's say I want my script. to start debugging here and I want my strip to say stop debugging here so the idea is that from the point where you are starting you have to put negative set X there and at the specific point where you want to stop you have to put set plus , Alright.
I hope you got the point, this is how you basically debug your script from one point to another, so let's go ahead and save it and see what happens now, so if we run our script. This way, you can see that you have the first two lines of debug and let's see what happens next, so if we enter the name of the file that is set to file dot txt, we press Enter and you can see that it basically hasn't shown anything. so it has displayed plus that you have understood the point. this is how you basically debug a bash script, so these are the three fundamental different ways to debug a script.
You can also continue and now you can also try these deep errors in other programs, other scripts and for example in the case of while and for loops, it would work the same, so I hope you learned something new in this course. I hope you enjoyed it a lot. If you want us to create another course on a similar topic or some other topic, let us know. comments below if you have any questions about this video, if you want to add anything in this video then also comment and let us know if you liked the video, please approve the articles written, you can visit our website and xn dot- com and until next time I will

If you have any copyright issue, please Contact