212 Bash Scripting Examples

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to the linuxin.com youtube channel in this video we are going to learn about bash which is a unique shell and command language written by brian fox for the gnu operating systems the name is acronym for the born-again shell upon on stephen pond the author of the direct ancestor of the current unique shell sh and we are going to try around 200 plus most important and most used patch examples right away and after watching this around 5 hours video you will definitely have a solid grip and command over bash so without wasting a single unit of time let's get started with the examples all right so the first script that we're gonna do is gonna be the simple hello world script so first of all in order to write a bad script what you have to do is that you have to have a file with an extension of dot sh which means that it has to represent a shell format okay so what we're going to do is that first of all you're going to open up your terminal and what we're going to do is that we're going to create a text file using our terminal so first of all you write your cd and you write here desktop because we will go to on our desktop and here we are going to create that one script file okay so here i would write here touch and let's write here first script dot sh okay so when we write that here you can see that we have first script dot sh file so if i click on it if i open it up you can see that this is an empty file so first of all in order to create it as a bash script you have to write here this thing hash and then exclamation mark and then you have to write here slash pin slash bash okay so whenever the terminal or the interpreter is gonna come and it's gonna read the file it's going to identify this file using this one simple line all right so now you hit enter you come to the next line and here simple as that you can write here echo and then you're going to write here double quotes and here you're going to write here hello world okay so it's pretty obvious that echo is being a print command here and when you will run your bash script it would basically print it out right so what i'm going to do is that i'm going to save it and if i go back here and if i try to run this script right now it won't run okay and why it won't run because if i do something like this ls hyphen l and if i hit enter here you can see that the permissions of this first script dot sh it's read and write read and read okay there is no form of executable permissions for this file and on top of that if you see here the color of this file here written is white right so the executable files they would have a green color something like this okay so now what we need to do is that we need to change the permissions of this file and how do we do that so we'll just write here chmod plus x and then we'd write here the name of a name of the first script dot sh and we hit enter you can see that it has not given us any errors which mean that it is it has worked and now the first script dot sh file has actually become executable so how can i double check it i would just write here ls and then space hyphen l i hit enter and you can see that executable permissions have been added to all groups okay so this one is basically uh owners group this one is a group and this one is a public group okay so this one is owner permission this one is group permission and this one is public permission so now we what we can do is that we can just clear the screen of the terminal uh and what i can do is that i can run the script right now okay so what i would do i would just simply write here dot slash and then i would just write here first script dot sh and now if i hit enter you can see that it has printed out hello world successfully right so this was our first script which was simple hello world script okay so now let's move on to our next script which is essentially echo command without new line so how do we do that for that we'd use hyphen n option with echo okay so what we're gonna do is that we're gonna go ahead and we're gonna go to our uh script okay so this is our first script dot sh uh file which is our scripting file essentially so what we can do is that we can add here hyphen n option and then we just write here hello and we come to the next line here we also have an echo here we write here word all right we come to the next line and here we also have echo and here we use hyphen n option and here we write here no and in the fourth line we write here echo and then we write here new lines okay so the thing is that when we basically save it and we go to our terminal we try to run this okay so let me just clear it and i run my first script here you can see that it has not given us any new lines because of the hyphen n option so when i was writing hello uh echo hello and echo world it was supposed to be printed out in you know the next line all right but because of the echo hyphen n option it has not painted it on the next line rather it has printed them on the same line so let me show you that as well so for example if i remove this hyphen n from here and if i also remove this hyphen n from here and if i now save my script go ahead run it you can see that everything has been printed out in the separate line okay so that was the concept of no new lines uh with the usage of hyphen n in echo okay so now let's move on to our next script which is essentially string concatenation with echo so how do we do that so first of all what we would do would go to our script and let me just remove all of that that we have written here previously and now we're gonna write here echo and here let's say we write here one here let's say we write here two and here let's say we write here three so now you can see that these are multiple strings in one single line with the help of echo that we are trying to print out right so now if i save this simply i go right ahead to my terminal and let me just clear it i try to run my script and here you can see that we have printed out three strings successfully with the help of echo and here if you go here to my script you can see that i have written three separate strings in a single line so the other way to do that would be that you have to write separate echo commands but you don't need to do that as you can do that with one single echo command as well and here i can add multiple strings okay the number is not basically restricted to three you can have four strings as well you can have five strings as well it totally depends upon you okay so if i save it i go right ahead i try to run my script again you can see that we have one two three four five all right so that was our third script okay so now let's move over to our next script which is essentially string continuation character okay so for example uh if we go to our old script that we have we have here one two three four five string so if let's say that in case you have multiple strings and that cannot be fit on a single line then in that case what you can do is that you can put here a backslash which means that the string is basically continued right now so here by putting that backslash either uh you know if even if you write that the other strings on the next line it would consider it the part of the same line so if i save it ctrl s i go back to my terminal and if i try to run it you can see that still uh we are getting the same output with no difference so for example if i write here a 6 like this if i save it if i go back here you can see that it is printing out till six strings so if i remove this backslash for some reason if i save it if i go back you can see that it is giving me some kind of error because it has it is not recognizing the strings on the next line right so for that we have this string continuation character uh which is essentially backslash so if i save it again go back here try to run my script you can see that all six strings have been printed it out okay so now let's move on to our next script which is essentially echo with tab characters so if we go back to our script we have here uh one two three four five six six strings so let me just remove um all of them and let's only have three or four strings let's say and let's say that you just want to have a sing all of them inside single quotes right you don't want to have you know multiple codes for that so for that what we would do we'll just remove these double quotes and now here you can see that we have one two three four okay but the problem is that it's going to consider it as one string okay inside the single double quotes right so what we can do is that we can use here the tab character and also we need to use hyphen e flag to enable backslash interpretation okay so how do we do that so for that you just write here echo hyphen e and then you would just write here one then you'd write your backslash t uh and then for three you'd also write here backslash t and then for four you'd also write here backslash d okay so now if you save it uh you go back to your terminal and you try to run your script you can see that it has right written here one two three four okay all four strings have been printed out and also it has basically uh printed them out with the exception of the spacing here if you can see so that space is equivalent to one tab and how that has happened it has happened because of that backslash t character that we have put here okay hope you've got the point you have understood it so now let's move on to our next script okay so now let's move on to our next script which is echo with new line characters okay so for that what we would do would just simply go back to our script and here would keep this hyphen e flag okay so that's where we're gonna use backslash with n okay so when we use that here uh you would see that it would perform the similar functionality as we were doing it with the slash t okay slash t for the tab and slash n is for the new line all right so if we save it uh press ctrl s or click this save button and go back to your terminal try to run this here you can see that we have one two three four each on a separate line while previously uh we had it with the tab spacing uh while we were using uh slash t okay so for slash n uh it would take your string the next string to the next line uh for slash t it would create a space equal into a tab uh between the first uh and the last string okay so and in between you would have that slash character all right hope you've got the point okay so now let's move on to our next script which is essentially echo with hyphen x to display all the commands so how do we do that so we go back here first of all the quest the right question to ask would be that why do we need to display all the commands so the idea is that it would be useful for printing all commands executing in script and hence you can see the progress okay so that's why it is pretty useful so now let's go ahead in our script and let's try to write here let's say let me try to write everything here separately so for that let me just remove all of that we just write here echo1 then we write here echo we write here 2 then we write here echo and then we write here 3 basically okay so one thing that we need to do is that we need to write here minus x or hyphen x at the top of the script where we have uh slash with slash bash written okay so we just write here hyphen x and all of that would change for us the execution of this script and this script is going to display us the progress uh and all the commands written inside the script as well so we just write here ctrl s or we can hit the save button we go back to our terminal let me just clear it we try to run our script and here you can see that it is basically displaying you each and every command and the output of that okay so in this case the output and the commands both were simple so we've got directly to it and we've got the result but in when you're writing more complex commands maybe it would take a while and you can observe the progress okay so here it shows that echo1 that was the command that was first executed then we have echo 2 that was the command that was secondly executed and then we have echo 3 which was thoroughly executed okay so that was the whole progress and whole procedure and it has shown us that this was the step-by-step procedure that a command was executed okay so if you've got the point okay so now let's move on to our next script uh which is a script number eight and it's printing strings that contain a single course okay so let's go ahead and let's go to our script and here we'll use this echo let me just remove all of that and also let me just remove this hyphen x because we don't want to observe the progress of the commands so the only case where you want to observe the progress of the command you're just going to write your hyphen x okay like this so right here you can uh see that we have here echo so we write here welcome to joe's barbecue okay so here you can see that we have single quote uh within the string and that is basically enclosed in the double quotes so let's see if it works fine with the echo so we'll just save it and we go back here in the terminal we'll try to run our script and you can see that it says welcome to joe's barbecue so you know in echo in uh bash scripting the single quote within the double quotes works just fine okay hope you got the point okay so now let's move on to our next script which is script number nine and here you can see that it says printing strings that contain double quotes okay so first of all in the script number eight we have done that thing with the single quotes and it works just fine and now let's see if it works fine with the double quotes as well so for example here uh we write here let's say my favorite movie my favorite movie is let's write here double quotes and let's write here limitless okay so i'm not sure if it's gonna work same uh i think it would work so let's just go ahead and let's try it so we press here ctrl s we go back to our terminal we try to run it and you can see that it has printed out my favorite movie is limitless so now if i go back to my terminal and if i try to run it here you can see that it is printing out my favorite movie is limitless but it is not printing out double quotes because i put double quotes with limitless so how do we make sure that those double quotes appear in the output as well so for that what we can do is that we can use backslash and we can use the backslash like this so we're just writing here backslash here and then we write here a backslash here so it means that now it is going to print it out okay so we just save it we go back here we try to run our script and here you can see that it says my favorite movie is limitless and now it has also printed out the double quotes as well so for single quotes you don't need to do anything but for double quotes you need to put backslash twice once where the string is starting before uh the double quote okay and the second backslash you're gonna put here just before where the double quotes are ending okay so hope you've got the point okay so now let's move on to our next strip which is uh script number 10 and here we'll see that how to write a file from inside a script that is also possible so how do we do that we go to our script here and let's say that we have this echo echo my favorite movie is limitless so what if i want to print this out inside another file which means that i want to write this entire thing into another file so for that what i would do i would just write here something like this the angle bracket that you know focuses towards uh the echo statement and then in here uh i'm just going to write here the uh file name which could be anything so you know i just write here file dot text okay so if i save it and if i basically go ahead try to run my script first of all let me show you that how many files on our desktop we have we only have one file which is essentially first script dot sh so now if we run our script here and if we ls here you can see that we have two files one is file dot text that have been created and if i open that file dot text in the nano editor then you can see that we have my favorite movie is limitless so that's that's how we have created a file within a script so if you want to uh go ahead uh to your terminal or maybe if you want to go out of your terminal and if you really want to go to your desktop here and if you want to open that file from here in the g edit editor you can see that it has my favorite movie is limitless so that's how basically you write a file from within a script hope you've got the point okay so now let's move on to our next strip which is override to a file from inside a script so that is not difficult at all all you have to do is that you have to go back to your script and in order to overwrite what you can do is that you can basically write another echo command but here you would write it on the next line so what would happen is that whatever you would have in the next line or the last line that would be overwritten over the previously written line okay so for example uh the first line was my favorite movie is limitless so i could change it and i could have here that my favorite book is my favorite book is think and grow rich okay so what happens is that now if i save it i go back to my terminal and i try to run my script basically okay i hit enter and you can see that it has not given us any errors which means that our script was actually it was running successfully previously and now it has given us an output so the output is on our desktop where we have that file so we'll just go right ahead we'd open up our file.txt and here you can see that instead of having my favorite movie is limitless now we have my favorite book is think and grow rich so that's how you basically overwrite a line in the script using the script inside a file hope you've liked it okay so now let's move on to our next script which is essentially append to a file from inside the script so how do we do that so what we would do would just go right ahead in our script and now what we're going to do is that instead of overwriting we want to append so how do we append multiple lines over each other so for example if i want to append this line instead of overwriting it what i would do that i would just put here double angle brackets in the same direction facing towards the echo statement all right so if i want to add another line append another line i could do that so what i would do i would just simply copy this line i would have here and instead of like my favorite book now i could write here my favorite car and i could write here my favorite car is ranged over okay so actually that is an suv to be specific so i would just write here my favorite suv is range rover all right so now you can see that we have these three lines and uh these two lines instead of overriding the first line now it would basically append them so how do we know that it has appended successfully or not so you save it we go right ahead to our terminal and we try to run our strip it has given us no errors which mean that our script has run successfully uh in order to see the output would go back to our desktop where our file.text resides so here you can see that instead of overwriting now it has appended successfully so first line is my favorite movie is limitless my favorite book is think and grow rich my favorite suv suv is ranged over so these are the three lines that we have been added to this text file successfully hope you have got the point all right so now let's move on to single line comments this is our topic for next script so what i'm gonna do is that i'm gonna go to my program and or the script and i'm gonna comment every line uh individually so how do we do that so for example let's say that if i want to append this second line which is an echo statement which states my favorite book is think and grow rich and it gets appended into file.txt if i wanna comment it so how do i do that i would just go right ahead in the beginning of this line and i would just put this hash sign and you can see that the color of this line has changed to blue right so all i have to do is i have to save it so a comment is basically something that has no impact in your code or in your script so it is just there for you for usually the things that don't matter and you don't want to have them any impact on your code so usually the usual use of comments is for programmers who are writing it the code and they are basically writing the logic of the code so that when some other programmers see that code he could understand it quite easily so that is the basic purpose of the comments but if you want to keep some of the code and you might want to use that in the later periods of time you can basically simply comment it like we have done here right so comment has no impact so if i save it if i go back to my terminal if i try to run my script and if i go back to my desktop and i try to open that file now you would be able to see that now i have only two lines one my favorite movie is limitless and my favorite suv is ranged over and the middle line the echo statement for the book is nowhere to be found right so in the same way you can comment other lines as well so this is basically the topic of the single line comments hope you liked it okay so now let's move on to our next script topic which is essentially comments from middle of the line so that is very interesting at it it is possible so for example uh if you want to comment your script from the middle of the line you can also do it so if we go back to our script here which is essentially first script dot sh and let's say that instead of like a comment from the beginning i want to comment this line right from here so now what would happen that instead of appending it to the file uh it would just echo that statement into your terminal okay because i have commented the rest of the line and the rest of the line when i say i mean that the part where it would append that into the file right so the other two statements the other two lines would still be appended in the file okay so now if i basically save it i go right ahead to my terminal and i try to run my script here you can see that in the terminal it got printed out right the echo statement that my favorite book is think and go rich and if i go back to the file the file.text those two lines would still be appended in there okay so if i open it up there you can see that those two lines are here so if i want to comment the third line i can also do that so let's say that i want to save this line to the file or if i want to append this line to the file and the other the third line i don't want to append it and i want to basically print it out in the terminal so i can just simply comment this and now instead of bending the third line it would append the second line and third line will be printed out in the terminal so if i go ahead save it go back to my terminal try to run it here you can see that now it is printing out my favorite suv is range server and the top two lines have been uh they are in the other file which is this hdfi dot text so if i nano it here you can see that we have my favorite movie is limitless and my favorite book is think and grow rich hope you've got the point okay so now let's move on to our next script which is multi-line comments using hair docs so multi-line comments in which you can previously we have seen that how you can have one line comment and we have also seen that how you can comment the line from the middle now let's see that how we can actually comment multiple lines at once so what you can do is that you can go to your script where you have written it so now it basically comment all of that using uh the hair docs okay so let's say that if i want to comment these two lines what i can do is that i can have a hair dog here so let's name this here dog as let's call it linux hint message and you can name it by the way whatever you want okay so i would call it that and then let's say i want to comment these two lines so what i can do is that i can basically end it right here so i would just write here linux sent and it would understand that here that comment thing has ended okay so whatever i would write inside here that would be considered as a comment and you know these could be like multiple line comments okay so this is a comment all right so hope you have got the point so this is how you use here talk and you can comment down multiple lines and now if i want to test it i would just save it i would just go right ahead to my terminal i would try to run my script you can see that there are no errors and if i try to open that file that was on my desktop right so i can do that so i'll just write here nanofile.txt and here you can see that we have my favorite suv is range over because we are basically appending it so if i don't append it and i want to overwrite it so now you would see that this thing would change so let me just go back i try to run my script again and i go i nano it and here you can see that when i go inside the file we have only one line here and all the other lines have been commented successfully hope you've got the point okay so now let's move on to our next topic of the script which is essentially simple variable usage so how do we do that so for that what we would have is that we would go to our script where we have written it uh let me just remove all of that because we are just going to create a new variable so in order to create a new variable in batch all you have to do is that you have to write a variable name then you put an equal sign here and then inside the double quotes you write the value if your value is the string and or if it is an integer you write it without the double quotes so the idea is that the variable is gonna automatically detect the data type of your data that you're providing so for example here i have the variable named as bar and inside of that i'm just gonna give it a string like hello world and then i go to the next line and then i echo it and then we have here variable var okay so now what would happen is that basically you if you take a look here i am printing out or i am acquiring the variable here essentially which is var but if you take a look at the above line we have hello world saved inside the variable which means that equal sign here defines that hello world is being assigned to the var variable so vr variable represents that specific string that i'm giving it to okay so when i would echo var it would basically print out the value that it has stored inside so let's go ahead and let's check it out so i save it i go to my terminal and here let me just clear it and i try to run my script so when i run it you can see that it has printed out hello world but if you take a look at the script i have not directly printed out hello world instead i have saved it in a simple variable and i have used that variable to print it out hope you got the point okay so now let's move on to our next script which is essentially multi-word variables combinations with code so what we're going to have is that we're going to have variables that are going to have multiple uh multiple strings or multiple words in it or one string excuse me and what we're gonna do is that we're gonna combine those two strings which would have multiple words inside each string okay so that's going to be fun and that's going to be interesting right so what we're going to do is that we're going to go back to our script uh let me just remove all of that so let's say that i have here a variable named as a and inside variable i have this is a car okay so this is one string then we have uh another string which says b and it says this is a mercedes okay so you know we have two strings which have multiple variables uh multiple words excuse me um and now if we want to combine them what we can do is that let's say we have another variable named as c and inside c we want to combine these two variables so we'll just write here inside the double quotes we'll have a dollar sign and in here in the curvy braces we have a and then uh we'll just write down here uh dollar sign and in the curly braces we'll just write b so now basically what we have done is that we have combined a and b inside the double quotes within c so everything that we have in a and b that would be combined and store in the variable c all right so we how do we know that we'll just go right ahead and we'll just basically echo the variable c here okay so now if we uh basically save it we go right ahead to our terminal we try to run our script here you can see that we have this is a car this is a mercedes so you can see that we have both lines in a single line concatenated or combined and we have it as a result so you can see that i am acting here only the c variable and if you go one line up here you are able to see that inside the c we have two variables a and b and if you go one line up more you'd see that we have a and b variable inside a we have this is a bar and inside b we have this is a mercedes so if we would have you know acquired them separately it would have given these two lines on separate lines but here you can see that we have combined them inside the c variable and we have acquired c variables successfully and when we have run the script we have got the result this is a car this is a mercedes hope you've got the point okay so now let's move on to our next topic of the script which is home variable so let's talk about this variable so uh in the script we are going to show you that how you can use this home variable by translating it so home variable is a built-in variable that stores the credential of your system and we're going to write a script um we're going to basically echo it and then also we're going to ls it because home variable has a specific part uh which would we would have missed it here okay so let's go ahead let's you know remove this entire script that we have here and now uh we're gonna have here a variable let's call it variable path and we write here an equal sign and here we have this home variable with a dollar sign okay so whatever the home variable has inside of it it's going to be stored into the variable called as variable path so now what i can do is that i can echo this variable path variable as well so i write here a dollar sign then i write here variable path all right so now once i have done this what i can do is that i can go to the next line and i can also ls it so by ls i mean that i'm going to ls this home variable so home variable actually has some kind of path right so i'll just write here variable path i'll basically save it i go back to my terminal and now i run my script and here you can see that the home variable it has slash home slash linux in which is essentially my username so if you know we go to ls if we do ls to this home variable which means if we do ls with slash home slash 10 accent these are the results that we would get these are some of the folders that i have in my home directory uh you know desktop downloads music public videos documents uh pictures etc okay so these are the uh you know folders that we have here so that is the usage of home variable so if i just remove this ls down here or let's say if i comment it what would happen so it would only print out the path so you can see that it has printed out the path which is essentially slash home slash linux hint okay so if i uncomment it it would also ls or and list everything that we have on that path right like this okay hope you have got the point okay so now let's move on to our next script which is essentially user variable so we're gonna talk about user really variable and we are going to translate it okay so for that what we would do would just go back to the script and we do the same thing here so instead of having a variable path what we can do is that we can call it a simple variable and inside of that we can have that user variable so with a dollar sign sign i would write here user and then uh inside the echo here i would just write here dollar sign and then i would write here var okay so yeah that is it so if we save it we go back to our terminal and we try to run our script you can see that the user variable prints out linuxent which is basically the username okay so this user is a built-in variable which would have stored inside of it the user name okay so that we were able to print it out hope you've got the point okay so now let's move on to our next topic which is essentially host name variable so the idea is that hostname variable is going to be the same as the user variable so let's go ahead and let's give it a go so what we're going to do is that instead of having here the user we just have here host name would write here it gets stored into the variable var and then we basically echo it so when we'll echo it will basically save it we go back to our terminal we try to run our script and you can see that it is giving us the same result as linux sent okay so the host is also linux and and the user is also linux hint in my case is but in your case it could be different okay hope you've got the point all right so now let's move on to our next script which is essentially echo home variable with escape to avoid expansion so the idea is that this home variable if i go back to my script if i write here home variable what's going to happen is that it is going to print out the value which is inside of that variable and which is essentially slash home slash 10 extent which you could see on your screen right now so the idea is that what if i want to print it out as a literal what if i want to print out that dollar home as it is so for that what i have to do is that i would use this backslash escaping which would help me to avoid this expansion of this home variable okay so what i would do i would basically save my script i go to my terminal and i try to run the script and here you can see that the literal home variable uh is there as it is okay so this backslash escaping is used to escape embedded uh double quotes and dollar signs or instances like that you know those instances uh which have to be treated as literals okay so that's where this backward uh slash escaping helps us okay hope you've got the point okay so now let's move on to our next topic which is topic number 22 writing to file in home directory using home environment variable so the idea is that what we're going to do is that we're going to use this home variable which actually have the value or the address of the home directory so we're gonna go to that directory we're gonna create a file virtually using a script and then when we'll create that file we write some content into the file and finally we display it using the cat command okay so let's go ahead let's go into the script and let's see how do we do that so first of all what i'm going to do is that i'm going to access the home directory using cd and then i'm going to use the home variable instead of the home path okay so this home variable contains the value something like slash home slash user so whatever your username is now once we have accessed the home variable now what i want to do is that i want to create a file here which would basically do nothing but it would have something in value in it let's say i write content here like i like range rover okay so this is something a simple line that i have written and let's say that i write it in a file which i call test dot text okay so this is a file that i want to create here in the home directory and once i would create that i want to make sure that if that content i like ranged over has been successfully written in the test.text file or not so for that i would just write here cat command and then i would write here cat and then i write here the file name which is essentially test dot text so now i'll just save it i'll go right ahead into the terminal first of all let me show you the contents of the home directory you can see that we don't have any test dot text file here right now so what we can do is that we can run our script here by the the name of our script is first strip dot sh so we hit enter and you can see that first of all it took it to the home directory then it re it has written this i like range rover inside this test.txt file and then finally it has printed it out here you can see that i like range rover so now if i ls the home variable you can see that we have test.txt file here finally which wasn't here previously okay so i hope you have got the point so now if you want to manually check the test dot text you can also get here oh excuse me first of all what you have to do is that you have to go to the home directory first so once you are there now you need to do this cat dog test.text and you can see that we have i like ranged over here hope you have liked it hope you have understood this better okay so now let's move on to our next topic which is essentially simple touch command so the idea is that touch component is used to create simple text files so what that's what we're gonna do in here so we'll go back into our script and in here we're gonna write here touch which is a command that basically creates a text file so we're going to call it file touch dot text okay so once we have created that we want to ls so that we could show that this file has been created successfully so we save it we go back to our terminal let me just clear it so first of all i'm going to ls here we are in the home directory right now we need to go to our desktop directory because that's where our script resides and until unless you're not in the exact directory you have to you know write down the entire path towards the script so in order to avoid that will just be directly in the directory where the script resides so now what we're going to do is that we're going to run our script but before that let me show you if i ls here you can see that we have three files here one is the text file okay file.txt other one is the script itself and the third one is the dot odt equivalent to the word file that we have of the libreoffice writer okay so right now we don't have any uh touch file here as you can see so what we're gonna do is that we're gonna run our script which is essentially first script dot sh so if i hit enter here now you can see that file touch dot txt has been created with the help of this running uh this first script dot sh so if right now ls again you can see that we have file touch dot text and we had used this ls inside the script as well in the similar way you can create as many files using the touch command as you want okay that's that's not a problem so that was fairly simple i hope you have understand it let's move on to our next topic which is essentially create a symbol link right to that linked file and cat that link file so the idea is that what we're gonna do is that we are going to create a file and we are going to create a link to that file and that link basically is nothing but a copy of that file okay so once we have made the copy of that file then we are going to basically get the file the link basically which would essentially get the file so whatever the contents of the file are they're going to be shown using the link file okay so first of all on our desktop that's where we are so if we ls here we have file.txt so let's say we want to create a link to that so first of all let me see that what we have inside the cat file.txt so we have this my favorite suv is ranged over okay so that is file.txt so what we're gonna do is that we're gonna create a link of that in our script so first of all we'll go to our script and in here we're gonna write uh a command ln which would basically create a link then we're gonna save it using the hyphen s flag then we're gonna write the file name which is essentially file.txt and then we're gonna write here the link name okay so let's call it link one so now once the link has been created we wanna alas it will show you that uh if the link has been created or not so once we have done that so now what i want to do is that i want to add something to this file so what i would do i would write here echo so i'll write here now i am writing inside the link one so remember that link one is nothing but the copy of file.txt and uh what we're gonna do is that we're gonna go ahead and we're gonna basically append it inside the link one excuse me so once we have done that so now what i'm gonna do is that i'm gonna basically cut the link one so that is it first we have created the link of the file.text then we have lsa to show you if the link has been created in the directory or not so once we have done that we have added one line to the link one which means that we have added one line to file.text essentially and then we have basically got it using the cat command so which means that it would display the contents of the link one so if we save it so after saving it go back to your terminal try to run your script here so first of all let me ls here and show you that what we have here so now uh i would basically run my script which is essentially first script dot sh so if i hit enter here you can see that we have you know my favorite suv is ranged over and now i'm writing in the link one so this is the copy of the file one essentially link one and in the ls here you can see that previously link one was not here okay but now link one exists here which means that we have successfully created the link one which is essentially the copy of file.txt so when we added something uh now i'm writing inside the link one essentially i have added or appended this line inside the file dot text and when we access the link one it we essentially access the file.text as well i hope you've got the point you have understand it that was fairly simple all right guys so now let's move on to our next script which is essentially chmod command and we're gonna see specifically that how we can make the file executable using this chmod command right so for that what we're going to do is that we're going to go back to our script and before we go back to our script let me just go ahead to our terminal let me just ls here you can see that we have these files in here so let me just clear it what i do here that i create another file named as let's call it script one dot sh okay so now if i ls here you can see that first script dot sh this thing is executable however if you take a look at this script one dot test set this is not right now executable okay because of the colors so if you want to check in detail you can also do that ls hyphen al so if you go here uh if you take a look at this script dot shn if you look at the permissions we have read write executable permissions okay so for all of that we have executable permissions and that's why the color of the script is also green and if you go here script what dot sh and if you take a look at the permissions here we only have read and write permission to the owner the groups have only read permission and the public have also only the read permission all right so what we want to do is that we want to add the permissions the executable permission to this script what dot sh using i repeat using the chmod command all right so what we're going to do is that we're going to go to our script here and in here we are going to write a piece of code a small piece of code for a simple command chmod then we're going to write here plus x because we want to add the executable permissions so once we have done that now what we're going to do is that we are going to write here script one dot sh which is essentially the name of this script so now if i basically save it we go back we try to run our script and we hit enter you can see that it has not given us any errors which mean that our command has been successfully executed and now this script one dot sh has been become executable so we can check it by writing here ls hyphen al if we hit enter you can see that now if we observe the script one dot sh right here we have added executable permission to it successfully okay so hope you have got the point all right so now let's move on to our next script in which we're gonna use chmod command to make the file not writeable and then we're gonna try to write in it all right so for that what we would do that would go back to our script here and in here we're gonna write something like we're gonna write here chmod hyphen w and then we're gonna write right here the file name on which we want to apply this and we want to make it non-writeable so for that what we would do uh would go to our terminal would change the directory to the desktop and in here on my desktop i'm just going to clear it first and now i'm going to alice here so you can see that we have um you know plenty of files here and let's say that this is file dot text right so if i go ahead and if i open it in nano let's say file dot text right so you can see that it has pretty much writable permissions and if i like save this right here i'd be able to save it you can see that h a has been saved in the file so now what i'm gonna do is that i'm gonna take away the right permissions of this file.txt so if i do this thing right here you can see that this file.text it has the right permissions for the owner w here right so we're gonna do what we're gonna do is that we're gonna go to our script and in here we're gonna type here the file name which is essentially file.txt all right so we save this thing here we go back to our terminal and we try to run our script which is essentially first script dot sh all right so we hit run and you can see that it has not given us any errors which mean that the script has run successfully so we'll just clear it and now here we're gonna write here ls hyphen al we hit enter and you can see that this is file.txt and now we cannot see the right permissions here all right so now let's try to narrow it let's try to open this thing and here you can see that we have uh h a and here you can see that we have file dot text is unwriteable right so it is unwritable and let's say if i try to write something you know it is being written here but when i try to save it let's say you can see that it says error writing file.txt permission denied all right so if i come out of here yes i want to modify it you can see that the permission has been denied all right so let's say that if i go back again in the file you can see that there are no extra characters that i i had added previously so that's how you make the file unwritable and that's how we're even if we are trying to write something in it we can't all right so now we're gonna move on to our next script which is ch own file okay so in in this script what we're gonna do is that we're gonna change owner of the file from one user to another and before and after we're gonna run an ls command all right so let's go ahead to our terminal and in here we're gonna run here ls so here you can see that we have uh the users here user and the group here so you can see that for all the files we have the user as the linux sent and the group has the linux and as well so what if i want to change some file uh to from from the linux into another user okay so how do we do that so let's say that i pick up this file file.text and i want to change the owner of this file so for that i would write a simple script i'll go to my bash script and in here instead of chmod i'm going to write here c-h-o-w-n which means change owner and here i'm going to write here the user which is the user that we have created if you don't have a user or if you want to create a new user you can create it by simply writing here sudo add user and then here the user name which is essentially bob so i have already added it so let's go ahead and let's try that so we'll just right here chmod bob and then we're gonna write here the file name which is essentially file.txt all right so we want to assign this file.txt to bob instead of linux hint so let's see what happens so we save it we go back to our terminal and we try to run this piece of code so we just write here you know dot slash and then we write here first script dot sh we hit enter and you can see that it says changing ownership of file dot text operation not permitted so what we're gonna do is that we're gonna have to write here sudo to give it the super permissions all right so we just save it we go back we try to run our script and you can see that it has not given us any errors all right so what we're gonna do is that we're just gonna simply clear it and we here we're gonna do ls again we hit enter and you can see that file.txt has been changed all right and the owner of the file load text is now not linux in but it is bob all right so let me show you quickly that how you can create a user if you don't have that right sudo add user and then you quickly write here any name so let's say i write here kali hit enter and you can see it is going to ask you for a password put in your password then you can write here the name okay so i just i'm just putting random names you can put in here phone numbers then it is gonna confirm if the information is correct press y that's simple that's how simple it is to add a user all right hope you've got the point so now let's move on to our next script which is using underscore variable touch file and then we're going to use underscore variable to append to the file so the idea is that first of all we're going to create a variable with underscore then we're going to create a new file using touch command then we're going to use that underscore variable that we recently created to append to the file that we have created using touch so let's go ahead and let's try to do it so first of all i would go to our script let me just remove this line here we're gonna write here the underscore variable so we'll just write here u and d underscore and then we're gonna write here var uh then we're gonna write here equal sign and let's write something in it we could write here anything so let's say we write here hello linuxcent uh audience right so now we come to the next line and in here what we're gonna do is that we're gonna create a new file so let's call it touch new dot text all right so once we have created a new file what we're going to do is that we're going to append that under variable underscore variable to this new dot text so for that what you would do is that that you would write your echo and then you're going to write here underscore variable all right and then you're going to write here these two brackets to append to it then you write your new dot text all right and then obviously you want to see if the file has that underscore variable content or the value uh obviously we want to check if that has been appended to it or not so for that would use the cat command we'll write him cat new dot text so that we don't have to go and manually open the file to check we can basically check it right away so we'll write here cat new dot text so we save this you know everything is done it's pretty simple first we have defined a variable underscore variable then we have created a touch command which would basically essentially create this new.txt file on our desktop because that is our current directory where our script resides and that's where it's going to run and then you can see that we are appending that underscore variable to the new dot text okay whatever is in it so for example if we remove this one angle bracket what's going to happen that every time you run the script it deletes the previous data and then it basically overrides the new data but when you have your double brackets what's going to happen that it keeps the previous data and it appends to it the next data all right so if we save it we go back to our terminal and we try to run our script with the first script.sh you can see that we have hello linux audience so if we ls here you can see that we also have the new dot text file which we didn't have previously all right so now if we do that once more if we try to run our script once more you can see that instead of having hello linux and audience once we have it twice if we run it one more time you can see we have three times because it is essentially appending the data so if we remove this one angle bracket we save it we go back we try to run it you can see that we have hello linux audience once only because now it's not appending it's basically adding the data and it would basically overwrite it so the basic job was appending so we'll just keep these angle brackets double and if we run it once more you can see that it's gonna keep appending all right hope you've got the point all right so now let's move on to our next script in which we're gonna use the let command all right so we use this let command in order to store value to the variable and then we're gonna perform the arithmetic operation of addition all right so let's go ahead to our script and let's try to write a script so let me just remove all of that previous code and now we're going to write here let a equals three all right so that basically allows you to define a variable so we could do that in separate lines or we could do that in the same line as well doesn't make a difference so we'll just write here b equals let's say 7 and then let's say we write here c equals a plus b all right so now it's done now what we're going to do is that we're going to write here echo and then let's say that we print out here c in fact we print out a we print out b as well all right so now if we save it we go back to our terminal we try to run this and here you can see that we have performed arithmetic operation using left successfully and here if you compare here um a is three right b is seven absolutely correct and then c is a plus b right which means seven plus 3 which is essentially equals to 10 all right hope you've got the point all right so now let's move on to our next script which is basically how to add subtract multiply and divide so we're going to basically use these for operations arithmetic operations on two variables all right so for that we'd go to our script let me just clear this let script that we have written here all right so now what i'm going to do is that i'm going to basically initialize two variables so let's call it number one and let's say its value is 10 and let's have number two and let's say its value is two all right so we have two variables and now we are going to perform addition subtraction multiplication and division using expr okay so we just write here expr which is a keyword then we're going to write here the variable name which is essentially one number one and then we're gonna use the other variable which is essentially number two all right so let's just test it first if it works then we move forward and then we do the other stuff as well so here you can see that it is working edition thing is working perfectly so let's go ahead and let's do um other operations as well so i'll just simply copy it four times i'll just change the signs here so let's say for subtraction we have minus for multiplication we have ester it and for division we have this sign all right backslash so we save it we go back here we run our script again you can see that it has added it right number one plus number two which means 10 plus 2 which is 12 then we have number 1 minus number 2 which is 10 minus 2 which is 8 right and then we have multiplication and you can see that it is giving some sort of syntax error that will basically address in a moment and here you can see that if we divide 10 by 2 you know the result would be 5 all right so how to get rid of the rate of this syntax error so for that you would just come here and expr is basically not recognizing this steric so in order to recognize this what we have to do we have to put here a forward slash so after putting a slash here right what you have to do is that you have to save it you go right here and then you basically run your script and here you can see that we have got the result if we multiply 10 by 2 you know 10 multiply by 2 it would be 20 all right so that's how you perform for operation addition subtraction multiplication and division hope you've got the point all right so now let's move on to our next script which is essentially increment a variable three times with using increment operator all right so what we're gonna do is that first of all we're gonna have a variable in our script that we're gonna increment all right so let's just remove this whole script and let's have a variable here called as var and let's give it a value one to keep track of you know increments all right so here basically first of all what i'm going to do is that i'm going to print out the variable simply that what value this variable has all right so along the way as we increment it i'm also going to print it out each increment so that you could see that how we are incrementing it all right so in order to increment it what you have to do is that you have to write here var the variable name essentially then you write here equal sign and then you write here dollar sign you write here two brackets all right and inside these brackets you're gonna write here var plus equals one which is basically essentially a an increment sign all right so once you do that obviously we need to acquire here again because the value the variable has been incremented all right so now we're gonna have a double quotes we're gonna write here var and um after doing that we come here again we basically uh do the similar kind of thing so i'm just gonna copy it so now this has this thing has been incremented once now it has been implemented twice now it has been incremented three times all right and each time we'll be able to see the result because we are acting the variable all right so we save it we go to our terminal and we try to run our script here you can see that you know the variable was one okay so this is basically without incrementing it all right so the next thing is that we have incremented it by 1 and you can see the value has become 2 here then we have incremented it by 1 and you can see that the value has become 3 and here you can see that we have echo variable again and you can see that now we have incremented it once more and it has become 4 right so that's how you basically increment in that script hope you've got the point all right so now we're going to move on to our next script which says that decrement are variable three times with decrement operator so previously what we have done is that we have incremented a variable three times now we're going to decrement it equipment increment means you know you go one value up decrement means that you come one value down so for example um let's say that this value or the variable it has a value five let's say so if we want to decrement it by one you know decrement means less one so we write here minus or negative or subtraction sign we write here as well and we write subtraction sign here as well so now basically you are pretty smart you know you do the math so here uh you know it would print out five because there is nothing happening between the initialization of the variable and the print statement or the echo and then you can see that it would be one less right so the the variable would become four then you can see one more or less the variable would become 3 and then one more or less it would become 2 right so it would be happening what we wanted in the first place which is essentially decrement using the decrement operator three times all right so just save it go back to your terminal and try to run your script and here you can see that initially the variable was five but now it is becoming one less it becomes four then it becomes three and then it becomes two whether [Music] while in the case of increment it was going up like from one to two from two to three and from three to four while in the case of you know decrement it is coming down from five to four four to three and three to two hope you've got the point all right so now let's move on to our next script which is essentially echo number modulus second number so we're gonna have two variables here and in our script of course and in our script after having two variables we're gonna basically take the modulus of the second number so let's uh see that how we can do that so we'll go to our script and in our script what we're gonna do is that i'm just gonna clear it okay so once i clear it what i'm gonna do is that i'm gonna write here number which is essentially a variable so let's call it number one equals four and let's call it number two equals two all right so now we have basically two variables so we're gonna have an echo statement here we're gonna have double quotes here we're gonna have a dollar sign here and then after having a dollar sign we're gonna have two brackets and inside these two big two two brackets i'm gonna write here uh the two variables number one is gonna be number one then we're gonna write here a percentage sign then we're gonna write here number two all right so once we do that now what we're gonna do is that we're gonna come out of here and we're gonna basically save this script we are going to go back to our terminal and here i'm around the script okay so i'll just basically write here first script dot sh i hit enter and you can see that the modulus of it is zero all right so if i change the number here if i go like five here if i save it and if i run the script you can see that now the modulus is one so for example if you divide you know four by two the result would the remainder would be zero right so if you divide five by two the remainder would be one yeah so that's how it works so if i write here you know six of course the remainder would be zero again uh you can see and if i run here right here seven you know the remainder would be one all right hope you've got the point that was pretty simple all right so now let's move on to our next script which is echo number two exponent of second number so we're gonna write a script where we'll basically take a square of the second number or the first number you could say all right so for example if we take the exponent of like you know eight right so that's gonna be like 64 the square of it so let me show you quickly that what i'm talking about so what you're gonna do is that you're gonna go to script and here what you're going to write is that you're going to write here a variable and then you write here equals and then you write here a dollar sign and inside here in the two brackets you're gonna write eight and then you're gonna write here static static two all right so after that what you're gonna do is that you're gonna simply echo the variable all right so let's see what results we get so after echoing it just basically save it go to your terminal try to run your script and here you can see that it's now 64. so what if you take the exponential of 9 so it's understood that it's going to be 81 right so that's how simple it is and that's how it works all right so now let's move on to our next script which is five mathematical expressions using expr so the idea is that there are five mathematical operations that can be performed using the expr function so let's go ahead to our script um and inside our script what we are going to do is that we're going to remove the old script and then we're going to write here two variable let's call it number one and let's keep it equal to 25 and then let's have number two and let's keep it equals to five so now what we're going to do is that we're going to perform five mathematical operations uh addition subtraction multiplication then we're gonna perform division and then we're gonna perform uh we're gonna take basically mod of these two numbers okay so let's go ahead and first uh operation would be expr then we'd write here dollar number one and then we'd write here basically plus and then we write here dollar sign and then you write here number two okay so these are the two variables that would be added like this all right so let's just copy it let's go ahead let's uh have here a minus sign okay and then let's have here a multiplication sign and we're gonna write here a forward slash and then we're gonna have here a steric then we go ahead on the next line and then we basically write here a division sign okay so we write here a division sign and then we go back ahead to the next line and here what we're going to do is that we're going to have the same copy paste and then we're going to have here a sign like this percentage sign okay so it this percentage sign would basically tell us about the remainder all right so let's just go ahead and let's just execute them so we save it so if we talk about this you know slash steric then what we have done is that we have done this thing because of multiplication because it won't multiply and it would give an error uh similarly to the example number 30 as we have done so now let's just say let's go ahead and let's run our script and see the results so in order to run our script with right here dot slash then the name of the script then dot as such we hit enter and of course let's try to add them so 25 plus 5 is 30 right 25 minus 5 is 20 25 multiplied by 5 it's 125 and if we divide 25 by 5 then it results into 5 and if we divide 25 by 5 the remainder would of course be zero all right so you can paste here any other numbers and of course you are going to get these results so that's how you perform mathematical operation using expr okay so now let's move on to our next topic which is five mathematical expression using parenthesis so previously we have done it with expr so now let's go ahead and let's do this so we'll go back to our script let's keep these numbers the same okay and then let's just remove this expr from here and let's write here echo let me just remove all of that and then we can basically copy paste the same pattern right so we write here echo and then we write here a dollar sign we have parenthesis here a double parenthesis and inside these parentheses what we're gonna do is that we're gonna write him number and then we're gonna write here plus and then we're gonna write here number two okay so now once you have done that come to the next line and do the same for everything with one exception that for multiplication there is no need to put that forward slash here okay so it would work without that forward slash so let's just go ahead and let's try changing these signs we write your percentage sign we write your division sign we write your multiplication sign and then we write here minus sign here okay so now you can see that clearly we have done the same thing pretty much and of course we'll get the same results until unless we change the variables so let's go ahead and let's try to change the variables from 5 to 25 to let's say 10 and 100 okay so let's save it and let's just go ahead and run our terminal or run our script in the dominant excuse me we hit enter and you can see that it's basically giving us some sort of errors and one thing that you might have noticed is that i forgot to write here number one okay so that is why it won't work because there is no variable named as num okay so there is only num1 so once you do that just basically save it just go right head to your terminal and try to run your script and there you would get the results so 100 plus 10 is 110 100 minus 10 is 90 100 multiplied by 10 is 1000 and 100 divided by 10 is 10 and of course if you divide 100 by 10 you get the remainder as zero so hope you've got the point so that's how you basically do five mathematical operations using double parenthesis okay so now let's move on to our next topic which is double parenthesis with dollar operators to assign to variable so the idea is that you can basically assign the variable to another variable using double parenthesis and you can make changes to it for example if you have a variable and if you want to add something to it and then basically store that entire value into another variable you could do that so let's go ahead and let's see how we can do that so what we would do would go to our script and in here what we're going to do let me just remove all of that let's have here one variable called num1 and let's have here another variable okay and what we're going to do is that we're going to basically use this num1 variable and we're going to add some value into it and then we're going to basically store it inside the var okay so we write here var equals we write here a dollar sign we write here double parenthesis and after writing here double parenthesis what we're going to do is that we're going to write here var excuse me num and then we're gonna write here plus and then we're gonna write here let's say 10 okay so now uh you can see here uh here we need to write another parenthesis okay a bracket and now we're gonna write here echo and then we're gonna write here var which would basically uh print out the value of var which is going to be essentially 110 because we are adding num1 to 10 and after adding we are storing the entire value into bar so now if we save it we go back we try to run our script so we hit enter and you can see that the result is 110 so that's how you basically assign to a variable using the double parenthesis with a dollar operator all right so now let's move on to our next topic which is topic number 38 in which we see that multiplication has precedence over addition regardless of the order okay so let's go ahead and let's try to do some example to prove this thing so what we do that we go back to our script and here we will write basically var which is essentially a variable name and then we're gonna write here a dollar sign and along with the dollar sign we'll have double parenthesis and in here we're gonna have three multiplied by let's say two plus one okay so when we do that we basically equate to see the result all right and now what we're gonna do is that we're gonna reverse the order and let's see if that gives us the same result so we'll write here var we put an equal sign we give here a dollar sign we write here double parenthesis and then we're gonna write here two plus one okay we are reversing the order and then we're gonna write here multiply by three okay so what would happen that it would do this thing first two plus one which would be three and then it would basically multiply three by three in the same way it would basically add this first three it would be three and then three multiplied by three would be nine so in both cases we're going to get a result nine with one uh condition that we have to name it another variable because two variables cannot print out two times right like it can print out two times but it cannot print out two different values so in order to make sure that those values are different we just write here new variable so now if we run this uh we're gonna get the same result so let's save it let's go ahead to our terminal we try to run our script and you can see that we have got the result same nine and nine so what has happened here that regardless of the order it has done the things in the same way it has added it first and then it has basically multiplied it hope you got the point okay so now let's move on to our next topic which is basically combining two strings in one line with variables so what we're going to do is that we are going to define two simple variables these are going to be the string variables of course and then we're going to try to concatenate them okay so we will go to our script and in here we are going to define two variables so first variable is going to be called var1 then we're going to put an equal sign here and of course it is a string variable so we'll just write here hello world all right so the second variable would be vr2 and we would give it a string value and let's call it let's concatenate okay so here you can see that we have uh two string values and now what we're going to do is that we're going to create a third variable in which we basically concatenate them okay so we have variable three then we put an equal sign here and then we write double quotes and inside those double quotes what we're going to do is that we have a dollar sign then we have a brackets all right these are the curly brackets that we do have normally and then we have here we are one which is a variable one of course and then along with that without having anything between them we have a dollar sign and then we write here a variable name which is a second variable name okay so hope you've got the point so now this way they would be concatenated and now they are the concatenated string is now being stored in the variable three so in order to extract that or print that on your screen all you have to do is that you have to write here echo and then you have to write here variable 3 and bingo you are done so once you do that just basically save it press ctrl s go back to your terminal and inside our terminal we're gonna run our script first script dot sh of course and before that we're gonna write dot and slash and we hit enter and you can see that it says hello world let's concatenate so now these two strings have been combined so let's try that if i give a space here if i press ctrl s if i come back to terminal run it that this kind of thing didn't actually work so in order to give a space here i might have to use slashes or maybe to take it to the next line i have to use something else okay so that would be probably uh slash n alright so here you can see that uh with a space i saved the file it it has worked but if i want to take it to the next line of course in that case if i write here slash n for slash n let's try this now if i run my script now you can see that it is printing out slash and here as it is right so another thing that you could try is that you could try giving in multiple spaces so let's give these many spaces and now let's run the script you can see that now these many spaces have been given in here so it's on you if you want to concatenate two strings using variables that's how you basically do it all right hope you have got the point all right so now let's move on to our next topic which is combining three strings in one line with variables so the idea is that previously we have done it with uh two variables so now let's go ahead and let's see that if we could do it with three variables or four or five maybe right so let's go ahead and let's try to do it so we'll go to our script and inside our script what we're gonna do here we have two variables already so what we're going to do is that we're going to have another variable let's call it variable 3 let's call it variable 4. so inside this variable 3 what i'm going to do is that i'm going to put something another value like let's call it uh let's concatenate again okay or let's do it again let's call it all right so once i have added the value inside the third variable uh adding it and concatenating is pretty simple as it was a previously in the previous uh topic of course so what we're gonna do is that we're gonna have here a dollar sign and then we're gonna have curly braces and then we're gonna write here variable three okay so once we do that now what we're gonna do is that we're gonna have here echo variable four instead of variable three because right now we are concatenating all three variables inside the variable four right so now we have done this echo variable four now we press ctrl s we go back to our terminal we try to run our script so let me just clear it first now we run our script and you can see that we have hello world let's concatenate let's do it again so that's how you basically concatenate three variables in case you want to concatenate more than three variables the concept is simple all you have to do is that you have to create another variable and then you have to add that variable inside the series of variable that we have written here and then you have to basically print out the main variable in which you are adding all other variables and that's how you basically concatenate three or more than three or multiple variables or multiple strings hope you've got them all right so now let's move on to our next topic which is topic number 41 in which we are going to use plus equal operator to combine strings so let's go ahead and let's see that how we can do it so we'll go to our script which is essentially first script dot sh so what we're going to do is that we are going to have two variables and we are going to combine those two variables using plus equal operator and then we're gonna see if this thing works or not so what we're gonna do is that we only need two variables variable one and variable two which we have already there which is hello world and the second variable says let's concatenate so the idea is that we can combine variable one and variable two inside one variable so let's say that if we were to combine these two variables inside one variable which is essentially variable one then we'd use plus equal operator and both of the strings would be concatenated so let's go ahead and let's see how we can do that so we just write here var1 which is essentially variable one and then we're going to write here plus equal operator uh and then we're gonna write here double equals or double quotes excuse me and inside these double quotes i'm going to write here var which means that whatever the value of vr1 is that plus equal sign would actually add up the other variables value to it okay so what we're gonna do is that we're gonna print out here variable one uh we're gonna echo it basically and we're gonna save it let's go to our terminal and try to run okay you can see that now i'm running my scripts on 20.04 my 18.04 operating system just got crashed for some reason so let's go ahead and let's try to run our script so first of all what i want to do is that i want to make sure that uh if my script is executable here and you can see that it's not executable it doesn't have any executable permissions so in order to add executable permission i just have to write here chmod plus x and then i'm gonna write here the file name which is essentially first script dot sh okay hit enter now if i lsal here you can see that first script dot sh is now executable and if i run my script you can see that it has given us the result hello world right why it is it has given us the result hello world because here we have not specified the variable variable 2 and this simple variable doesn't even exist so if we write here variable 2 if we save it we go back to our terminal we try to run our script again you can see that now it gives us a result hello world let's concatenate and here you can see all i have done is that i have written here variable 1 plus equal and then i have written here variable 2. so what has happened is that variable 1 it has been a main variable and the variable two it has been a secondary variable and what it has done that it has combined the contents of variable one and variable two inside variable one so that's how you basically concatenate two strings using plus equal variable hope you've got the point all right so now let's move on to our next topic which is topic number 42 in which we're going to show you how to create multi-line string variable with hair dock okay so let's go ahead and let's try to do something let's do an example here so we'll go to our script and here what we're going to do that let's just remove all of that and here we're going to write var and then we're gonna write here equal sign we're gonna write here a dollar sign and then we're gonna have here cat and after that we're gonna have here two angle brackets and here we're gonna write end here doc okay so end underscore here doc and once we have done that now what we're gonna do is that we are going to write multi-line comments so here we could write here we could write multi-line strings excuse me so we write here multi line string using here doc okay so once you have written here multi-line strings in order to end that what you're going to do is that you're going to use here here doc so you would write your end here doc okay so once you've done that now you're gonna come out of here and now if we echo this the variable we're gonna get the multi-line string result so what we would do we're just right here echo var we're going to control s we're going to press ctrl s and here we're going to go to our terminal in order to run our script we hit enter and you can see that now it is printing out basically multi-line string and how did we do that we have done it using uh the here doc variable which would basically help us get multi-line input inside one variable and you can see that we have printed out multiple lines comments using just one single variable hope you have got the point okay so now let's move on to our next topic which is topic number 43 in which we're gonna see that how we can cat multi-line here doc text so the idea is that hair doc text it could be like here dog pointer it could be anything like you could write end here dog or you could write your name or you could write linux sent you know you could write anything in all capital and it would be considered as the hair doc starter and also the endpoint so let's go ahead and let's try to do that um we will just go to our script in here and let's try to remove all of that and here what we're going to do is that we're going to have cat we're gonna have double angle brackets and in here we're gonna have a variable uh here dock actually and let's call this here dog as linux hint okay and in here this is the starting point basically and where we want to end we'll also write here linux send okay we can also write here here doc or we can write here end here doc instead of linux send you know it totally depends upon you what we're going to do is that we're going to write here the current directory or the current working directory is and of course we're going to write here pwd and it would basically print out the current directory and then we're going to write here you are logged in as and then of course we're going to write here the variable for the user okay so if we write here who am i it would basically print out the user name which is which we are currently logged in as okay so we are logged in as linux and so probably it's going to print out that so we are just going to end the here talk here so we'll just write your linux hint and now it has been ended so now we save our script let's go back to our terminal let's just clear it and try to run our script and here you can see that it has basically printed out that the current working directory is slash home slash linux and slash desktop because that's here we are you can see that our current directory is desktop right and if we talk about the user that we are logged in as you can see that here it is telling us you are logged in as and then who am i it has printed out linux hint okay so that's how you basically get the here dot text um using a bash script hope you've got the point okay so now let's move on to our next topic which is topic number 44 if conditional statements so the idea is that if condition statement it's something like that you have to have a statement a condition and if condition is true only in that case the if block would be executed otherwise it's not going to execute anything or it's not going to execute any block so let's go ahead and let's try to write an if condition here and verify if we could use if conditional statement to check if the variable that we would have is greater than five okay so that's what we're gonna do this is a simple example you could have any other example if you want to okay so let's go to our script and let's try to remove everything that we have here because here we're gonna write an if condition so first of all let's define a variable let's give it a value eight and what we're gonna do is that we're gonna have an if condition here we're gonna have big brackets here and make sure that you give a space between here okay so before writing anything you have to have a space here if you don't give a space and you start writing right away it would cause an error okay so i'm going to show you that as well that how it would cause an error so if we write here var the variable and then we write here hyphen gt which is a condition which basically checks if the variable is greater than the given input or given number so if we write here five of course variable uh is greater than five right eight is greater than five so it's going to be true and definitely it's going to execute the if block so we just write here then and then we basically echo good okay and then what we're gonna do is that we're gonna end the if block all right so if that condition is gonna be false in that case of course we don't have any else block and of certainly we don't have anything else outside the if block so this condition if that is false the program is going to return nothing so you won't you won't see any output all right so if we save it we go back to our terminal and if we try to run our script you can see that it basically echo good here okay so if we write here 9 of course 8 is not greater than 9 and this condition would become false and this script won't print out anything okay so if we write here 5 again if we go back run our script you can see that it is printing out echo good so if we remove that space let's say if we don't have any space here then it's going to print out some sort of error so you can see that it says command not found it has not detected the greater than command because we have not given here a space right so it is mandatory on both sides you have to give a space and then you start writing whatever condition you would write inside the if statement so hope you've got the point that's how you write an if conditional statement okay so now let's move on to our next topic which is topic number 45 and in this topic we're going to talk about logical and operator in conditional statements so let's go ahead and let's try to um do an example and let's try to understand this thing okay so here you can see that we have let's call it variable equals four and we have the if block here so let's say that we are going to have two if uh the conditions here okay inside the single if block so for that we have double brackets here and then we have and operator and what we're gonna do is that we're gonna have here let's say variable greater than two and what we're gonna do is that we are also going to use a less than operator okay so we give a space and we will write here var which is essentially the variable name then we write here hyphen lt and then we write here eight okay so this means that basically if condition uh has now been extended into two conditions and it won't work until unless both conditions are true because we have and operator between them so what does it mean having and operator between two conditions simultaneously so the idea is that you have and condition between two conditions it means that both of them have to be true in order to execute the if block if one of them is false either one of them okay then if condition or if block is not going to be executed and it's going to consider itself false so in order to execute this echo good both of the conditions have to be true period okay so now if we save it you can see here um we have we are checking here we have variable equals four um first condition is if variable is greater than two of course four is greater than two right and then we have and operator which means that there is another condition which we need to verify before executing the if block so it would come here so 4 is less than 8 yes of course this is true as well so now both of them are true that's why this thing here would be executed so we can echo here anything like echo and then we could write here that var is between two and eight okay so in that case this condition would be true and it by any chance if that condition is false in that case it is not going to break out anything because we don't have any else block and we don't have anything else in our program two right so we save it we go back to our terminal and we try to run this script so you can see that it says four is between two and eight and of course this statement is true right so if we make any of the condition false knowingly it won't execute anything so for example if we do that 4 is greater than 5 of course this is not true this is false so this condition would become false and you can see this condition is true however because of the being false of this condition it would not execute this if block so we can verify it we can save this file we go back to our terminal we try to run our script and you can see that we have not got any output okay so hope you've got the point so in the and logical operator both of the conditions have to be true all right so now let's move on to our next topic which is topic number 46 and in which we're gonna talk about logical or unconditional statement okay so let's go ahead and let's try to do an example but before that i would like to explain it to you that and operator in the case of and operator both of the conditions have to be true right but in this case of r you don't have to consider both of statements would be true only then the if block would be executed it won't happen so even if your one condition is true it would consider that it should execute the if block right but in the case of and statement both had to be true only then it would execute the if block right it was pretty simple hope you've got the point so now we'll basically do an example and let's see if you could basically get a better concept so for that we'd go to our script all right here we have a variable let's change the value to variable let's call it 16 okay variable have a value of 16. and here we can put a condition that variable is greater than 2 or 5 let's keep it 5 and here instead of having and we're going to have a double pipe operator sign which is used for r okay and here we're gonna say that variable is less than you know eight let's keep it that way so the idea is that we know that 16 is greater than 5 that condition is true but we also do know that 16 is not less than 8 right 16 is actually greater than 8. so this condition is false right now but this condition is true so here you can see that inside the if block we have an echo statement which says the variable is between two and eight so let's say that variable or let's call say one condition or both are true so same cases with the both true statement if both of them are true wonderful still if lock would be get it would get executed okay still if block would be executed okay so i hope you've got the point uh and now if let's say i want the second statement false or second statement true and if i make the first statement false still it's gonna work right so it says 16 is greater than 18 which is absolutely false right 16 is less than 18 and here you can say that it says 16 is less than 20 which is true right so previously this first condition was true and this one was false now the second one is true and the last one the first one is false okay but still it would execute this if block so how do we know that all right so now let's go ahead and let's try to run our program and let's see so if we run our script here you can see that it says one condition or both are true because we know that this one was true so if we reverse the order as we have done previously you can see that now this condition is true and this one is false so if we save it we go back to our terminal we try to run our script and you can see that it still has executed the if block so what if both of the conditions are false so in that case what's going to happen that both condition if both conditions are false it is not going to execute its block okay so both condition had to be false in order not to execute the if block or whatever the thing that you have okay so let's say uh we know that variable uh 16 16 is greater than 22 it's false and 16 is less than 8 that is false as well okay so if we do that we save it we go back to our terminal we try to run our script and you can see that we have not got any kind of result okay so it is a proof that only one condition has to be true then it would execute you know in in the case where both conditions are false it would not execute okay hope you've got the point all right so now let's move on to our next topic in which we are going to discuss if else if and else conditional statement so previously we have seen if statements only now we'll see the else if and the else statement additionally okay so for that we'll just do a simple example because of which you could simply understand what exactly it is so we'll go back to our script that we are writing right now and in here let me just remove all of that and let's try to create a scenario so let's say that we know that how a student is graded right so for example if he has the marks above then 90 then we can say a grade or we can say excellent okay so if the marks are like you know greater than 60 we could say okay good and if let's say they are greater than 50 we could say just satisfactory if they are less than 50 we could say that it is not satisfactory and your fail so something like that so this is a scenario that on which we are going to do an example so let's go ahead so first of all we'll create a variable called as marks and let's say we give it a value as 65 for now the next thing we're going to do is that we have an if block here an if condition and here we put a condition like let's say if the variable marks if it is greater than 90 okay so if it is greater than 90 then we say that excellent okay so we write here then and then we write here echo statement and we write here excellent okay so here you can see that one if statement has been done right and in order to end the if statement we all know that we have to write here f i but here or if block we are not then done yet with that okay so we're gonna have an else if block okay so first for example if the first condition is not true instead of breaking out of the if body it's gonna check for else if which is another if block again we check if the marks let's say this time we write here a condition if the marks they are greater than let's say 60 okay so if they are greater than 60 what we want to do is that we're going to print echo and let's say good okay and we can the beauty of this having else is that you can put as many if statements within one if block as you possibly can okay so there is no restriction to that so we come here again and we write here another else if block we write here brackets and inside these brackets we write here marks and then we write here greater than or equal to and let's say we write here 50 and then we write here then and then we write here echo and now let's say we write here just satisfactory because of course if the marks are over 50 just over 50 then we can say satisfactory okay so once we have done that now what we're gonna do is that we are going to write here an else block okay so else block means that if all if else if fail only then this else block is going to be executed so we write here echo and then we write here not okay and then we write here fail okay so that is it you know we have our if else if and else blocks right in the correct position okay so now let's go ahead and let's see if um this thing works or not so you could be thinking that if we have a number 95 then of course it's greater than 60 it's square than 90 it's greater than 50 right so how this condition set a set of condition is you know perfect so let me tell you the first thing first so first of all this if block is going to execute it so for example if the number is not greater than 90 it's gonna come to the next else if block if the number is greater than 90 it's gonna pick it up here print this out and then break the brick out of the if block so it's not going to look at the other else if block even okay so that's why this uh you know set of statement works and it would not cause any logical error okay so let me explain to you that what this code does first of all we know that we have marks which is equal to 65 it would come here it would check 65 is greater than 90 this is not true this is false right so it's not going to execute this thing here it's going to come to the next block so here you can see that as if the marks is greater than 60 so 65 is greater than 60 it is true so it is going to echo good and it is going to break out of the if statement it's not going to check out any further so now let's go ahead and let's try to run our script and let's see what results we get so we go to our terminal and in here we try to run our script and you can see that we have not got get any kind of result and why is that because we have not saved it so we save it we go back we run our script and you can see that we have got the result as good okay so white has printed out good because the marks are 65. so let's say we write here 95 instead of 65 so now what results uh we're gonna get so we go back to our terminal again we run our script and you can see that we've got the result excellent if we just write here any marks that are like above than 50 then in that case what's going to happen is that it's going to come here i'm going to run it and you can see that it prints out just satisfactory and if i change the marks to less than 50 let's call it 25 and if i save it i go back to my terminal try to run it and you can see that it says not okay fail so as i have explained to you that how each condition is gonna work you could see that we have worked with almost every condition and each time it has executed that specific condition perfectly hope you've got the point now let's move on to our next script that is nesting if condition you have got an idea about the if else condition in the previous scripts so in this script we will see that how we can choose the nesting if condition in batch let's get started this is my script dot sh file so to see the working of nesting if condition we have to declare a variable so i have declared a variable here which is equal to 6 so let's declare an if condition here by using the if keyword is double quotation dollar variable so i'm going to evaluate this variable on to the greater than condition so whenever we have to use the greater than condition in batch we have to write here hyphen gt 1 so if dollar variable is greater than 1 so what it has to do then [Music] we have to insert an other if condition so the nesting means that we have multiple if conditions that are working simultaneously and if condition within an if condition so all right so i'm going to write here if dollar variable is less than so whenever we have to evaluate the expression on the basis of less than we have to write hyphen lt if it is less than 10 all right so let me just add the space here as well then echo the number lies between 1 to 10 all right so we have two if conditions here so i am going to end this secondary block first f i fi so this is now the example of the nesting if conditions this is the first if condition this is the other if condition which lies inside the first if condition all right so this is our program what it will do it will check that if the variable is greater than 1 it will execute this if condition if variable is less than 10 so it will print on the terminal that the number lies between 1 to 10 and lastly we are entering the if conditions all right so this is our script let's open up the terminal [Music] let's say in the directory to the desktop because the script dot sh is saved on the desktop all right so now let's execute our script script dot asset and it has printed on the terminal the number lies between 1 to 10 so that is an idea of the nesting if condition i hope you got my point all right so now let's move on to our next script equal versus double equal operator in this script we will see that what is the basic difference between choosing equal and double equal operator the equal operator basically compared the type o for multiple variables but the double equal operator perform by to byte comparison and it check that whether the value stored inside the variable is same or not so let's see the script of this all right so these are our two variables the variable one is bar whose value is equal to hello and the second variable is rav whose value is equal to word so these two variables are of string type because they contain some characters or word so let's write the first equal operator if dollar variable is equal to dollar rab then and we are putting an other if condition here if dollar variable is equal to is equal to dollar rave do not forget to give the space between the bracket and your variable because otherwise it will throw an error all right so if this condition satisfies then what it has to do it has to print here both equal and double equal operators are same all right so if this condition falls then what it has to do and it has to execute the else condition like that else echo all right so in this case it will print both equal and double equal operators are not same all right so this is our script we have to end the first if condition so what it will do basically first of all it will compare the type of these two variables the type of these two variable is same both belong to the string and after that it will execute this if condition dollar variable is double equal to dollar rav so now it will perform the by to byte comparison if the data inside these two variable is same then it will print both equal and double equal operators are same and otherwise it will execute the else condition and it will print on the terminal both equal and double equal operators are not same let's go back to our terminal and execute our script and you can see that it prints on the terminal both equal and double equal operators are not same so this is the misconception that we use this is an ambiguous statement that most of the people say that the equal and double equal operators are same but in actual they are not same i hope you got my point all right so now let's move on to our next script that is test not equal strings so far we are done with the equal comparison in this script we will see that how we can test not equal strings in back so let's move on to our script dot sh file this is the first variable variable which is equal to hello and the second variable is rav which is equal to word so whenever you have to test for the not equality or inequality then you have to use this sign exclamation sign or factorial sign and then we have to put the equal sign so this is not equal to sign here so if dollar variable is not is equal to dollar rav then what it has to do it will print on the terminal both the strings are not same and else it will print on the terminal that both the strings are same in this case both the strings are not same so it will print both the strings are not same on the terminal so let's go back to our terminal and let's execute our script so it print here both strings are not same so in this case this echo command is executed because variable is not equal to rav so this condition is basically true and it has printed on the console both strings are not same so i hope you got my point all right so now let's move on to our next script in this script we will test that whether the two strings before or after are equal alphabetically or not so in the script number 49 i described that when we have to check that whether the data store inside the string is equal or not for this purpose we have to use the double equal operator let's see a script for this purpose this is my script dot sh file so i am going to change the value of the second variable is equal to hello and now we have to perform a comparison alphabetically so i am going to put here the double equal operator if this condition is true we have to print here in the echo condition both the strings are same alphabetically otherwise we have to print both the strings are not same alphabetically all right so it will perform a bite to bite comparison here let's go back to our terminal and execute our skip dot asset so both the strings contain hello and hello so both strings are same alphabetically i hope you got my point all right so now let's move on to our next script test string is null in this script we will check that how we can evaluate whether the test string or the given string is null or not so this is our script dot sh file i am going to remove the second variable because we do not need it this is our first variable a string type variable and it does not contain any value it is equal to null all right so now we have to change the string here the string is is null else we have to print the string is not null all right here i have only injected the column it means that this is the empty string it also demonstrates that this is the empty string so when it will execute the if condition it will check that if the variable is equal to empty it will print the string is null otherwise it will print the string is not null so i have saved my script let's go back to our terminal [Music] and the string is null because it does not contain any value i hope you got my point all right so now let's move on to our next script test string is not null in this script we will check that how we can evaluate whether the given script is null or not null previously we have checked the null string and now we will check the not null string so let's go back to our script i am going to write here hello the string is not null now the if condition will remain same if variable is equal to null then it has to print the string is null otherwise it has to execute the else block and it will print the string is not null so our expected result is gonna be the string is not null so let's go back to our terminal and execute our script the string is not null this is the result that we have got because the value of the variable is equal to hello when it comes to the if condition dollar variable is equal to null the condition is false it is executing the else block and it is printing on the console the string is not null i hope you got my point all right so now let's move on to our next script the name of our next script is test numerical comparisons greater than and less than in this script we will see that how we can perform the numerical comparisons how we can compare the two numbers whether a specific number is greater than the other number or not or it is less than the other number or not so this is the agenda of this script so now let's move on to our code this is the code for this script let me just copy this code and paste this code into our script dot sh file so first of all we have declared a variable here whose value is equal to 50. all right so now when we have to perform the comparison we have to write the if condition here if dollar variable is greater than 10 so hyphen gt is used for the greater than comparison and dollar variable is less than 20 for the less than comparison we'll use the hyphen lt so these are our two conditions these are the end conditions first of all the variables value will be compared to 10 if the variable value is greater than 10 and the variable value is less than 20 then what it has to do it will print dollar variable lies somewhere between 10 and 20 so let me just tell you again for the greater than comparison we use hyphen gt and for the less than comparison we use the hyphen lt all right so this is our script now let's open up our terminal and execute the script dot sh file all right script dot sh file is present on the desktop so script dot sh the value of the variable was 15 so 15 lies somewhere between 10 and 20. so the end conditions with the if are true both the conditions are true so this is how you have to perform the numerical comparison greater than or less than for the greater than we have to use hyphen gt and for the less than we have to use hyphen lt so our next script is test numerical compare greater than or equal less than or equal so now we have to compare between the greater than or equal or less than or equal this is our code for the greater than or equal or for less than or equal so now let's copy this code into our script dot asset file so all right so now the value of the variable is equal to 25 when you have to perform the arithmetic operations like greater than or less than then we can use this sign as well so this sign represents a greater than and this sign represents the less than and the meaning of this compound sign is greater than equal to less than equal to so all right when the code will execute it will execute the if condition if the dollar variable is greater than equal to 20 and dollar variable is equal to less than 30 and dollar variable is less than equal to 30 then it has to print on the terminal that the total variable lies somewhere between 20 and 30. when you have the and operator here it indicates that both of your conditions must be true so in this case the value of the variable is equal to 25 25 is greater than equal to 20 it is true and 25 is less than equal to 30 it means that this condition is also true all right so let's go back to our terminal and let me just clear the terminal and if we execute our script assets again you can see that the 25 lies somewhere between 20 and 30 it means that both the conditions are true so this is how you can perform the greater than equal to or less than or equal to comparison in your batch script but the thing that you have to remember you have to remember about these signs our next script is test compare numbers are equal so in this script we will basically check that whether our numbers are equal or not this is our code for this script which is test compare numbers are equal now we have to compare the two variables we have to compare the equality of the variables so therefore i have two variables here variable is equal to 50 and the other variable is equal to variable 1 which is equal to 50. so in order to perform the comparison we have to implement the if condition if dollar variable is equal to is equal to dollar variable 1 so the double equal sign is basically used to evaluate the values of two variables so then what it has to do it has to print on the terminal compare variables are equal if this condition is false then else block will be executed and it will print on the terminal that the compared variables are not equal so i hope you got my point that when you have to compare the value of two variables you have to use the double equal sign or double equal operator so this is our script dot asset file let me just put this code here and go back to our terminal and if we execute our code or if we execute our script now you can see that the value for the variable 1 was 50 the value for the other variable was also 50 so when it executed the if condition it has printed the compared variables are equal you can get the better idea of our output from here variable is equal to 50 variable 1 is equal to 50 when it performs this comparison variable is equal to is equal to variable 1 so the compared variables are equal so that was the whole concept of comparing the variables for equality all right so now let's move on to our next script that is test compare numbers are not equal [Music] this is exactly the same that we did in the previous script the script number 56 but now what we will do in this script we will change the values of the variables now the values of the variables will not be equal and in this case the else block will be executed so this is the sample code for this script now we have two variables variable is equal to 50 and other variable is equal to 25 if dollar variable is equal to is equal to variable 1 then what it has to do it has to print on the terminal compared variables are equal otherwise if the condition is false it will print all the terminal the compared variables are not equal so this is our script let's go back to our script dot a set file and try to execute our script and let's see that what we get on the terminal script dot is it the compared variables are not equal because because in this case the else condition is executed instead of this double equal sign we can put the condition here is not equal and in this case the condition will be true the factorial or exclamation sign with the equal operator is used for the not equal to condition so now we have changed the condition if variable is not equal to variable 1 then it has to print compared variables are not equal otherwise it has to print are equal so we can change our script in this way as well i hope you got my point let's go back to our terminal let's clear the terminal and execute the compared variables are not equal in this case the condition is true and this line is printed on the terminal all right so now let's move on to our next script in this script we will see that whether a test file exists or not in the bash scripting we can check the existence or we can verify the existence of the files and the bash provide a specific flag for this purpose and this flag is hyphen f flag so let's see that how we can do it all right so this is our code for checking the existence of file first of all we have declared a variable file variable to store the name of the file inside this variable so file is equal to file.txt file.txt is the file that we have to check whether it exist or not so for the existence of the file we choose the flag that is hyphen f so we have written the if condition here if hyphen f dollar file it means that if the dollar file or file.txt value exist inside the hyphen f flag then what it has to do it has to print on the terminal that the file exists otherwise it will print that the file does not exist so let's paste our code inside the script dot sh file and to go back to our terminal and let's try to execute our code all right so script.sh the file.txt does not exist so if i create a file on my desktop named as file.txt and if we run our script again now you can see that it prints on the terminal that the file exists so firstly when it came to this if condition it checks that the file.txt was not present on the desktop that it printed or the terminal file does not exist and when it found the file it printed on the terminal that the file exists so that is how you can verify the existence of file in bash now let's move on to our next script that is test file is not zero size in this script we will see that how we can evaluate or how we can check the size of the txt file or any file this is our code to check the size of the file let me just paste this code in script.sh all right now to check the size of the file we use the hyphen s flag for this purpose so the file variable is equal to file.txt it is the name of our file if dollar if hyphen s dollar file it means that it will evaluate now the size of the file if the file have some size then what it will do it will print on the terminal file is not of zero size otherwise it will print the file is of zero size or empty so now let's go back to our terminal execute our script again file is of zero size or empty because the file is just created because we have created the file in the previous example so let's insert some data into the file echo hello this is file and let me just store it into the file.txt and if we run our script again the file is not of zero size or not empty because now it contains some data i hope you got my point that how you can check that whether your file is of zero size or not now let's move on to our next script test file is a directory or not in the batch we can evaluate whether the file is a directory or not all right so this is our code to check whether the file is a directory or not now what we have to do we have stored the name of the directory inside a path variable so to check the existence of the directory we choose the hyphen d flag if hyphen d dollar path if the condition is true then it will print path is a directory and not file after that we have the else if condition if hyphen f dollar path if the file is if the given path is a file then it will print or the terminal the path is a file and not a directory otherwise it will print the path is not valid so this is our code let's paste it into the script dot is it and let's evaluate all right so directory is not valid because it is neither a directory nor a file so let's create a directory here with the name dir and let's execute our file again all right so dir is a directory and not a file so in this case it has basically found that the dir is a directory mkdir is a command to create the directories in bash so this is how you can check that whether the given path is a directory or file to evaluate that whether it is a directory we choose the hyphen t flag so that's it for this script i hope you got the point all right so now let's move on to our next script in this script we have to check whether the given file is a symbolic link or not for this purpose we will also use a flag and the name of that flag is hyphen l to check whether the file is a symbolic link what we will do we will create for this purpose we will choose the hyphen l flag if the given file is a symbolic link then it will print it on the terminal that the given file is a symbolic link let's paste our code in our script dot sh file so we have created a variable here for the file file is equal to link one link one is the name of our symbolic link if hyphen l dollar file it means that if the file is a symbolic link then it will print on the terminal that the file is a symbolic link let's go back to our terminal and execute this script dot sh file and let's create a file here that is link one all right let's execute our script again if we put the else block here it will be easy for us to evaluate the conditions echo dollar file is not a symbolic link all right so link one is not a symbolic link because it is a file we have just created a file here so this is how you can check the existence of a symbolic link by using the hyphen l flag all right now let's move on to our next script that is about to check whether the test file has read permission or not in the linux based operating system and unix based operating system you know that every file have the read write and executable permissions if a file has a read permission it means that we can read the data from the file so through the bash script we can check that whether the file has a read permission or not so let's see that how we can do it all right so this is our code for this script i'm gonna paste it script dot sh file to check the read permission of any file we have to use the hyphen r flag so what we have done here we have created a variable file and we have stored the file name inside this file variable so now it will check if dollar r contains some information of reading of the file then it will print on the terminal the file has the read permission otherwise it otherwise it will print on the terminal the file does not have the read permission so dollar r is the main phenomena here in this script let's go back to our terminal and let's execute our script so the file.txt has the read permission i hope you got my point the next script is about checking that whether the test file has write permission or not so for checking the right permission we use a flag that is hyphen w flag the hyphen w flags evaluate that the file has the right permission or not if the file has some right permission we can write the data into the file otherwise we cannot write the data into the file so this is our code let's paste this code into our script dot sh file hyphen w is the main phenomena to understand here all right let's go back to our terminal let me just clear the terminal and execute our script again so file.txt has write permission if we change the right permission of the file through chmod minus w file dot txt and if we execute our script again now you can see that the file.txt does not have the right permission okay let me just add the right permission again file.txt and the file.txt has the right permission so we have to use the hyphen w for the right permission and we have to use the hyphen r for the read permission these are very important flags in bash so i hope you got my point and that's it for this script all right so now let's move on to our next script the next script is about test file has execute permission so in this script we have to check whether the test file have the executable permission or not we will use the hyphen x flag to check the executable permission as we did in the previous script but the difference is that for the right permission we use the hyphen w flag but for the execute permission we will use hyphen x flag so now let's see the script for this all right so this is our script we have a file variable here and the name of the file is file.txt which is stored into the file variable and now we have put the if condition here if hyphen x dollar file it means that it has to check whether the file has the executable permission or not if the file has the executable permission then it will write dollar file has execute permission else it will print it does not have the executable permission all right i hope you understand the point that the hyphen x is the main phenomena here it is the main flag to check the executable permission let's go back to our terminal and try to execute our script dot sh all right file.txt does not have execute table permission so let's keep the file.txt the execute table permission by using the chmod command file dot txt all right i think the file does not exist let me just create a file here with the name file.txt all right if we run our script again the file.txt does not have the executable permission all right now the permission is granted if we check here ls iphone al and the file permission is and the executable permission is connected to the file all right let's run our script again the file.txt has the execute permission so this is how you can check the executable permission of the file in bash now let's move on to our next script the script number 65 the name of the script is case conditional statements with numbers the case statement is basically a conditional statement it evaluate the condition or it check the value of any variable then we have defined the multiple cases and the condition is evaluated according to these cases if it match with the particular case then it print the output on the terminal so let's see an example of it this is our example program this is our script for the case first of all we have declared a variable whose value is equal to 10 whenever you have to apply the case conditional statement we have to use this word case so let me just copy this in script dot age and you can see that case is the keyword here and we have to apply the case on the variable so case dollar variable in if the value of the variable is equal to 10 so basically the 10 is the case here is one case whenever you have to define a case you have to define it in this way so if the variables value is equal to 10 it has to print echo it is 10 and it is the end of the first case the double semicolon then we have another case the case number two if the value is equal to 20 then it has to print all the terminal it is 20. the second the third is the value number 30 if the value of the variable is equal to 30 then it will print on the terminal it is 30 and lastly in the case statement we have a default case so if the value is not equal to 10 20 or 30 then this default case will be executed and it will print number is not 10 or 20 or 30 it is something else so i hope you got my point let's go back to our terminal and try to execute this script it is 10 and if we change the value and make it 100 and we execute our script again so number is not 10 it is not 20 or 30 but it is something else so in this case the default case statement is executed so this is how you can apply the case statement in your bash script i hope you got my point all right now let's move on to our next script case conditional statement with strings in the last script we evaluated the numbers with the case conditional statement but in this script we will evaluate the case conditional statement with strings all the things will be remain same but now we have to define the strings instead of numbers all right this is our code for the case conditional statement with strings in this case now we have a variable car whose value is equal to bmw and we have multiple cases here the case is applied on the car variable so if the case value is equal to toyota then what it will do it will print it is toita if it is honda it will print it's a honda and if it is a pmw it will say it's a bmw and after that we have a default case if the car does not belong to toyota hona or bmw then it will print um seems like another card so this is our default case this case is basically executed when the string does not match with the above cases and whenever we have to end the case statement we have to write this keyword esac all right this is our skip dot sh file i have pasted my code here let's go back to our terminal and try to execute it script dot sh it's a bmw if we write here konda so now this case will be executed and it will print on the terminal it's a honda all right and if we write here some other card like suzuki so now it will execute this default case and it will print it seems like another car i hope you got my point all right now let's move on to our next script case conditional statement with wild cards so when we talk about the wild cards we are basically taking the input from the user we define the cases that evaluate the input from the user and then it will execute the case so let's have a look on the case conditional statement with wildcards all right so this is our script for the case statement with wired cards so what it will do the user will enter something the user will enter any word so for the case number one we have written here dollar one it means that it will evaluate the first word that will be entered by the user and if we write here dollar two it will evaluate the two words then three words forward and so on if the input from the user contain req and after that it has some words then it will say it's a requirement or it's a request if the user input contains meet and then it will write on the terminal it's a meeting otherwise it will execute the default case and it will write this is a default statement all right so now let's execute our script we haven't entered any data on the terminal so it has printed this is a default statement so if i write here req requirement like that it will say this is a requirement because the bash is a case sensitive language so let's execute it again and write here request or something like that and it says it's a requirement or request and if i write here hello it will execute the default statement and if i write here meet me at 11 now it say it's a meeting so this is how the wildcards work in batch script i hope you got mine all right the next script is execute a command with backticks so in this script we will see that how we can execute the commands which contain the backticks so let's go to our script.sh this is our script dot sh file it contains ch own command the ch one command is used to change the ownership of any directory or any file the ch one command will basically the owner of the directory dir with linux it if it has any other owner like test user or something like that then it will change the owner to linux sync this is a bad tip so so in this way the backticks can be included in the batch scripting and we can execute our command all right let's go back to our terminal this is our terminal we have dir here this is our directory and let's try to execute it first of all let me just show you the owner of directory the owner of directory is already linuxing so if we execute this command it will not have an impact on the directory corner so what we have to do i have another user which is the test user and i want to replace this linux synth with the test user all right so this is our script it required the suit of permission so write your sudo script dot asset enter the password and if we write here ls hyphen al and you can see that now the owner of the dir is test user so this is how you can execute your command with backticks i hope you got my point all right so now let's move on to our next script that is execute a command in a dollar parenthesis variable so what we have to do in this command we have to store a command into a variable and then we have to execute the command through the variable so let's see the script for that so all right this is our code i'm just gonna copy this code in our script dot sh file all right the cat command is used to print the content of any file or text file so in this case when we have to use this with the dollar sign after putting the dollar sign we have to insert the parenthesis and inside the parenthesis we have to write our command so cat file.txt is our command and it is stored into a variable after that we have written here echo echo dollar variable so it will print our command all right i hope you got my point that when you have to execute your command with the dollar sign then you have to put the dollar sign here after that you have to put the parenthesis write down your command and save it into a variable let's go back to our terminal all right so this is our terminal and let's write here script dot sh so it has basically printed hello and welcome this is a bash example so this is the content that we have in our file dot txt so what it has done it has stored the command into the variable and it has executed the command and after that it has printed the content on the terminal the content is hello and welcome this is a batch example that is what we have in file.txt i hope you got my point all right so now let's move on to our next script our next script is about execute a command and to capture the standard output into a new file in this script we will execute a command we will store the content of the file into a variable and after that we will save this content into another file so let's go to our script this is our script here we have a variable in the variable we have stored cat file.txt so what it will do it will basically store the content of file.txt into a variable after that it will save the variables data into the output.txt file if the output.txt file does not exist then it will create a new file all right let's go to our script dot sh and try to execute our script so initially i don't have any output.txt file and it has created a new output.txt file here and the content is stored into this file hello and welcome this is bash example so this is how you can execute a command and you can capture the standard output into another file for capturing the output you have to use this sign and we say this uh greater than sign so variable this sign output dot txt basically points that or indicate that that whatever is stored in variable should be saved in output.txt all right now let's move on to our next script and the script is execute a command and capture the standard error so in this script we will learn that if we execute a command and it shows some error or it throws some error then how we can capture this error into a file all the things are same we have cat file.txt and the content is stored in variable and whatever is in the variable will be stored in standard error.txt file all right let's go to our script.sh and try to execute it all right let's execute our skip dot sh file it shows an error and it has created a new file and the error is stored into this file or the creation of this file is the indication that there occurs some error so i hope you got my point that when the error occurs how you can store it into the file or how you can indicate it through the creation of a new file all right now let's move on to our next script execute a command and capture the return code whenever we execute a command if it is successfully executed then it returns 0 and if there is some error it returns the error code or it returns some code so in this script we will learn that how we can capture this return code and we will create a new file and we will store the return code in this file all right this is our code we are printing the content of file.txt we are storing it into the variable echo dollar variable it will print the content on the terminal and then echo dollar sign question mark it basically prints the return code and it will store it into the new file return code dot txt so dollar with the question mark is used to capture the return code this is the idea all right let's go to our script dot message save this our script and and let's execute our script dot sh file all right it has printed the content on the terminal and created the new file return code dot txt and it store zero it means that the command is executed successfully and the return code is stored into the new file i hope you got my point all right so now let's move on to our next bash script this script is about the execute command the short form of the execute command is exec this command replaces the current process whatever it is running in the bash this command basically replace the previous processes and it execute the current process all right so for example if we are updating our apt repository cache by using the sudo apt update command and we run the exec command or execute command with with echo hello world so what it will do it will terminate the previous command and it will run the current command all right so let's see the working of it in batch that how we can use this command in bash so this is our script for this command so what this command will do here it will create a new txt file that is tmp and it will write the content into the text file that is hello world because the last line of the script is echo hello world it means that it has to put some data into the newly created text file that is essentially tmp all right i'm gonna copy my code into my script dot sh file and let's go back to our terminal all right so this is our terminal let's execute my script dot sh file and you can see that it has created a new file on the desktop that is tmp and the content of this file will be hello world all right so this is how you can use the execute or exec command in bash all right so now let's move on to our next script eval command the eval command is used to print the arguments on the terminal whatever the string it will take as an argument it will print on the terminal it is a very useful command of bash and it is very simple to use so let's see that how we can use this command in our bash script all right so this is our script whenever you have to use eval command you have to write it like this and after that you have to write any string value what it will take as an argument and it will print it on the terminal so i'm gonna copy this script into my my script dot sh file and let's go back to our terminal and try to execute our script so currently the argument that we have passed to the eval command is a hello world so it will print this on the terminal all right so let's execute our script my script dot sh and you can see that it has printed on the terminal hello world so this is eval command for you and this is how it works i hope you got my point okay from here onwards we'll try our best example in the vs card because in this we have an integrated terminal and we can write our script here and get the output in the terminal the next script that we are going to try is for loop list of values in this we'll see that how can we write a for loop in bash for writing a for loop we'll just type for declare a variable here that we are going to use in the for loop let's say i and then we'll provide a list here like one two three four five and then we echo dollar i like this is the value after saving the file if we run our script we can see the output this is the value one two three four and five but what if we want to loop on range of numbers so the next example that we are going to try is a for loop on range of numbers because we can't write one two three four five six seven eight nine ten so we'll give range here in curly braces and we'll just say let's say 10 to 20 all right now if i save the file and run the script again we can see this value 10 11 12 13 14 onward to 20. so this is how how we can use for loop on a range of numbers the next example that we are going to try is for loop output of command in this we need a file.txt so let's create a file.txt in the same directory in which we'll write something and later access it using the for loop like writing something in file we have it here after saving the file let's move to our bash example.sh file and in this we'll write for i in dollar paranthesis get file dot txt and now we'll echo dollar i now if i save the file and run the script again in the terminal we can see in the output writing something in file we have it here all right the next example that we're going to try is using for loop to search for files in the directory so first let's make a couple of files in the same directory and now if we want to list all the files in the same directory we can use for loop to list them like for i and dot slash static dot txt after saving the file running the script in the terminal we can get the list of all the files in the same directory so this is the for loop that runs through the current directory and print out all files with dot txt extension in the next example we are going to see that how can we use for loop with conditional break so let me clear the terminal as well and remove those files as well now if we want to use conditional break with the for loop the syntax is very simple first let's say we want to try the for loop from 1 to 10 curly braces 1 to 10 and now we'll apply if in the for loop now after saving the file and running the script we can see in the output we get the list from one two three and four so this is how we can create a for loop to print from one to ten but if we put a condition inside a for loop to print till 4 and after that break out a for loop we have tried break in the for loop but in the next example we are going to see how can we use continue in the for loop so if we want to use continuum let's change the condition here like dollar i is equal equal to five and continue if dollar i is equal equal to five else echo i now after saving the file and running the script we can see that one two three four five is skipped and later six seven eight nine and 10. all right after learning all about for loop let's move towards the while loop all right in this example we are going to try while loop with integer counter first we'll initialize a variable i is equal to 0 and later we'll write while loop with double parenthesis syntax and in this we'll write plus plus i less than or equal to 5. and in the body of the while loop we'll write echo counter is at dollar i let's try turn at the end of the while loop save the file and run the script we can see the output counter is at one two three four and five the while loop worked perfectly in the next example we are going to try until loop based on file size let's make two files in the directory and now in the bash example we will write anti-loop and which will increase the size of the file until it reaches 2 kb all right after writing this code save the file and run the script in the terminal we can see after a while that the new size of file exceeded target of 2kb this was just a demonstration that how we can use anti-loop based on a file size alright now in the next example we'll see how can we create an array variable and access by index in bash we can create an array using parenthesis syntax like this and access it using the secure bracket syntax if we save the file and run the script in the terminal we can see in the output that we have got the second index element mercedes in the next example we are going to see how can we create an associative array variable and access it using index the syntax for creating an associative array is very simple like first we declare the variable and later we'll create an associative ray like this and now if you want to access any associative index will simply write car and in the square brackets like honda after saving the file and running the script we can see in the output that we have got switch all right in the next example we are going to see how can we access array members with loop so first let's create an array a simple array and then we will apply a for loop in this folder we will iterate through every element of the car and get the output so let's save the file and run the script we can get every element of the car bmw toyota and honda alright in the next example we are going to see how can we use a time command if we want to know about the time of the file like our bash example example.sh we'll simply write time and our file name which is bash examples it will give us the time like real time user and system this is how we can just use time command when running the script to get time details such as real time system time and user time now similarly if we want to print it we'll simply first write a variable in our script file like war is equal to dollar date and later will echo war save the file and run the script hopes i made a mistake in the syntax it's not curly braces it's parentheses sorry for that and here we'll write it let me clear this and now save the file and run the script again that's how we can get that data as well in the next example we are going to see how can we print date with different formats so let me clear the terminal and clear that as well but these are some of the different ways to print date like in this format we'll get dead with the dashes and in this format with percentage g we can get there with the slashes and percentage y it will print out the year only let's run the script well first let me save the file and run the script as we can see in the output these are some of the different ways to print it in the next example we are going to print secondary labs for block of a code now in this code we will iterate through the loop for three times we have echoed out writing code in curly braces to calculate time so let's save the file and run the script as you can see in the output it has printed writing code in curly braces to calculate time three times and then it printed it takes 0.00 seconds to complete this task now let's move to our next example in which we'll see how can we read text from a file and print it to the screen so for that let me first create that terminal and clear the dash examples file as well for this example i'm going to write some code to which i'll explain later but before that we'll create a file in the same directory from where we'll read text in which we'll write some couple of lines and then we'll try to read it let's write the code for reading text from file now in this example we have write a while loop in which we are fading it a file file.txt and then we will echo out every line using while loop so let's save the file and run the script as you can see it has printed out writing something here here it is and hi from linux and now in the next example we'll see how can we read text from a file using an if condition like if we want to print only the first line or seconds so we will write if condition like this now here we have put a condition that if n is greater than 2 then break the while loop so let's see save the file and run the script as you can see it has printed out only two lines let's move to our next example in which we'll see how can we use basically command in this example we'll use read command to get input from the user and then we'll echo it out now let's save the file and run the script after running the script as you can see it is asking me to please enter three words followed by enter so let's write awesome fantastic and great and it will acquit out hello awesome fantastic great now in the next example let's see how can we use read command into an array now in this example we are getting data or array from the user and then we'll echo it out using the for loop so let's save the file and run the script now if i run the script it will ask me to enter an array let's enter an array for example the name of the cars like toyota bmw honda and kia now when i hit enter you will see that it will print out the given input ray member as follows toyota bmw honda kia that's great so this is how we can use read command for entering input into an array and then displaying members of ray let me clear the terminal and move to the next example in which we'll see that how can we use read command with delimiter so for this example as well i'll write the code first and then explain it later now in this we have provided or as a delimiter to the read command ifs is equal to r now if we save the file and run the script we can see that it will print hello car as bmw and model is i8 so this is how we can use delimiter with read command in the next example we'll see how can we use read command with timeout so for that let me clear the terminal and clear the bash examples file as well and write some code now in this example we have set the timeout for 14 seconds now when i save the file and run the script it will ask me to press enter key or wait for 14 seconds now if i press enter key it will stop or otherwise it will wait for 14 second and then it will print date so this is how we can use timeout with read command let's move to our next example in which we'll see how can we use menu option with select command let me clear the terminal and clear the bash examples file and write some code now in this example we have first created out an array cards with the elements bmw toyota and honda and later using the select command will acquire the chosen card by the user so first save the file and run the script it is asking us to please choose a card company now if i enter 2 it will print out total that you choose toyota and if i enter 3 it will print out that you choose honda and if i enter 4 like quit it will obviously get me out of the select command so this is how we can use menu of car companies using select now let's move to our next example in which we'll see how can we make a menu option with select command from array variable now in this example we are doing kind of a same task but using the different technique using an array so in this we have first created a card array bmw toyota and honda and then we'll echo out the selected car using the select statement so let's save the file and run the script now when i hit 2 it will obviously print out that you choose toyota the selected card company is toyota awesome now if i run the script again and i choose somewhere wrong like 5 it will obviously print out wrong selection select any number from one to three so now if i choose one again it will print bmw and break the select statement so this is how we can use menu of car companies with select from an array variable let me clear the terminal and clear the bash example file as well to move to our next example in which we'll see how can we split string by space splitting a string by space is very easy we can simply assign the whole line to the for loop and then we will echo every word so let's save the file and see the output let's run that script as you can see it has printed out the whole line separated by space this is a line which would have spaces in the next example we'll see how can we split string by custom delimiter so for that purpose let's first clear the terminal and we'll write some lines like this now in this we have supposed that our delimiter is exclamation mark so we are going to split the string on the base of exclamation mark using the while loop like this so let's save the file and run the script to see the output as you can see that we have split the string using a custom telemeter all right now let's move to our next example in which we'll see how can we split string into array variables in this example we'll split the string into the array variables we will apply a for loop over that array variable and then echo out every word so let's save the file and run the script to see the output as you can see it has splitted string into array variables and echo out every word all right now let's move to our next example in which we'll see how can we split string by multi byte delimiter for this example i'll write code first and then explain it later this is a bit lengthy code let me zoom out a little bit to understand it better and let me shorten the terminal window as well now in this example we are going to pass through the whole line using the while loop while i is less than line length and if telemeter is equivalent length then keep on merging array and after getting done with the while loop declare dashb myarray let's save the file and run the script to see the output as you can see it has splitted string by multiply telemeter all right that's good now let's clear the terminal and clear the bash example file as well to try our next example in which we'll see that how can we use patch command line option with position for that purpose we'll first echo out dollar one and dollar two like first and second position and then we'll run the script with the two options so first let's echo dollar one and dollar two now let's save the file and run the script with the two values like john toe as you can see it has printed out first and second position values similarly in the next example we'll see that how can we pass command line option with shift command so for that purpose we'll first write shift command and then echo 0 1 2 and 3. after writing this line save the file and run the script with options like bmw honda and toyota now when i hit enter it will print out the options all right now let's move to our next example in this example we are going to see how can we use past command line using get opt for this example i am going to paste some code to which i will explain it later now using this code i am going to parse command line using get opt get opt's command is supposed to be used inside a while loop so all options are passed then immediately after the get operated keywords we have to find the options that our script will accept they are x y and z now if we save the file and run the script with the dash x it will print out this is x and if we go with the dash y it will print this is y now if we go with the dash set we need to provide arguments as well like john and it will print the value provided is john and if we go for the value except x y and z like dash a it will print illegal option dash dash a in the next example we are going to see how can we read password from stdin without printing it we will intentionally print it but while we will type it won't appear after writing this code let's save the file and run the script it will ask me to enter a password so let's type linux123 as you can see it didn't show me anything but when i hit enter it will echo thanks for putting in your password which is linux123 awesome let's move to our next example for trying this example we'll first have a call.txt file in which we'll have the name of the cars now after creating this card.txt let's move to the bash example.ss file in which we will write cat card.txt pipe sort now after saving this file let's run the script as you can see it is sorted using the pipe syntax similarly let's take a look at another example cat pipe word count how can we do that so we can do that simply writing wc dash w and now if i save the file and run the script again it will print out five that's great let's move to our next example in this example we are going to see how can we use base script to read from stdin and send data using pi so i will write a code first and explain it later now in this example we are reading every line from the core dot txt using while loop and then we are copying it to the new dot txt so let's save the file and if we take a look at our directory using ls we can see that there are only three files bash examples card.txt and file.txt and now if we run the script after running the script if i take a look at the directory again using ls we have a new dot txt and now if we get new dot txt it will have all the card names that's correct all right now let's move to the next example in this example we're going to see how can we echo to bc command for a simple math expression so i will write a little bit of code first and explain it later now in this example i have used five simple math expressions such as addition subtraction multiplication division and modulus so let's save the file and run the script we will definitely have the results 60 4500 5 and oh awesome let's move to the next example in this example we are going to see how can we equal to pc command for a more complex math expression so let me write a code first and explain it later now in this example i've used more complex math expression using basic mod so let's save the file and run the script we'll definitely have a complex result all right now let's move to the next example in this example we are going to see how can we print a common text using function and then call that function multiple times so let me write a function for echo something in it and then call it multiple times all right in this example i have simply created a function first and then call it multiple times let's save the file and see the output in the terminal as you can see it has printed hello this is a text three times that's great let's move to our next example now in this example we're going to see how can we use a function which returns a string so let's create a function which returns a string all right now in this example i have created a function in which i have returned a string hello this is a text but after creating the function i have created a variable with the name rut str and in which i have assigned it a string bash is excellent echo that out then i call the function and later i will echo it out to see the difference in the return string so let's save the file and run the script as you can see it has first printed bash is excellent from here and then after the function call it has echoed out hello this is a text that's awesome let's move to the next example in this example we are going to see how can a function takes parameter and returns sum so let's create a function in which will return a sum now in this example i have created a function in which i have added two variables five and four here and then i echoed out sum is sum five plus four should be the nine so let's save the file and run the script as you can see it has printed out some s9 that's great now let's move to our next example in this example we are going to see how can we use tablet command to print string in this example we are simply printing a string in bold so let's save the file and run the script to say the output as you can see it has printed the string in bold characters let's move to the next example in this example we are going to see how can we use depot command to print numbers like we want to print the columns of the terminal let's save the file and see the output as you can see it has printed out the columns of that terminal 123. let's move to the next example in this example we are going to see how can we use tripod command to move cursor in terminal to arbitrarily location now in this example we have simply moved the cursor 10 from the top and 10 from the left so let's save the file and see as you can see the terminal is moved a little bit right and 10 rows from the top that's good let's clear the terminal move to the next example in this example we are going to see how can we use tribute command to clear the terminal now in this example when i will run script in the terminal the terminal will get clear so let's see save the file and run the script as you can see it has cleared the terminal now let's move to the next example in this example we are going to see how can we use t port to print attributes of terminal now this script will simply print attributes of terminal so let's save the file and see the output in the terminal that's it that's how we can use deport to print attributes of terminal let's clear the terminal and move to the next example in this example we're going to see how can we use t port to print more complex attributes of terminal in this script we are simply printing out lines columns and colors of the terminals let's save the file and run the script to see the output as you can see it has printed out more results let's clear the terminal and move to the next example in this example we are going to see how can we assign string variable with declare in this script we have simply declared a variable and then echo it out let's save the file and see the output in the terminal by running the script as you can see it has printed out the string variable with declare let's move to the next example in this example we'll check if variable created will declare or not now in this example we have simply declared a variable and then equate out and in the if statement we have checked if it is declared or not if war is not declared then echo war is not declared else echo war is declared and set to the variable whatever we have assigned it like string variable will declare so let's save the file and run the script as you can see it has printed out first the string string variable with declared and later from the else block it has printed what is declared and set to the string variable with declare awesome let's move to the next example in this example we are going to see how can we use an option for declare to name ref alias in this example we have first assigned a to the bar variable and then assigned the part to the 4 using declare and then echo 4 and bar and later we have assigned b to the 4 and then again account 4 and bar it will first print a a and then b b so let's see save the file and run the script in the terminal as you can see it has printed out a a and b b this is how we can use n options for declare to name wrap aliases so let's clear the terminal and move to the next example in this example we are going to see how can we force variable to integer with declare in this example we have first assigned 2 to the string variable and then using declare we have force and integer to the variable and then echo string so obviously it should print 3 so let's save the file and run the script to see the output awesome this is how we can force integer to the variable with declare let's clear the terminal and move to the next example in this example we are going to see how can we force case with declare in this example we have just forced the case with declare so let's save the file and see the output it has printed four so this is how we can force case with declare and using another variable inside case all right now let's clear the terminal and move to the next example in this example we are going to see how can we declare a read-only variable in this example we have just declared a variable author with the read only dash r and assign linux into it and later we have changed it to someone else but when we echo out author it should print linux in because it is read only so let's save the file and run the script and to see the output as you can see it has shown first that line for author read-only variable and then it printed linux and awesome so this is how we can declare a read-only variable let's clear the terminal and move to the next example in this example we're going to see how can we use indexed arrays with declare now in this example we have just declared an indexed array and then access zeroth index and all the indexes so let's save the file and run the script and the output should be the zeroth index linux and linux ubuntu linux in the second echo that's great so let's clear the terminal and move to the next example in this example we're going to see how can we make an associative array with declare in this simple example we have declared an associativity and then assess values let's save the file and see the output it should print the channel name oh sorry the website name first and then channel name youtube and linux awesome let's clear the terminal and move to the next example in this example we are going to see how can we automatically answer question with a yes command so we are going to copy the file.txt content to the new dot txt and override with automatically yes answer now if we save the file and run the script it will ask for the overriding privileges but it will automatically answer yes as you can see it is done so let's clear the terminal and move to the next example in this example we are going to see how can we use and operator to run second command only first succeed so we are going to declare two variables and then we are going to write an if statement with an and operator and echo either condition passed or get failed now in this example i have hardcoded the variable values to the 4 and 2. sorry it's s2 okay now in this condition s1 is greater than s2 it's true but it is not s1 is not equal to 3 so in case if this condition gets true but it will still echo out the condition failed so let's save the file and run the script as you can see condition is failed now if we change the variable s1 to the s3 now the condition will get passed awesome let's clear the terminal and move to the next example in this example we're going to see how can we use auto printer to run second command only if first fails so instead of writing and end we are going to write r operator with the pi syntax in this example i have assigned 3 to the s1 and 4 to the s2 which means s1 is smaller than the s4 and the first condition of the if statement will get failed and it will go for the second statement and print condition pass so let's save the file and run the script as you can see it passed the condition on the base of second argument because it was fast let's clear the terminal and move to the next script in this example we are going to see how can we use xox to operate on all files in directory in this example we are listing out all the files with the name of file file and then we are printing word counts so let's save the file and run the script as you can see there was only one file in this directory so it has printed out 3 16 and 82 with the file.txt all right now let's clear the terminal and move to the next script in this example we are going to see how can we wait for another command to complete in this example we are going to wait for the process id first and then echo these two commands now if we save the file and run the script it will first acquire the first two statements and then the last two statements but if i comment out this wait line you will notice that the order will not be the same it has first printed the testing weight command 1 from here and then it has echoed out these two statements instead of this so this means that weight command is waiting for the process id to get finished first and then echo these two commands all right so now let's create the terminal and move to the next example in this example we're going to see how can we combine kill and weight commands in this example we have simply used kill command with the weight commands let's save the file and run the script as you can see it has simply printed out testing weight command and pid which was 316524 was terminated so this was just a basic demonstration that how can we use kill and weight command combined let's clear the terminal and move to the next example in this example we are going to see how can we use wildcard on file selection so let's first create some files in the directory with the file1 file2 and filezname as you can see i have created three files in the same directory and if i ls in the terminal you can see the files are here as well now in order to use wildcard on file selection we'll write the script like this now this script will list down all the files with the name of fili either that is file 1 file 2 file 0 file.txt so let's save the script and run the script in the terminal as you can see it has printed out four files file one two file.txt and file z also let's clear the terminal and move to the next script in this example we are going to see how can we use question mark wildcard on file selection for example we don't know the character of any file somewhere in between so we can use a question mark instead of that character like if we want to search for a file c but we don't know either it is f i l or something a b c or e so we use question mark and then write c so let's save the script and run the script in the terminal as you can see it has printed out file c awesome let's clear the terminal and move to the next script in which we're going to see how can we use scale packets on file selection for example we want to search for the file 1 and file 2 so we can use file e and then square brackets with 1 and 2. now it will print the both file 1 and file 2 file as you can see in the terminal after running the script it has printed out two files now let's clear the terminal and move to the next script in this example we are going to see how can we use pipe for file selection option so we will list down files whose name starts from either f to let's say z in this example we are first listing out the files whose name start from file to z onwards and then we are printing the word count using the pipe syntax so let's save the file and run the script as you can see it has printed out five in this example we are first listing of the files whose name start from f2h and then we are printing word count of that file so let's save the file and run the script as you can see it has printed r4 because there are only four lines in that file and there is nothing in file 1 or file 2 so it has printed for now let's clear the terminal and move to the next script in this example we are going to see how can we use brace extension comma separated list now if i save the file and run the script it will print out linux 1 linux 2 linux 3 and linux aliens mailing c awesome now let's clear the terminal and move to the next example in this example we're going to see how can we use brace expansion range now in this example we are first echoing out letters from a to e then m to z and 1 to 12. so let's save the file and run the script as you can see it has printed out a b c and d then m1 to the m12 and then n1 to the n12 and onwards to the z awesome so let's clear the terminal and move to the next script in this example we are going to see how can we use brace expansion with preamble now if we save the file and run the script it will print out file 1a file 1b file 1c and same goes for the file 2. as you can see in the output it goes perfectly fine and let's clear the terminal and move to the next script in this example we're going to see how can we perform operations on list of files with brace expansion now in this example we are making three directories four five and six and then listing it down so let's save the file and run the script it will first create three directories and then list them down as you can see now we have three more directories in the same directory awesome let's clear the terminal and move to the next script in this example we are going to see how can we reference home directory using tilde so it is very simple we will echo with the tilde so if i save the file it will obviously acquire the home directory home slash linux user awesome now let's clear the terminal and move to the next script in this example we're going to see how can we use parameter expansion from printing constant or variable if unset so the syntax for the constant or variable if one set is this so let's save the file and run the script to print the constant or variable unset awesome now let's clear the terminal and move to the next script in which we are going to see how can we use substring expansion for printing part of a string in this example we are first echoing out the whole variable and then we are going to echo star war from 0 to 6 only united part okay so let's save the file and run the script as you can see in the output it has first printed out the whole variable and then from 0th index to the sixth index only so let's clear the terminal and move to the next script in which we are going to see how can we use parameter expansion for assigning constant to a variable if set now in this example we are simply assigning a value to a variable or constant if unset so let's save the file and run the script you can see we have assigned a value successfully so let's go create the terminal and move to the next script now in this example we're going to see how can we check a file executable and set if not now in this example we have simply just assigned a file one to the file variable and then check either it is executable or not using if statement if it is executable will acquire file is executable otherwise will echo file is not executable then change its mode to the executable mode and then print file is executable now of course file one is not executable so it will go for the else block so let's save the file and trend the script as you can see file was not executable but it later make it executable now if i list down the files in the directory using dash l command it will show that it is executable now awesome so let's clear the terminal and move to the next script now in this example we're going to see that how can we check if file is owned by user and set if not so for that purpose we'll simply just check using dash o command and let's change the statements as well and in order to give privileges to the user we'll use this command sudo chwn linux user file so let's save the file and run the script since i have created the file so it will go for the echo file is owned by the currently login user save the file and run the script from the terminal as you can see it has printed out file is owned by the currently login user so let's clear the terminal and move to the next example in this example we're going to see how can we use wc command to count lines in a file in this example we are simply printing out the word count of the file file.txt so let's save the file and run the script it has printed out that number of lines in the file are three let's clear the terminal and move to the next example in which we are going to see how can we use wc command to count characters in a file so for counting lines we use dash l in order to count characters we'll use dash c simple let's save the file and run the script it will print out the number of characters 82 in the file.txt awesome now let's clear the terminal and move to the next example in which we are going to see how can we combine ls and wc using pipe to count files in a directory so let's save the file and run the script to have an output like this so this is how we can use ls and wc command and combine them using a pipe so let's clear the terminal and move to the next script in which we are going to see how can we use head command with the default arguments in this example we have assigned file.txt to a file variable and then we are echoing out file using the head command and with the sudo privileges so let's save the file and run the script it will first ask me for the password of linux user and then i got the four lines of the file.txt awesome let's clear the terminal and move to the next example in which we are going to see how can we use head command to print first two or three or n number of lines so we will simply put dash n number of lines and then number of lines in the digital form like two so let's save the file and run the script as you can see it has only printed out the first two lines so let me clear the terminal and move to the next example in which we are going to see how can we use tail command with default arguments so instead of head we'll use tail now so let's save the file and run the script it will print out the four lines again but in the next example we are going to see that how can we use tail command to print the last two or three or n number of lines so just like for the head we're going to use tail dash n and two lines so let's save the file and run the script as you can see it has printed out the last two lines that's great now let's clear the terminal and move to the next script in which we are going to see how can we use find command to locate matching pattern files there can be multiple scenarios that you don't remember the total file name like you know the first two or three characters of the last two or three characters so we'll try all of them like if we know the file name we can do like this it will obviously print out the file.txt so let's save the file and run the script awesome now for example you don't remember the last two characters of the file so instead of writing le will use static now let's save the file and run the script it will still print out the file.txt awesome now if we don't remember the first two characters so we can use static in the start as well let's save the file and run the script that will print file.txt now if we just only remember the i and we don't know either the start was f so we'll use static i and static so let's save the file and run the script it will still print file.txt awesome let me clear the terminal and move to the next example in which we are going to see how can we use fight command to locate files based on date now in this example we are first finding out the file one from the directory and then using the grep command and giving it the argument of october 28 it will print out every file one in the same directory if it is generated on the october 28th so let's save the file and run the script it will print out only the file one which is generated today october 28th awesome now let's create the terminal and move to the next craft in which we are going to see how can we use cut command to parse telemeter columns of data so in order to try this example let's open up the file.txt and make columns in this file using tab so let's save the file.txt and go back to the bash examples.sh in which we are going to write cut command to parse delimiter columns of your data like the fourth column now if we save the file and run the script in the terminal it will only print out the fourth column from the file.txt is linux and fourth line is it yes and if you want to give a range like two to three so we can do it like this now if we save the file and run the script it will print out the second and third column from the file dot txt as you can see here awesome now let's clear the terminal and move to the next example in this example we are going to see how can we use grep command to search for a pattern so in the terminal we will first use the cat command to see the file.txt okay this is what we have in the file.txt now let's use the grub command to search for the somewhere as you can see it has searched for the word sum from the something and printed it out all right now let me clear the terminal and move to the next example in which we'll see how can we use crep command to search for a lack of pattern to search for a lack of pattern using the grep command we'll use dash v like this now let me open up the file.txt here as you can see all the three other lines include is so it has only printed the first line in which there is no is that's great let me clear the terminal and move to the next example in which we'll see how can we use gap command to search for case insensitive pattern for that purpose we'll use dash i for example we want to search for the word linux although it is in capital letter but we'll search it using that small letter but with the dash i okay it worked perfectly fine but if i search for the word linux instead of using dash i obviously it won't get any result so dash i used to search for case insensitive pattern let me clear the terminal and move to the next example in which we'll see how can we use grep command with wildcard for example in file.txt we want to search for that this but we just remember the first two letters and don't know what's next to it so let's search for this using the static syntax as you can see in the output it has only printed out the three lines which include th i awesome let's clear the terminal and move to the next example in which we'll see how can we use grep command to search all files in directory recursive in our previous example of crap command we are just searching in the single file file.txt now as you know we have another file new.txt which includes same lines so to search all the files in directory we'll use dash r with the grep command as you can see in the output it has printed out three lines from each of the file from the file.txt as well as from the new dot txt awesome now let's create the terminal and move to the next example in which we'll see how can we use grep command to search for file in directory like we search for a pattern in a file and get the output of the file name as you can see from the whole directory there are only two files which includes this static file.txt and new.txt so it has printed out only two files awesome let's clear the terminal and move to the next example in which we'll see how can we use variables in awk so for that purpose the syntax is very simple in this we have created a variable with the name of site and assigned it a value linux in and then print it so let's save the file and run the script in the terminal as you can see it has printed out the variable site which includes linux end awesome now let's clear the terminal and move to the next example in which we'll see how does it blk helps in splitting on wide spaces so basically awk offers built-in dollar one for the first word dollar two for the second word and dollar three for the third word in a line so if we want to print writing something here we can do like this now in this we have simply printed out the second and third index values which is something and here all right now let's save the file and run the script on the terminal as you can see it has printed out something here here right here this is and is the from every line of the file second and third index all right now let's clear the terminal and move to the next example in which we'll see how does awk helps to print last field in each line for printing the last field of any line we can simply use dollar nf for that now let's save the file and run the script as you can see we have only got the last columns of every line here is is linux intent line all right that's great let's clear the terminal and move to the next example in which we'll see how can we use awk and use custom delimiter for that purpose we'll run the script now in this simple example we are just replacing the space with the dash now let's save the file and run the script in the terminal as you can see in the output every space is replaced by the dash all right let's clear the terminal and move to the next example in this example we see how can we use awk to print only match of rejects so for example from the file.txt we are going to search for hair using regex so the script would be like this now let's save the file and run the script as you can see it has only printed out three lines which include here awesome let's clear the terminal and move to the next example in which we'll see how can we use awk with if else condition so for that purpose we'll run this script now in this example we're going through each line of the file file.txt and checking if at the first index the word is writing then print yes it is otherwise print no it is not so for these five lines it should print yes it is no it is not notice not and so on now let's save the file and run the script in the terminal as you can see it has printed out yes it is no it is not and so on awesome let's create that terminal and move to the next example in which we'll see how can we use awk with ternary operator so instead of writing this long code we can make it shorter using the ternary operator like this now in this we are doing the same task we are first checking if at the first index the word is writing then print yes it is otherwise print node is not so let's save the file and run the script in the terminal to have an output as you can see we have got the same output awesome let's clear the terminal and move to the next example in which we are going to see how can we use set command to replace matching text so for that purpose we will run this command now in this simple example we are just replacing here with the where in the file.txt so let's save the file and run the script as you can see here is replaced by the ware everywhere that's great now let's clear the terminal and move to the next example in which we'll see how can we use set to replace second instance of matching text online so in the file.txt we are going to replace is with the where all right because we have two instances in a single line so for that purpose let's write is here and write two here for the purpose that we want to change the second instance all right let's save the file and run the script as you can see the first is is same but the second is is changed to where awesome now let's clear the terminal and move to the next example in which we'll see how can we use set command to delete specific line for example we want to delete the second line from the file.txt this one so we'll run this command set to d default delete from the file.txt now if we save the file and run the script as you can see the second line is deleted awesome let's clear the terminal and move to the next example in which we'll see how can we use set to add a line before matched line for example we want to add a line before this writing something here so we'll search for the writing using the rejects and add the before word before this line so for that purpose we'll run the script now in this example we are simply just searching for the writing using the regex and then adding a line before the match line so let's save the file and run the script as you can see the before word is included before the writing something hairline awesome now similarly in the next example we'll see how can we use set to add a line after match line so for that purpose we'll use a instead of i here and replace the word let's say after now let's save the file and see the output as you can see the afterward is added after the line writing something here awesome now let's clear the terminal and move to the next example in which we'll see how can we use sort command to sort data in a file so for example we have a code.txt file here in which we have the name of the cards and we want to sort it out so to sort the data in this file we'll run this script sort car.txt as you can see the name of the cards are sorted out perfectly awesome now let's clear the terminal and move to the next example in which we'll see how can we sort data by ignoring the case so for that purpose let's create a new file in which we'll have case insensitive items alright after creating this file let's sort it out using the dash f lag with the sort command as you can see the case.txt file is sorted out perfectly all right now let's clear the terminal and move to the next example in which we'll see how can we use sort command to sort the data numerically alright so for that purpose let's edit a case.txt file and enter some numerical values here all right after creating this file and entering some numerical values let's save the file and sort it using the dash and flag with the sort command now as you can see the case.txt file is sorted out numerically perfectly fine also now let's build that terminal and move to our next example in which we'll see how can we print unique data with unique command now basically the unique command is used to filter out the repeated lines in the file like this so if we print the file.txt using the unique command it will print out only four lines instead of five for example as you can see it has printed out only four lines as i said before now if you want to print the count as well so you can use dash c flag with this as you can see it has printed that this line is repeated two times now if you want to print only the repeated line instead of all others we can use the d flag as well as you can see it has only printed out the repeated line that's great now let's clear the terminal and move to the next example all right so now let's move on to our next script the name of the script is count unique lines in file with short unique and wc command in this script we will see the use of these three commands that is sort unique and wc so this is our script first of all what we have to do we have to enter the file name where we have stored the data all right so here we are reading the file name and we have applied the if condition here if hyphen f file name it means that it will check if the file name exists then it will return true otherwise it will return false so hyphen flag is used to check the existence of file if the file exists then what it has to do it has to apply the sort command on the file name so sort command will basically sort out all the data into the file after that we have applied the unique command which will basically count the unique words in a file so this is our script let me just copy this script into our script dot asset file this is our script dot asset file and if the file does not exist what it will do it will execute the else command and it will print on the terminal the file does not exist let's open up our terminal this is our terminal let's execute our script enter the file name we have a file file dot txt in which i have stored the data so all right it is basically first of all sorting all the data into the file then it is counting the unique lines so linuxin appears three times in the file welcome appears one time in the file hello appears one time in the file bmw appears one time and so on audi so this is how you can sort your data in a file and you can print the unique data all right now let's move on to our next script the name of the next script is convert data to the upper case with tr comma the tr command is used to convert the data into the upper case or lower case if all the data is in the lower case then we can convert all the data into the upper case and vice versa the use of the tr command is very easy in script so let's see that how we can use it this is our script let me just copy this script into our script dot sh file and i will explain all these steps one by one so first of all we have to enter the file name and then we have applied the if condition to check the existence of the file and if the file exists then we have to apply the tr command so what the tr command will do this is the syntax of writing the tr command tr small a to z then it has to convert all the data into the capital letters so first we have to write here small a to z and then we have to write capital a to z in order to convert all the data into the capital letters and here we have specified the name of the file as well because it has to read all the data from this file so if the file does not exist it will execute the else command and it will print on the terminal the file does not exist all right this is our file.txt in which you can see that all the data is in the small letters so now let's go to our terminal and try to execute our script dot asset the name of the file is file.txt and you can see that it has converted all the data into the capital letters hello welcome linux hint bmw rd and suzuki so this is the conversion of the data into the capital letters all right now let's move on to our next script which is convert data to lowercase with tr command previously we have converted all the data into the upper case letters now we have to convert all the data into the lowercase letters all the things will remain same in our script dot sh file but here we have to change the first argument of the tr command so if all the data is in capital letters or upper case letters so what it has to do it has simply converted into the lowercase letters all right so this is the minor change that we have to do in our script so let's go to our terminal again and try to execute our script all right file.txt so you can see that it has printed all the data into the lowercase letters although all the data is into the lowercase letters in file.txt let me just put some data in uppercase letters so we can notice the change as well so here in file.txt some of the data is now in uppercase letters and you will see that it will convert all the data into the lowercase letters all right file.txt and you can see that it has converted the uppercase hello letters into the lowercase i hope you got my point all right now let's move on to our next script the name of the script is create a tar file so in this example we will learn that how we can create the car file by using the tar command it is super easy to perform the job so let us go back to our script dot insert file all right so this is the script to create a tar file first of all we have to specify the name of the file that we want to convert into the tar file so here we are getting the name of the file from the user so if the file name exist then we have applied the tar command here the tar command is used to create the tar file so when we have to create the tar file we have to write a tar hyphen cv zf this is the option that we have to specify here so what it will do it will create the file with the extension dot tar and the name of the tar file will be that name which will be entered by the user for example user enters file dot txt so the tar file will be file dot txt dot or file.tar so lastly it has to remove the file which is entered by the user so when we have created the tar file there is no need to store that file the previous file into your system all right so this is the basic syntax of using the tar command so in the else condition we are printing on the terminal file not found if the user enters some file which does not exist in the given directory so let me just say my script dot acet file and let's go back to our terminal cd desktop script dot sh so on my desktop i have multiple files so i have a file here whose name is code dot txt and let's create a tar file of that all right now you can see that it has created the tar file of code.txt and it has removed the original code.txt5 so that is how you can create the tar files by using the bash commands from your script so i hope you got my point now let's move on to our next script the name of the script is create a zipped star file so previously we created the tar file but now we have to create a zipped jar file so there is nothing much difference while creating the tar files and zip tar files all the things will remain same we have to check for the file existence first then we have to write the tar command into our script like that and the options will remain same but the file extension will change now previously the file extension was dot tar but when you have to create the zip tar file the extension would be dot dot gz so we have to write only gc in our previous command and it will do the job all right so we have a file here output.txt let's create a zip tar file output dot txt and you can see that it has created a zipped door file for that that is output dot dot gz so that is all about creating the zip tar file i hope you got my point all right so now let's move on to our next script the name of our script is unsuper compressed star file previously we discussed about creating the tar file but now we will see that how we can unzip a compressed r file so this is our script for unzipping a compressed r file first of all we are printing here on the terminal enter the file name because we require the file in order to unzip it in the if condition we are checking if the file exists then what it has to do it has to unzip the file to unzip the file we have to use the car command with the hyphen x z f option so here we have written star hyphen x cf and after that we have written the file name so it will unzip the compressed r file and if it does not find the file then it will print on the terminal that the file not found so let's copy our script into our script dot sh file and try to execute our script cd desktop all right script dot sh on desktop we have a compressed her file the name of the file is code dot txt dot dot all right so let's hit enter and you can see that it has unzipped the compressed her file and we have code.txt file here on my desktop so this is the idea of unzipping the compressed r file you have to use the tar command for this purpose but with the option hyphen xcf it will do the job so i hope you got my point all right so now let's move on to our next script the name of our script is pass json file with jq json is a popular data format it is used to store the data and exchange the data between your server and your software applications for example i have a student data so i can store it into the json format and later on i can exchange it with my server or software application so json is the abbreviation of javascript object notation the whole data is stored in the form of the object so passing the data is referred to reading the data so for this purpose we use the jq command in bash for passing the json file so let's see that how we can pass the json file with jq command in bash all right so this is our code to pass the json file with jq command i'm gonna paste this code in our script dot sh file so first of all what we have to do we have to enter the name of the file it will read the file name and it will check if the file exists and if the file exists then it will print the content of the file on the terminal using the cat command and after that we have jq command the jq command will pass our json file so when we say about parsing the file it means that we are reading the data from the json file so this jq command will present the data on the terminal in a well formatted way so that we can read it in a good way and if the file does not exist then it will print here the file not found so this is our jq command to pass the js1 file let's go back to our terminal and try to execute our script dot sh so on desktop i have a file a json file the name of the file is example.json so initially you can see that the data in the example.json file is in this way all right so we have to pass it and we have to read the data so the name of the file is example.json so all right you can see that now it passes the data and it prints its content on the terminal so this is the jq command if you do not have installed the jq command in your linux system then it will show an error so make sure that you install the jq command first you can install it by using suto apt hyphen get install jq command all right so when you will hit enter it will install the jq command in your linux system i hope you got my point now let's move on to our next script print a specific field in json with the jq command previously we see that how we can print the content of the json file with the jq command and in the json file we have multiple fields so when we have to print a specific field or we have to fetch the data over a specific field then we can do it with the jq command so for example there are multiple sections in this json file quiz match and q2 these are multiple sections what if i have to get a specific data like q2 question or options so for getting any specific field we have to write the jq command like this this is our code let me just copy it into the script dot sh file so first of all we have to enter the file name it will read the file name and it will check whether the file exists or not so if the file exists and we want to fetch this data quiz sport question 1 question so if we want to fetch this data that what is the question number one in the sports section then we have to write it in this way like quiz dot sport dot question one dot question or it could be any specific field so the idea is that you have to separate the different sections of the json with the dot quiz dot sport dot q1 dot question so it will return us this question which one is correct team name in nba and we are choosing the jq command here to pass this data so let's go back to our terminal and try to execute our script example dot json all right so now it returns us a specific field that is question which one is correct team name in nba and for example if we want to get this field quiz.maths.q1 so we have to change our script like this quiz dot math dot q1 dot question so now it will return us the first question of the math section so 5 plus 7 is equal to so this is the first question as you can see here in example.json file so this is how you can print the specific field data in json all right so that was all about passing the json file data now let's move on to our next script which is about printing the cpu information with sudo file system a sudo file system is a temporary file system in any linux based operating system when we own our operating system or our laptop or computer then this soda file system is created and it stores the information of our cpu our memory and the other things this pseudofile system is also called as the proc system it is accessible by using the backslash proc so in this script we will see that how we can print the cpu info with the sudo file system so remember that the sudo file system is the proc system it is a temporary system whenever you will own your computer this system will be created and when you will shut down your system all the information of this system will vanish all right so this is our command to print the cpu information of sudo file system let me just paste this command here in our script dot sh all right as i described earlier that the pseudo file system is also known as proc system so slash proc and the brock holds the information of your memory your cpu your processes and other things as well so cat cat command will basically print the content on the terminal cat slash proc slash cpu info it will return us the cpu information so let's go to our terminal and try to execute our script dot asset file so this is the cpu information the processors are 0 the vendor id is genuine intel cpu family is 6 model is 58 model name is intel our core i7 and the cpu is 2.00 gigahertz and the stepping is nine so all the information of the cpu is printed on my terminal so you can also see the address size here 36 bits physical and 48 bits virtual it also shows the cl flush size that is 64 and cache alignment that is 64. so physical id of the cpu is 0 siblings 1 core id 0 cpu course is equal to 1. so this is how you can get the information of cpu by sudo file system i hope you got my point all right so now let's move on to our next script which is print the memory info with sudo file system previously we printed the information of cpu and now we have to print the memory information with sudo file system so in order to print the memory information we simply have to replace the cpu with mem info mem refers to the memory all right so this is now our command let's go back to our terminal and clear our terminal all right script dot sh and now it will print the memory information the total memory of the system is this kilobytes the free memory is this the available memory is this these are the total buffers cache and all the memory information by the pseudo file system is printed on my terminal so what you have to do you simply have to write a command slash proc slash memory info so that was all about this bash example or batch script i hope you got my point now let's move on to our next script which is print mounts with sudo file system as we know that slash prop system is known as sudo file system when we turn on our operating system then the sudo file system is formed and when we shut down our system the sudo file system vanish so now let's see that how we have to print the mouse with pseudo file system mount command is used to bind your hard disk drives or any storage device with your file system so whenever you have to use any device or any storage device first we have to mount it with the system using the mount command so now in this script we are gonna print all the mouse devices with the sudo file system let's go to our script this is our code for printing the mouse with the sudo file system it is stored in the slash proc system so cat slash proc slash mouse it will print the mount with the sudo file system i have copied this script in my script dot sh file so let's go back to our terminal and try to execute our script script dot sh all right so it will show all the information of the devices that are mounted with the sudo file system for example dev pts dev pts and it is showing all the information of the mounted devices on your terminal so this is how you can print the mouse with sudo file system i hope you got my point now let's move on to our next script print network stats with the sudo file system the network stats show that what are the networking devices that are connected with our operating system and how the data is going how how much data we have uploaded and received so the sudo file system maintains a directory that is slash net inside the slash net directory we have netstat command the netstat command do the job and it print out all the network stats on the terminal so let me just open a terminal here and let's move on to slash proc slash net directory so if we ls here you can see that here we have a command net stat so netstat let's run this command excuse me for this all right so now it shows all the devices network devices on my terminal and the prints all the network stats so the first protocol that our operating system is using is tcp the received data is zero the send data is zero the local address is linuxind and this is the port number this is the foreign address this is the state it is in waiting state tcp on this local address with this foreign address is established and similarly we have multiple devices here so this is how you can print the network stats on your terminal using the sudo file system so let's try to run this in our batch script this is our script so i'm gonna copy it into my script dot sh file and let's go back to our terminal so here you can see that cat cat will basically print the content slash block slash net slash netstat all right so script dot sh so it has printed all the information on my terminal so i hope you got my point that how you have to print the network stats with sudo file system and that's it for this script all right so now let's move on to our next script the disk usage with due print disk usage with d2 command the dq command is referred to as disk usage command this command prints that how much disk is used by all the processes files and systems it is also used to track all these files that are using the excessive memory or excessive disk usage so let's see that how we can use the dj command to print the disk usage the dq command is the short form for disk usage it is used to estimate file space usage that issue command can be used to track the files and directories which are consuming excessive amount of space on hard disk drive so let's see an example of this script that how we can use the d2 command in order to print the disk usage all right so this is our command to print the disk usage let me just copy it into the script dot sh file so d u d u is the command that we will use so slash home slash linux in slash documents so it will print the disk usage of particular directory that is documents directory and if i remove it documents directory so now it will print the disk usage of my whole system the name of my system is linux synced so it will print now the disk usage of whole system script dot sh and you can see that the output is too big it is not readable in human way it is not easily readable so now let's write here the name of specific directory so that we can get the sufficient output to read all right so here you can see that my directory have four blocks the bash scripting ebook slash images have the ten thousand three zero eight blocks all right so the data is not printed in the human readable way but it has displayed the total disk usage of specific directory and similarly if i can write here documents instead of downloads directory so if i execute my script now you can see that this is the information that i have got from the documents directory this is the total usage so there are multiple options that we can use with the disk usage command for example hyphen s will present the summary of the total disk usage if i write here hyphen s and execute our script again so this is the summary i hope you got my point our next script is print disk usage in human readable way with dq command so previously you saw that the disk usage was printed in 108 10323 it was not readable by human we were not able to understand that what this output actually is because the human can understand the output of the disk usage in kilobytes megabytes or gigabytes so let's see that how we can print the disk usage in human readable way with the deq command so as i stated earlier that we can use multiple options with the tu command as we use the hyphen s option to summarize all the information so to print the output of the d2 in human readable way we have to use this option hyphen h it will print all the information in human readable way so all right let's go back to our terminal and let's see that what is the output of this so now it is a pretty much clear that the documents directly have a total disk usage of 108 kilobytes and if i remove this documents directory so we will get the information about our whole system all right so now you can see that it it shows all the information in kilobytes and megabytes for example the home linux in bash bash script ebook it contains 12 megabytes and similarly for the downloads directory downloads directory it will show the information if we so let's execute our script and now it shows the information that the patch scripting ebook images folder have 11 megabytes and all these things so i hope you got my point that when you have to get the output of the u command in human readable way then you have to use hyphen h option all right so now let's move on to our next script create file of specified size with dd command the dt command is used to create the files or we can also create the file with the touch command but it is not the topic here the topic is the dt command so dt command is used to create the files and we can specify the size while creating the file that it should not exceed 1 megabytes or 2 megabytes and we also have to mention that how much blocks of memory it should create so let's see this script all right this is our script let me just copy this in script dot sh file all right so this is our script first of all we have written here dd dd is our command and it will create this in slash dev slash zero the off option is used to define here the name of the file that is my file dot txt and the count refers here to the number of blocks that how much blocks will be created for this file in memory so i just want to create one block and the bs refers to the size of the file and it is the abbreviation of block size so i have written here 1 0 2 4 it means that a file of one kilobytes will be created with the count if i write here one zero two four so it will create one zero two four blocks of memory and one byte will be stored in one count so i don't want to do that i just want to store it into the one block and here we have specified the size if you want to create two kilobytes file then you can write here two 0 4 8 now it will create a file of 2 megabytes all right so now let's execute our script the size that we have defined is 2048 it means that the file size should be 2 kb all right this is our my file dot txt go to properties and you can see that the size is exactly 2 kilobytes so this is how you can create a file of specified size with ddd command all right so now let's move on to our next script our next script is time disk writing with dd and time in the previous script we created a text file with the dd command of specified size and in this script we will also notice the time of file writing the dd command by default shows the time on the terminal which it takes to write the file so for example if we run our previous script cd desktop script dot sh so now you can see that it is the time here 0.018 so this time is in seconds that is taken by the dd command to write this file but we can also add here o flag which is equal to dsync so this option basically slow down the writing process of the file and the time value will increase so let's go back to our terminal and try to execute our script again so you can see that it shows here one plus zero records in one plus zero records out two zero four eight bytes it means that a file of two kbs is created in 0.015 seconds so this is how you can use the dd command to notice the time profile writing so let's move on to our next script it is about send a mail with mail program in this script we will see that how we can send an email through our bash script ssmtp is the protocol that is used to deliver the email from your computer to the mail hub so first of all we have to install ssmtp protocol on our linux system so open up your terminal and write your sudo apt install ssmtp enter your pseudo password so ssmtp is already installed on my system but if you are installing it for the first time then it will take a couple of minutes so after installing the ssmtp we have to open the configuration file of ssmtp in order to open the file right on the terminal sudo g added slash etc slash ssmtp dot com all right so now we have to configure this file we have to add some data into this file first of all we have to define the root for ssmtp that what is the email that we will use to send the email to the other emails or other respondents so root is equal to test email tsub gmail.com this is the email that i have created for this purpose after that we have to define the mail hub mailhub is equal to smtp.gmail.com and we are using the port number 587 so you have to define the port number in this way first of all put the column here and then write the port number after defining the root and mail hub we have to add the authentication credentials so authentication user or auth user is equal to test email tcp gmail.com in my case but you have to write your own email after that you have to write your password of the email which will be used to send the email to other respondents for example auth pass is equal to your password we have to encrypt our email to get to the security issues so that's why we have to write here the argument use star ttls which equal to yes so basically it will enable the encryption on your emails when the email will be delivered to the other person then it will decrypt the email automatically so these are the things that you have to add into your ssmtb.com file after that you have to save it and close this file so these commands will be provided in the description of this video so you can copy this command and write into your ssmtp.com file but don't forget to configure it by your own email all right after saving the file close it this is our bash script to send the email so ssmtp after that you have to write your email address and you have to save your script dot sh5 so let's go back to our terminal and execute our script all right so now the terminal is converted into an email console right here the email address of the person that you want to send email like test email tsap at the gmail.com in my case now right here from and write the email now please and now write the body of your email hello this is a sample email all right before sending this email go to your email application and you have to allow the less secure apps access so in case of gmail go to your application click on account in the security tab you will find an option here less secure app access so open it up and enable this toggle button like allow less secure apps it is on so now this email address can get the email from the less secure apps all right so after writing this you have to press ctrl c on your terminal or control d all right so the email is sent so you can see that i have received an email so this is how you can send a mail with mail program i hope you got my point let's move on to our next script send a mail with mail program change subject in this script we will basically add the subject to our email previously we sent an email without any subject but now we will add the subject to our email all the script will remain same we do not have to make any change in our ssmtp.com file or into our script our script will remain same but you have to go to your terminal and execute your script add here to write the email now add the from don't forget to put the space here so put the space write your email in order to define the subject or change the subject of the email you have to define the subject here subject write the subject of your email like this is sample email and now define the body hello i am sending a sample email now press ctrl d and email is sent successfully so let's go back to our email and here you can see that the subject of the email is sample email so this is how you can add the subject into your mail program all right now let's move on to our next script send a mail with mail program to multiple recipients so now we have to send the email to multiple people so now let's see how we can do it there is no need to change the ssmtp.com file or your script dot asset file just go to your terminal and execute your script right here to enter the email and for adding multiple recipients you have to give the space and write another email like this and before adding any other email in two add one more space and write any other email address like this all right rest of the procedure will remain same like test email tcp gmail.com define the subject hello this is the body of the email and now press ctrl d and the email is sent to the multiple recipients so this is email the subject is hello and the in body we also have hello world so i hope you got my point that you have to configure your ssmtp.com file once and you have to write ssmtp and your email address in your script dot sh file and the subject to from and the message body will be added on the terminal there is no need to change your script again and again all right now let's move on to our next script send a mail with mail program to cc and bcc list to send an email to your cc and bcc list go to your terminal execute your script dot sh write here to and add the email address from write the email address here all right now add the subject of the email hello and to add the cc list in your email write cc here put the column give space and write the email that you want to include in your cc all right test email and similarly for the bcc list write your bcc and derive the email address add the body of the email and now press ctrl d the email is successfully sent so you can see that in cc we have test email tcp and in bcc we have our email address so this is how you can add the cc and bcc list in your mail program i hope you got my point all right so now let's move on to our next script this is print mouse usage with df as we previously discussed that the mounts are basically that devices that are linked with the file system to store some data or to store the files all right so now in this script we have to print the mouse usage that how much amount device is used and we have to do this task with the df command the df command is used to print all the information that is related to the file system so if we execute the tf command df it will show the all mounted devices all the information of the file system and here you can see that this is our device you dev and it is mounted on slash dev and it is used zero percent so df command return all the information of the file system along with the amount usage but if we write here df hyphen a so it will return all the accessible mounted devices with their information all right so here you can see that these are our file systems these are mounted on these directories and this is the percentage usage and similarly we can run this command in our script as well so if i write here df with hyphen a and try to execute our script on the terminal so here you will see that we will get the same result cd desktop script dot sh and you can see that we have got the same result so this is the name of the directory where a particular file system or device is mounted on and this is the usage in percentage for example this is tmp fs it is mounted on here it is mounted here and it is only one percent used i hope you got my point now let's move on to our next script that is send a mail with mail program with encoded attachment so in linux there are multiple command line mail programs like we can use the mail command we can use the mud command and the other commands as well previously we did all the mail related tasks with the ssmtp and now we are gonna choose the mud command to send a mail with mail program with encoded attachment so in this script we will send an email which will contain any attachment in order to use the mud command first we have to install it into our system by using the command sudo apt install mud so this is the command for the debian based operating systems if you are using any other operating system then you can set the relevant command on the internet all right this is the command enter your sudo password and it will install the command on your system the mud command is already installed on my system but if you are installing it for the first time then it will take couple of minutes so now let's see how the mud command actually works it takes multiple parameters so first of all this is our code i'm gonna copy this code into our script dot sh file and i will explain this code to you all right so echo this is the message body so first of all we have to define the body of the email after that we have to put this pipe sign and we have to write the name of the command that is essentially mat and hyphen a hyphen a option will attach the file so here you have to give the full path of your file for example if the file is placed inside the downloads folder instead of your home directory then what you have to do you have to specify the full path like this downloads slash file.txt so it will go to the downloads folder it will search for the file.txt and it will attach it with your mail program all right so file attachment is done after that the hyphen has option defines the subject of your email so the subject of the email is file attachment after that we have to put this double dash and we have to write the email of the recipient so test email tsab gmail.com this is the email of the recipient so when it will send the email successfully it will print a message on the terminal that email is sent successfully all right now let's go to our terminal and try to execute our script here so script dot sh email is sending and you can see that it has printed a message here email sent successfully so let's check our email all right so you can see we have got the file attachment here to me and the subject is file attachment this is the file dot txt all right file let's change the subject again file attachment this is the message body 2 and and send the email again all right cd desktop script dot sh email sent successfully and here you can see that again file attachment is the subject and we have got the file.txt it contains this information all right so this is how you can send the attachments with your email using the mail program so mail program that we used in this script is mud command i hope you got my point now let's move on to our next script that is download web page with curl command curl is a command line tool to transfer data to or from a server using any of the supported protocols like http ftp pop3 scp and etc car is very powerful linux library this tool is preferred for automation since it is designed to work without user interaction so you just have to write the curl command after that you have to define the url of the web page and it will download the web page and it will store it into a mentioned text file so let's see that how we can download the web page with girl command let's go back to our script dot sh file all right so this is our script dot sh file and this is our script to download the web page using the curl command in order to use the curl command you have to write your girl after that you have to define the url of your web page and in my case i have written here https www.google.com and if you want to save this webpage in any text file then after writing double hyphen output write the name of your text file like file.txt so what this command will do it will download the web page and it will store the output into a file that is file.dxd so this is very simple process of downloading the web page using the curl command let's go back to our terminal and try to execute our command but before executing your script dot sh file make sure that you have downloaded the command so if you have not installed or downloaded the cal command then write this command on your terminal sudo apt install curl and it will download or install the curl command on your system so write this command hit enter the curl command is already installed on my system and it is in the newest version but if you are installing it for the first time then it will take couple of minutes all right so after doing this execute your script script dot sh and you can see that it is downloading the web page and it has stored the output in my output.txt file alright so you can see that the output is stored into this file so this is how you can use the curl command to download the web page so let's change the url and execute this command again for any other webpage linksynt.com all right so we have changed the url and let's and let's execute our script again all right in our file.txt it has stored the result this is the result so i hope you got my point so let's move on to our next script it is supposed to http with girl it means that we have some data in the file and we want to transmit this data into some webpage or http let's go back to our script.sh file so when we have to post some data to any web page then we have to use the curl command like this hyphen x option mention here the hyphen x option and after that we have to write here post because we want to post or transmit something from a file to any web page or any server all right after that you have to write here hyphen t at the rate and now write the name of the file that is file.txt so whatever stored in the file.txt will be transmitted to the given url and the url is http www.google.com that i have i have put this url to demonstrate the working of call command post to http all right this is my file dot txt this is the url let's go back to our terminal and execute our script so it will basically transmit the data of file.txt to http www.google.com so the key point to remember here is that we have to write here post and hyphen d after that we have to write at the red sign and we have to mention the name of our file all right now let's move on to our next script that is put command in background so there are multiple ways for putting the command in background however the easiest way of putting the command in background is by choosing the and symbol so when you write any command on your terminal and you put the and sign at the end of the command it will run the command in the background all right now you can see that the command is running in the background and similarly you can do this task into your bash script as well let's go back to our bash script and write here sudo apt update and and it will do the job so it is very simple to put the command in the background the idea is put the end symbol at the end of the command and it will work in the background i hope you got my point now let's move on to our next script that is resume command from background so previously we put a command in the background now we have to resume the command from the background and let's see how we can do it the simplest and the easiest way to do this job is by using the fg command ft command will put all your background commands on the foreground fg is the abbreviation for the foreground so let's execute an other sudo apt update command with the background all right and now let's write here fg and you can see that it has put the command in foreground so the idea is very simple write fg in your best script try to execute it and it will do the job so that was the idea of resume command from background i hope you got my point all right so now let's move on to our next script that is execute last command again with bang bang bang bang is used to execute the last command so let's see that what is bank bank command and how it works in linux i am writing here command sudo apt update and it will update my apt repository cache so this is the last command that we have executed sudo apt update so to execute this command again with the bank bank we have to write on our terminal double factorial sign or exclamation sign so this is the exclamation sign or bank bank command that i have written on my terminal so hit enter and you can see that it is executing the last command again sudo apt update so this is how you can execute your last command with bank bank now let's move on to our next script or next command this is t command to print to file and screen the t command is used to print the data on the terminal and it also save this data into another file simultaneously so let's see the execution or working of t command in our batch script this is our script dot sh file so in order to print the data on the terminal and to save it simultaneously in any other text file we have to use t command like this t hyphen a hyphen a is the option that is used to append the file so in the file if there is any existing data so it will not be replaced the t command will append the file and it will add the data at the end of the file so t hyphen a file dot txt file dot txt is the name of my file where we have to store the data so let's save our script dot asset and go back to our terminal script dot sh now the terminal has become a console so if i write here hello it will print the hello on the terminal and it will save it into the file as well so it has printed it on the terminal if i write here linux hint so linuxint is printed on the terminal and it is saved into the file as well so to exit it press ctrl d let's open our file dot txt and you can see that the hello and linux hint is saved in my file.txt as well so this is how you can use the t command to print the data on the terminal and to store it into the file so this is it from this video we hope you have liked this video and this video has helped you in understanding bash examples in this video we have covered almost 200 plus dash examples so if you have like this video keep visiting our website linuxin.com and don't forget to subscribe our channel thank you so much see you in some other video
Info
Channel: linuxhint
Views: 103,929
Rating: undefined out of 5
Keywords: bash, bash scripts, bash scripting, 209 Bash Scripting Examples, 212 Bash Scripting Examples, 200+ bash scripting examples, 212 bash scripting examples
Id: q2z-MRoNbgM
Channel Id: undefined
Length: 286min 5sec (17165 seconds)
Published: Wed Nov 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.