Transcript for:
Shell Scripting Basics

hi guys uh Welcome to our first Hands-On lesson today today we will look at what kernel is and what is shell and make a small application we can start from the meaning of the kernel a kernel is the core of an operating system it is responsible for managing all of the systems resources such as the CPU memory and storage the kernel also provides a set of system calls that allow user mode programs to interact with the hardware it manages the following resources of the Linux system like file management Process Management IO management memory management device management Etc and this is PM and guys we can continue with shell what is shell uh we can say that a shell is a program that lets you interact with your computer's operating system it takes the commands you type in and turns them into instructions that the operating system can understand here is another way to say it a shell is a program that sits between you and your computer's operating system it translates the commands you type into something that the operating system can understand I hope this is helpful also in Shell scripting shells are usually uh interactive meaning that they accept commands from users and execute them immediately however sometimes we need to run the same commands repeatedly and it can be tedious to type them all in each time shell scripts allow us to write down a sequence of shell commands in a file and then execute them all at once this can save us a lot of time and effort especially if we need to run the same commands on a regular basis shell scripts contain a list of commands however shell scripts are more powerful and flexible because they can use the full range of shell features such as variables loops and conditional statements to create a shell script simply open a text editor and write down the the commands that you want to execute then save the file with a Dosh extension for example a shell script called myscript Doh would contain the following commands uh let's continue with creating a shell script login to your Linux machine and open the terminal navigate to the folder where you want to store the shell script one create a new file with the SSH extension such as script. Sage with this command touch script. s each to give the script executable permission by typing script d three open the script file in a text editor and add your commands now we will add some commands to the shell script open the shell script with any text editor of your choice and add some commands uh we will use Nano type in nanoscript yes we can write whatever we want here with Nano command after saving and exiting our changes will be in our file we open it and look and see the same change here now let's apply our commands here here we will use the echo command defined by the Linux kernel in the shell script if we run the script here by typing the message we want our output will appear after completing the last changes in sh file we come to the terminal screen and we can run our shell script as follows here we can see the output of uh our Echo command also we can add comments in the shell script any line that starts with that in the shell script is treated as a comment and is ignored by the shell during execution except the shebang line which we will see later in this article let's see an example yes the only output shown here is the hello world message in the first part the other part has not been processed as a comment uh yes friends we have come to the end of our first lesson thanks for listening enjoy your [Music] work Hello friends in today's lesson we will make a brief introduction to variables and make small applications about them yes shell scripts support the use of variables and we need not Define a variable's type during its declaration there are two types of variables these are system defined variables and user defined variables system defined variables also called environment variables you can view all the current environment variables using the print NV command user defined variables are set by the user and they exist only during script execution you can define a variable by simply typing its name and assigning a value with equal sign and access a variable by adding a dollar sign before the variable name variables are demonstrated in the following example script before starting our example let's prepare our editor and terminal uh there are many shells available in Linux such as the born shell the corn shell and gnu Born Again shell you can specify which shell the script will use even if the script is executed from another shell terminal to do this add these on top of the script file this line is called the shabang line here we state that we will use the bash script infrastructure now let's define our first variable first of all I write a comment on the first line to indicate the location let's define our first variable here and give it the value as a string we can then display it this way with the echo command we are getting an error here and we have a syntax error regarding the spelling of shibang let's fix it immediately let's define another variable here by giving readly mode to variables that we do not want to be changed later in the code we can control the first value assigned to the [Music] variable here we see the mistake we made and we see again that we cannot name the variables with the mins sign yes we were able to observe the value of our variable by running our code again and now let's test that the our value here is really in Renly mode first let's try to assign it value to this variable again and see the error we will get when we run our script yes we can observe the readly mode and we keep this feature in mind yes now let's suppress our variable let's see that after assigning an integer value to a new variable these two parts cannot be ConEd or summed we have seen that the shell script does not provide us with such a feature now let's move on to taking action with an input received from the user first of all we make a request that indicates the user's entry then we will assign the variable value given by the user to the variable here with the read command then we write our variable name to show the input we received but do not forget to put a dollar sign in front of it now let's enter a name and we can see that we have saved it in a variable in this way we are provided with a pleasant welcome in the part we have written in the code Let's do an example here let's open a file according to the requested string after receiving this name from the user let's see a file that is automatically generated in the directory we are working in let's run our code give input here and see that our file has been created after completing the process we received our message and can view our file in the desktop directory we can also view our created file in this way yes friends thank you for listening see you in our next [Music] lesson [Music] Hello friends welcome again to our lesson today we will examine comparison operators with you this part which we will use later when writing our conditions is a nice part of shell scripts you can compare two variables in Shell scripting we make these comparisons to make decisions we will show how to do this with small examples in this lesson first we prepare our environment let's clear the codes we wrote previously and get started first of all I would like to include some keywords here these are equal to not equal to greater than less than is equal or ask ke comparators let's start by trying some of the comparators here first of all we use them for testing we will understand conditional statements better when we see them in future lessons first let's start with the equal operator here we use positional arguments know know as dollar and question marks which give us the output of the last line so that we can see the result after writing the expression in this way we will be able to see whether the outputs are correct or not here one indicates false and zero indicates true let's try to increase our examples here let's copy the same line and try it with a different operator here we will observe that the results of incorrect comparisons are one and the results of correct comparisons are zero when we look at our output we can see these in an order only our first and last conditions are true the rest are false now let's add a line here so we can move on to new examples and not confuse the results we can also compare strings here let's make an example about this when we check this with the equal operator when two strings are equal we observe that our output is correct that is zero when we check this with the not equal operator when two strings are equal we observe that our output is wrong that is one now we can move on to string comparison as a new example here we see that when we compare the two strings we get an unexpected behavior and we cannot work directly with the equal or equal not operators as seen in the code we are outputting the value false that is one as a result of an uncertain Behavior thank you for listening friends see you in our next lesson [Music] hello everyone Welcome to our lesson today we will see conditional statements with you conditional statements are used to execute a block of code only when certain conditions are met we use comparison operators to check the now we are preparing our environment we will see some structures for these conditions the key keywords here will be conditional options such as if and else let's see a few conditional statements now we can start our example let's define two variables here and assign values in the example we will do here um let's compare two numbers as condition first of all we start with our if keyword and write our condition we want to compare our two numbers with the not equal to operator and return our state as true thus since our condition is verified we will be able to perform the operation within the block we can see our message as output in the terminal because our condition is true now let's check what will happen if the situation here does not occur by adding an else statement if the condition was not met the Block in the else statements would run let's fix our code to see this and we can see that our new message has been printed on the screen we can also use conditions Ned now let's make let's make an example for this here we can Define another if else block inside the else statement as a structure this way when we edit our code we will get the correct output for example let's compare two numbers and write a condition that is not [Music] true since we will enter our else block later let's change the condition here and check whether our number is greater than zero in this way it will select the correct state for itself and print the message our new conditional statement is case structures direct action takes place according to the selection specified here it has a slightly complex syntax structure even I can get confused sometimes and I usually look it up on the internet the case value that will occur according to the value of our condition here will be selected since we set the value here as one the first message will be suppressed as a condition let's add a new case and see what happens with a different option let's make an example here and ask the user to enter the condition to be selected here we receive an input from the user with the read keyword and accordingly we can have the user choose the action to be performed in the case this this is important because for the first time we we are making a transaction depend on the user yes guys thank you for listening see you in our next [Music] lesson hello guys today we will examine Loop statements with you first let's prepare our environment there are total three looping statements that can be used in bash programming first while statement second for statement and third until statement to alter the flow of loop statements two commands are used break and continue first let's start with the while loop M do while using a while loop here we will go through the following structure first of all the expected condition is written next to the while keyword afterward the desired operations are listed among the do done keywords um secondly for Loop when using a for Loop the first variable is used as an iterator and takes these values throughout the loop the operations processed during this time can also be performed during the cycle we can continue with until statements as seen here its structure is very similar to the while loop and in the same way all operations can be performed between certain keywords as an important note we can say this if the condition in the loop loop is met our Loop will continue now we can continue with our first example I thought of a few examples for this lesson uh first we can start with the foro after typing the structure here we see that our iterators will take the specified values in order in order to see these let's print them with the echo command in our Loop the keyword specified in the for Loop is important and we got an error because we forgot it here we continue on our way by adding this and as an output we see our numbers as shown in the figure for now we will use the break keyword in our Loop if we use this keyword Loop ends so we as a condition here and request that if this condition is met our Loop will not end we achieve this with the break keyword here when our iterator reaches the value three our Loop ends and thus we can only see one and two as output if we had uh use the continue keyword instead of break here our Loop would only skip when it reached this step and we would get 1 2 4 5 as output in general when the condition is met continue will jump to the next iterator value there and the loop will continue by passing here as if it had never been here now let's come to our new cycle here we will see while loop generally after typing the well keyword our condition is placed while doing this we Define an iterator and give it a value if the condition in our well Loop here is met the loop will continue let's not forget to write the do and done keywords again here we make a mistake and forget to reduce our iture that's why our Loop is defined as an infinite Loop we are decreasing the iterator value here and we can see the output correctly to test we can increase the iterator value here and run the loop more we can see the iterator values here as output now let's move on to another example first let's define an array here we haven't seen it before but let's learn here how arrays can be defined this way we can use this array in our Loop like this let's try to suppress our array values with Echo command we can see as output that the values here are displayed sequentially now let's move on to another example let's see what can be done using infinite Loop here since we expect our condition within the loop to always be met we can use true after the while keyword when we run our code here we will get continuous output and it will be very fast so we can use the delays that our operating system provides us with the command we will use here we will repeat our operations at 1 second intervals and we we will be able to observe our output better yes our cycle can continue like this forever but we stop it now let's move on to another example in our example here we will end our process with a request from the user for this we will check a value assigned to our variable by the user and end our Loop accordingly here we we ask the user to enter a number and if the number is correct we want the loop to continue otherwise the loop will end and the program will be terminated here we set our choice value to yes in order to enter the first condition of our Loop and if the user receives a value other than yes we end our Loop yes friends we have come to the end of our lesson thank you for listening see you in the next [Music] videos hello everyone Welcome to our lesson today in our lesson we will see positional arguments first let's get our environment ready positional arguments or special parameters are the arguments or values which we pass to the shell script while executing the script in this lesson we will discuss the special parameters of the shell script before we do that let's understand what parameters are in the shell don't forget guys a parameter is an entity that stores a value the variables are parameters that are defined by the user to use in a specific shell script special parameters are Ron variables that are predefined and maintained by the shell for now we can see these parameters in example in our first example we can see that the script name can be obtained uh as output with the specified argument uh as seen in the figure uh we can also print it as output with the dollar and one as the first argument after the script name in our example we can give the arguments after our script name 1 by one likewise we can access our second parameter by giving the argument number we want after the dollar sign thus in the arguments we give we use the hash sign after the dollar sign since we have two arguments here we will see two outputs in another example we can obtain the output of all given parameters in a single way we have also come to the part that I use the most here we can suppress the stat of the previously executed command if the last command is executed correctly it will return zero um there is also a place here for the P section uh which will be included in our Advanced topics later sometimes we will need to kill the process if we want but first we will need to learn the P this is how we can learn the P ID of the shell we are running here yes another thing we need to learn is the flags of our shell we can print these on our screen along with the dollar and minus signs HB flag is used to print the help message for the command being executed in a by likee format yes guys thank you for listening see you in our next [Music] lesson Hello friends uh today in our lesson we will see how to store the outputs of [Music] commands here we can see the directory we are currently working in with the PWD [Music] command when we put the command here into a variable let's suppress it with Echo command we will see that we get the same result here we can store any any command this way let's do another example we can obtain information about the status of the objects in our directory with the ls minus La command we can assign them to a variable in the same way and see the output Let's do an example here let's check whether the directory we are working with is the directory we want with the PWD [Music] command here we will uh use our else statement along with our if statement if our directory is as we want let's see the correct directory message if not let's print it with a different message as you can see we get this output when our directory is correct however when we try to access our script from a different location the directory we are in is not correct and we see the message in the else statement yes guys thank you for listening see [Music] you Hello friends uh today in our lesson we will see the exit codes of shell [Music] commands whenever a command ends and Returns the control to the parent process it returns exit codes between 0 and 255 exit code 0 means the command was successful and any other exit code means uh the command was unsuccessful you can view the exit code after running any command by by accessing the dollar in variables you can manually set an exit code for your shell script this can be used with conditional statements to convey if the script's purpose was achieved or not now we will make a small example about this we will ask the user for a number in return we will tell it to return an exit value after completing our code here we receive an error due to our syn text error let's fix that right now later when we give the value 100 as input our first if statement is executed and we see the message like this when we give a value other than 100 our else statement is executed we can see what kind of value value our script returns to us with the positional arguments we have seen before here after our script ends we can see the output values as dollar and question mark here when we enter the value 100 we can see that our output is zero we have seen this with positional arguments yes friends thank you for listening list in see you in our next [Music] lesson hello everyone and today uh in our lesson we will make an introduction to the subject of redirection first we can prepare our environments the topics we will see here will be override redirection and append redirection we can briefly talk about redirections as follows when you run a command in Linux it can take input produce output or both redirection allows you to change the default destination of input and output this can be useful for a variety of tasks such as saving the output of a command to a file or provid ing input to a command from a file for example you know when we run the date command it gives us output to the screen however if we want to save the result to a file we can use output redirection here we will send the output of our Command to a different place and this way we can store the output of the date command in a file and reference it later these directions can be useful when we work with multiple and large out outputs or inputs since we can use file data directly as input and store the results in files yes friends here we can also save the output we want to a different file using overwrite redirection along with the echo command this way when we refresh our file we see the change if we want to see it separately as output we can add it to our script with the cat command what we also need to see here is the append redirection if we add this the new output we will add will be appended to the old file in this way we can see that the text file grows as our script runs now let's see how to use input redirection input redirection in shell script is a way to redirect the input to a command from a file or another input source this can be useful for a variety of tasks such as processing large amounts of data or automating tasks the redirection operator tells the shell to read the input to to the command from the file or input source that follows it for example the following command redirects the input to the sort command from our file here we save some words to our text file then we will wait for it to be processed as input and sorted with the sort command yes we can see that the input we gave with redirection entered the process correctly yes friends we have come to the end of our lesson thank you for listening hello everyone Welcome to our lesson today we will continue with our second lesson which is the continuation of redirection here we will specifically focus on merged redirection and give examples of them merg redirection in shell script allows you to redirect the output of a command or a program to a specific file descriptor instead of standard output the Syntax for using merge redirection is the greater than an essand an operator followed by the file descriptor number for example the following command redirects the output of the ls command to the file testex while also merging the output in this example the greater than erson in one operator tells the shell to redirect the output of file descriptor 2 standard error to file script of one standard output this means that any error messages output by the allas command will also be appended to the file test txt merge redirection can be useful for a variety of purposes for example you could use it to log uh both the standard output and standard error of uh command to a single file you could also use it to combine the output of multiple commands into a single file okay guys we can continue with error redirection error redirection is transferring the errors generated by some false commands to a file rather than s. whenever a program is executed at the terminal three files are generated standard input as zero standard output as one and standard error as two these files are always created whenever a program is run by default an error stream is displayed on the screen in the below mentioned example the file descriptor used above is 2 SST CD err using two and greater than redirects the error output to a file named error. dxt and nothing is displayed on STD out yes friends here we can only see our STD err message in the file along with the redirect here two greater than % one means that STD redirects to the Target of STD out more formally the error message generated by two gets merged with the current output one also guys we need need to add this note greater than and % operators can be used to redirect both standard output and standard error but it is not supported in all shells s and ksh do not support and while bash and zh supported guys our new to is discarding the output sometimes you will need to execute a command but you don't want the output displayed on the screen in such cases you can discard the output by redirecting it to the file Dev null to discard both output of a command and its error output use standard redirection to redirect STD ER to STD out here two represents STD ER and one represents STD out you can display a message onto SDR by redirecting Stout into S as follows here thank you for listening to our lesson friends see you in the next [Music] lessons Hello friends today we will talk about A New Concept with you our topic is here document a here document in Shell programming is a special block of code that is passed to a command as standard input it is often used to pass multi-line strings to commands including shell scripts which can then process them as a single string here documents are introduced by the less than operator followed by a delimiter token the delimiter token is a word or phrase that is used to Mark the end of the here document the data for the here document is then provided on the following lines and the here document is terminated by the same Del token on a line by itself first of all uh we prepare our working environment and prepare for our code for example the following command uses a here document to pass a multi-line string to the cat [Music] command yes here we put the less than sign after the cat command and follow it by typing eof to indicate where our commands will end here we can Branch into different strings instead of eof for example we can write a command or a different string but make sure that the two hear words are the same yes we can also put different strings instead of aof here we can write command or any other string the only important rule is that the text here should be the same now another example is the WC command with theel command we will receive information on how many lanes there are in total I am writing a messages line by line here and we will see the number of the lines and of the descript we see here since there are 1 2 3 4 5 6 7 lines the answer will return us as seven but we got the output as nine yes friends we made a mistake here and made the empty lines count therefore when we delete the empty lines and run it again we can get the output seven yes we found our output correctly the total number of rows we will get here is seven and we see and another example is the sort command here we can specify the words as we wish I can write last here or anything but it has to be the same as my last token word we can list the words we will write here I enter multiple words one by one and see them sorted as output this order ordering is of course alphabetical friends we can see in the output that the strings we gave are listed alphabetically and another application is that we can give the commands sequentially here here we will enter different commands in a certain order yes here we can set our token again to the word we want this appears to be a good option for us in every example but I will still use eof here to indicate it in general first of all we will pause our process for 1 second with the Sleep command we indicate these commands that will come later at 1 second intervals here and as we see we obtained the outputs respectively now let's try to write the texts um we specified in a file here with a new example here thanks to the eof keyword all written text will be saved in our text file until we see the next eof keyword now let's look at the output of this yes friends as you can see our text is saved in our file thank you for listening to us see you in our next [Music] lessons Hello friends today we will move on to our new topic with you today we will examine what piping is before we start our operations let's prepare our [Music] environments firstly we can start with pipe means as uh pipe is a form of redirection pipe transfer standard output to some other destination and that is used in Linux and other Unix like operating systems to send the output of one command program or process to another we can make it do so by using the pipe character the pipe is used to combine two or more commands and in this uh the output of one command acts as input to another command and this command's output may act as input to the next command and so on the command line programs that do the further process thing are referred to as filters for example the following command uses a pipe to connect the output of the ls command this command will list all of the files and directories in the current uh directory one screen at a time the ls command produces output on standard output piping can be used to connect any two commands that produce and consume Text data this makes it a very powerful tool for Shell scripting yes we can see the old text files in this directory also we can continue with chain commands chain commands are to use piping to chain multiple commands together for example example the following command uses piping to chain together the ls sort and unique commands this command will list all of the files and directories in the current directory sorted by size in reverse order with duplicates removed yes guys for now we can continue with process input from a file here you can use piping to process input from a file for example the following command uses piping to process the input file a.txt and print the output to the console this command will sort the contents of the file a axt in reverse order and print the output to the console yes guys the other new topic is how can I redirect the output of a piped command to file we can use redirection operator to redirect the output of a piped command first we list the files here and with the piping process we obtain all text files here then we transfer all the information we obtained to a file with the redirection operator yes friends when we look at our file content we can see that we have recorded this information correctly to sum up guys piping is a very powerful tool for Shell scripting it allows you to connect commands together to create complex data processing pipelines thanks for listening guys see you in our next lessons [Music] hello everyone uh today we are moving on to a new topic with you our topic is to pass and parse Linux shell script arguments and parameters first of all let's prepare the environment in which we will work yes guys there are two ways to pass arguments and parameters to A bash script firstly positional arguments these are arguments that are passed to the script when it is executed they are accessed using the dollar one dollar two Etc variables secondly named arguments these are arguments that are passed to the script using the minus or minus minus prefix they are accessed using the OPG variable to parse arguments and parameters in a bash script you can use the get ops command this command allows you to define a set of valid arguments and options and then pars the command line arguments to see which ones were specified here is an example of a bash script that uses ghetto pits to pars arguments and parameters first we select the specified opts that our system can Define we will then follow this in a specific order within the cycle we will constantly check these as cases and choose these parameters and make decisions if the argument is selected as minus uh here we'll go into the first case here this way the logic will continue yes friends we have finished our first part the next step we will do includes positional arguments first we check that the number of parameters entered is greater than zero if this condition is met we continue with a for Loop here to show the positional arguments now we can run and test our script we have a syntax error here first we run it and try again and as our result shows the arguments are correctly specified yes guys for now we can start with a new topic shift keyword you can also use the shift command to iterate over the positional arguments the shift command removes the first positional argument from the list of positional arguments here is an example of a bash script that uses the shift command to iterate over the positional arguments in this example uh as long as there is the number of arguments we will see the arguments as output that's why we use a while loop and since the first arguments are constantly removed we print the remaining first argument to the screen this cycle continues until we have no variables left uh now we have completed our example friends let's test my code by giving different arguments here as we can see the arguments here are displayed respectively thanks for listening see you in [Music] our [Music] hello everyone today we continue with A New Concept our New Concept to perform operations on a file on shell script shell scripts can be used to perform a variety of operations on files such as Reading Writing appending deleting moving renaming and changing permissions here are some examples of shell scripts to perform operations on files first we start with reading normally we could see the content with the cat command what we want to do here is to run the cat command via a variable in this way the file to be read can be seen in a variable yes as we see we saved the content here with a variable secondly what we want to do is add something to a file after adding to our file here we can see it from the terminal with cat command and the command worked and the correct output appeared in our next file operation we want to add more text to our file after doing this the words will be appended yes friends as you can see here we have a text that combines with the previous text a pen processing occurred correctly our next step here is the move operation we can move our file directly to the directory we want I choose temp directory here and we can do this by opening a new terminal and using the ls command to check whether our file has been moved or not yes friends we see our file I run the command here in the same way to restore our file to its previous location now our other topic is naming our file likewise we can do this with MV command when we give the new name of our file as the second argument the operation will occur correctly here we can check the content of our file again yes our other topic is change permission here we can look at the previous permission with ls minus S command afterwards we can change our permission and compare it 777 permission means granting all permissions and when we checked again the permissions were changed successfully another topic is deleting our file here we can remove our file with RM command as you can see we can delete our files when we give the files we want to delete as the first argument now now we can move on to our new topic here we will check whether our file exists first of all we learn the file name of which file the user wants to check afterwards we can check this file name with the minus E option in the if condition if we have a file the message looks like this however if we search for a file that does not exist we can see the message that that our file cannot be found uh as shown in the figure in our current example let's check whether our file size is larger than 0 bytes likewise we take the name of our file as input and test our operation in the if condition with the minus s option if we run for an existing file here we can get the message that our size is greater than zero otherwise the state in our L's condition will be realized another example is reading our file line by line here first of all we get our file name in the same way when we use the red command with the minus P option we can also add our message here then we can read the lines of our file one by one in an endless loop and print them on the screen now in our new example we will print the number of characters number of words and number of lines in our file likewise let's request the name of our file then let's put our different commands into different variables the commands in the variable here will give the number of characters number of words and number of lines respectively then let's put the information here on the screen I realize I made a mistake here because I saved my commands as string when I check my output after fixing this I can see the values belonging to the file here thank you for listening friends see you in our next lesson I wish [Music] everyone hello everyone today we will give a general summary of how to use the grip command the grip command is a powerful tool for searching and filtering text in Shell scripts it can be used to search for patterns in text files find specific lines of text and extract data from text files here is an example of a shell scripter that uses the GP command to search for log files for specific errors search for log files that contain the error message system search for log files that contain the error message system and ignore the cas scrap minus I system VAR log CIS log 1 search for log files that contain the error message system and print the line number of each matching line grip minus n system V log CIS log 1 here are some examples of how to use the grip command in the shell script search for all lines in the file my file XT that contain the word hello search for all lines in the file my file. t60 that contain the word hello and World here search for all lines in the file my file dot that do not contain the word hello um with minus V command um if you want to print the line number of uh each line in the file that contains the word hello you can use this command with minus n command yes guys we can see the result in our terminal and also we can save this output a file n of the r operation also here we will use some other options with grip commands one case insensitive search the I option enables to search for a string case insensitively in the given file it matches the words like hello with upper case o the other example is displaying the count of the number of matches we can find the number of lines that match the given string or pattern with minus C [Music] command our third option is to display the file names that matches the pattern we can just display the files that contain the uh given string or pattern with minus L command fourth option is checking for the whole words in a file by default get matches the given string pattern even if it is found as a substring in a file the W option to grip makes it match only the whole words with minus W command the fifth option is displaying only the Matched pattern by default GP displays the entire line that has the Matched string we can make the GP display only the match string by using the minus o option another option is to use minus V option for inverting the pattern match you can display the lines that are not matched with the specified search string pattern using this option here when we search uh for a nonsense word with the minus V option uh we can see the entire text as everything EX except this word will be seen to make sure of this we can observe the text with the cat command and see that it is the same the other property is matching the lines that start with a string the pun regular expression pattern specifies the start of a line this can be used in GP to match the lines that let's start with the given string or pattern the 10th operation also specifies an expression with the minus a option can used multiple times like this the most important property is to print n specific lines from a file minus a prints the search line and N lines after the result the last property is to search recursively for a pattern in the directory minus r prints the search pattern in the given directory recursively in all the files the syntax is like that in here minus I to search for a string case insensitively and minus r to recursively check all the files in the directory to sum up the grip command is a powerful tool that can used to perform a variety of [Music] tasks hello everyone today we continue with our New Concept today we will continue with the subject of alysis that will facilitate our transactions aliases in Shell scripts can be used to create shortcuts for commonly used commands to create an alias you use the Alias keyword followed by the name of the Alias and the command that you want to Alias um for example the following Alias creates an alias called L for the ls uh minus LL command uh but only we need to use the Sho operation the shoed minus s expand aliases command is used to enable the expansion of aliases in Shell scripts by default aliases are not expanded in Shell scripts this means that if you use an alas in a shell script the shell will interpret the Alias as a literal command to enable the expansion of aliases in Shell scripts you can use the shop minus s expand alias' command at the beginning of your shell script for example the following shell script will enable the expansion of aliases if you run this shell script the ls minus L command will be executed as seen here we can briefly use this command in more than one way and the results we get are as seen let's separate the outputs here with an echo command and observe them again like this there are a few reasons why you might want to enable the expansion of aliases in Shell scripts it can make your shell scripts more readable and maintainable for example you can use aliases to create shortcuts for commonly used commands it can simplify complex commands for example you can use aliases to combine multiple commands into a single command it can allow you to use aliases in Shell functions here are some additional tips for using the shop minus s expand the aliases command in Shell scripts place the shop minus s expand alias' command at the beginning of your shell script this will ensure that aliases are expanded in all of the commands in your shell script be careful when using aliases in Shell scripts if you are not sure what an alias does you can use the Alias command to view the definition of the Alias test your shell scripts thoroughly before using them in production this will help to ensure that your shell scripts work as expected to sum up guys aliases can be a powerful tool for making your shell scripts more readable maintainable and efficient by using aliases you can create shortcuts for commonly used commands and simplify complex commands here are some additional tips for using aliases in Shell scripts use descriptive names for your aliases this will make your shell scripts easier to read and understand use comments to to document your aliases this will help you and others to understand what your aliases are doing uh test your aliases thoroughly before using them in production this will help to ensure that your aliases work as expected I hope this information is helpful please let me know if you have any other questions I hope this information is helpful and thanks for listening guys see you in our next lessons hello everyone today we will examine the weight command which is an important specifier weight command is one of the process management commands the following shell script demonst rates the use of the weight command in Linux first we will create a few background processes here the slip command is suitable for this and we can run it in the background with the Amper sign as you can see our output may appear after waiting for 2 seconds but we need to use the weight command for this um even with the weight command processes are weighted and we can see the finished text this script demonstrates how to use the weight command to ensure that all background processes have finished before exiting the script this is important because it prevents the script from exiting prematurely which could leave unfinished tasks behind here is another example of a shell script that uses the weight command first create a simple process that will create the file and write into it and this will store the process ID of the running process dollar and the exclamation mark is a special variable in Shell scripts that will hold the P ID of the last active process Salan and wait until the process is completed with wait command as you can see here the P of our running thread is visible then we can see our output with our end message uh yes friends we have completed our lesson see you in the next lessons [Music] hello everyone in today's lesson we will summarize the signal handling mechanism and realize our examples signals are a way for the operating system to notify processes of events such as a keyboard interrupt Sig or a termination request Sig term signals can be used to implement a variety of features in Shell scripts such as graceful shutdown or handling user input to handle signals in a shell script you can use the Trap command the Trap command takes two arguments the signal to handle and the command to execute when the signal is received the following example shows how to use the Trap command to handle the the Sig in Signal as seen here we write in our triap Command how it will react when our s signal arrives we will do this in an infinite Loop and make an observation at 1 second intervals yes guys after the Ctrl C process only we are seeing that this program is not terminated therefore we need to develop our code as shown guys yes friends we will try to handle this situation by adding a new function here here we add a condition for our script to stop after the signal arrives and write it like this we need to specify it in the command trap we wrote here so that we can see how it will react when the signal arrives and we see that our application terminates after our c c [Music] command yes guys our new topic is chaining the signal handlers we can combine our commands after the Cru process we use the Sig int signal as an example to learn the Trap command first we demonstrated the custom Cy Handler here we will write our functions one after another in the Trap command so we create two similar functions then we specify them in the Trap command and tell them in what order they should run after our signal we write our infinite loop again and observe what kind of output we will get after receiving our interrup as seen here our functions worked consecutively after the Ctrl C command yes friends we have completed our lesson see you in the next [Music] lessons Hello friends in today's lesson we will see how we include the debugging option in our system and what kind of output we get debugging in shell script is the process of identifying and fixing errors in a shell script there are a number of different ways to debug a shell script including using the set minus X option this option will print a trace of every command that is executed along with the arguments to the command and the value of any variables that are expanded this can be helpful for identifying errors in the flow of the script or in the syntax of individual commands using Echo statements you can insert Echo statements at various points in your script to to print the value of variables or the output of commands this can be helpful for tracking down errors or for understanding how the script is working in addition to these General debugging techniques there are a number of specific things you can do to make your shell scripts more debuggable such as use descriptive variable names and function names this will make it easier to follow the flow of your script and to identify errors break down your script into smaller more manageable functions this will make it easier to debug individual parts of the script use comments to explain what your code is doing this will make it easier to understand your script and to identify errors test your script thoroughly with different input data and this will help you to identify errors that not may not be apparent when you are running the script with a small set of test data here is an example of how to use the set minus X option to debug a shell script here we write our function that we can debug and give the text we want to see as as an argument afterward we can disable the debugging option again as follows we do this with the set plus X command now when we see our outputs we can see them together with the commands called when our debugging option is on in summary debugging shell scripts can be challenging but it is an important skill for any Shell scriptor by following the tips above you can make your scripts more debuggable and identify and fix errors more quickly thank you guys for listening see you in our next [Music] lessons hello everyone in today's lesson we will explain what Rex is we can also see its expansion as regular Expressions so what are these regular expressions are a powerful tool for text manipulation and pattern matching they are used in a wide variety of applications including text processing data validation and network security shell provides several built-in features for working with regular expressions these features can be used to search for replace and extract text patterns from strings first let's prepare the text file we will use and our working environment first let's save the output of the file we will read with our cat command in a variable let's see what happens later when we want to print this output on the screen here we could not get the uh output correctly because the event we wanted regarding the spelling did not work therefore let's try to obtain the output of this command with a different spelling rule as you can see we were able to save the contents of our file into a variable able now we can move on to the example we will make with rejects Expressions here we need to add a few more lines to use in our examples we will see its benefits in the examples we will use in our first example we will use the dots sign using do sign we can find the string if we don't know the string exactly or just remember the beginning and end of the string we can use DOT sign as the missing character and it will fill in that missing character secondly using the K sign we can find all the strings that start with the given character here we will want to see all the words that start with i and the outputs in the last three lines we wrote will match it as seen in the outputs in our next example using dollar sign to match the ending of the string we can find all the strings that end with with the given character here we can see all the lines ending with a we notice that the line below ends with a space character not a so if we fix this we can see our message correctly let's write another message and see if we can view it too and as can be seen we could see our two lines in the same way now the example is using asterisk sign to find any number of repetitions of a string using asterisk we can match up to zero or more occurrences of the character of the string in the example we have done here we ensure that the words we want are like this by specifying apple with an asterisk when we write another word here in the same way we get a similar output we have gone through a few simple examples here but there are Advanced sections for rejects this way we made a good start see you in our next [Music] lessons hello everyone in today's lesson we will make an introduction to the use of said command and explain how to use this command in a simple way the said command is a stream editor that allows you to perform various operations on text files it is commonly used for tasks such as searching or replacing inserting and deleting text you can use the said command in a shell script by embedding it within the script here's an example of how to use the said command to replace all occurrences of the word the two words we write after the letter s in quotation marks after the said command are important in our example here we will change the words hel to high later we can see these as output but we cannot make any changes to avoid corrupting our file so we do not save these files our next example is to delete all lines containing the specified word here after this said command we write the word we want to delete and indicate it with the letter d as seen here all lines containing the word hello written in this way our third example is to insert a word here we write the number of lines and the word we want to insert after the CDD command as seen when we run this this word is added to the first line if we wanted to put the word word ABI on the third line instead we would write it as 3i and as we see the word appears in line three in our last example we have to append the word what we will do here after the said command is to use the letter A when we look at the output we can see that our word has been added for each line thank you for listening friends said command is a long topic and has different features we can conduct more detailed research as needed good day to [Music] everyone hi everyone in today's lesson we will try to obtain some data about the user with the input provided by the user this will be a small video as an example now let's move on to the subject of our lesson every operating system allows for the creation of multiple user accounts linux-based operating systems offer various commands and functions for managing user accounts and their details in this lesson we will delve into these methods and demonstrate how to create a shell script to efficiently accomplish these tasks here in our script we will write our code and use switch cases to compare the user input we have used some useful commands which will help us to satisfy the user requirements let's begin the script let's first print our messages to inform the user here we can print the the information repeatedly after giving the minus E argument with the echo command thanks to the messages we make here we will offer options to the user the user will be able to make transactions with numbers such as 1 2 and three that he chooses from here now let's first see the output of our Eco command and we can observe it correctly the next step is to ask the user to make their selection by taking an input here we will produce output according to the input given by the user with the case structure if our user selects one information will be displayed on how many users have logged in in total if our user selects two the last logged in users will appear here if our user chooses the third option we will obtain the group information of the current user finally let's print a different message here in case he makes the wrong choice now we can test our code when we press one here we get the value one since we have only one user number by selecting two we can see our username and by selecting three we obtained the group's information but if we made a different choice we only saw the message at the end hence we were able to find out various login related result using our shell script thanks for listening guys see you on our next [Music] examples hello everyone in today's example we will write a script that will delete files that have been created in our system for nothing and take up zero bytes of space that have never been used a zero siiz file is a file that contains no data and has a length of zero it is also called a zerobyte file incomplete transfers can be responsible for the zero sized file for now we can create a Zer sized file intentionally by running the touch command in the terminal here we can look at the sizes of the files in our directory with uh the ls L command here let's create our files with touch and we can see that their size is zero yes guys we can continue with our script first our perspective is to check if the directory exists in the current folder directory and use for Loop to Traverse each file and check its size the if line checks if the directory exists if it does the following code block is executed the Eco directory exist line prints a message to the user that the directory exists the for Loop line iterates over all zero sized files in the directory the RM command line removes the current file the last line done line ends the loop the else line is executed if the directory does not exist also now we can look at our output here let's first give a ridiculous directory as a directory and make sure that the else statement works now we can test our own directory and delete the places with zero file size as you can see here we made a mistake now let's examine our mistake generally while debugging let's observe where the error is with the echo command as you can see we cannot find the necessary files let's change the place in for here and continue on our way we can test the command here this way and see that we can access our zero size files now we can change the location we changed with Echo with RM command and as you can see our files have been deleted from the desktop thank you for listening friends see you in our next [Music] lessons hi guys in this lesson we will discuss the process in shell script processes are a fundamental part of shell scripting as they allow you to automate tasks and control the execution of programs in this guide will explore how to manage processes in Shell scripts using various commands and techniques you can leverage shell scripting to automate process management tasks we can continue our example here this code defines a function called infinite Loop that prints hello world to the console every second forever the function is then called in the background and its process ID for now we can write our script the while loop starts an infinite Loop here the echo hello world line prints our message to the console the Sleep one line sleeps for one second the done line ends the the infinite Loop for now we completed our infinite function now let's call our function here and see that we get a continuous output if we start this with the ERS operator we create a background process and it appears on the console like this to stop this we need to list all processes with the PS command reach the pit of our function here and then terminate it uh now let's write this process into script here we first need to record the p of the background process we started in our example here let our background process terminate after 6 seconds and let's print its speed to the screen so that we can observe the result yes friends as you can see our transaction has been completed in addition to this as we have seen there are several tools to view processes the PS command provides detailed information about running processes including their pids user IDs CPU usage and start times and our other important tool is top command and the top command displays real time CPU and memory usage by by processes allowing for identifying resource intent Ive ones thanks for listening friends see you in our next [Music] examples Hello friends today we will learn to interpret the output of a command and we will do this with GP command the exit startus of a command is a number that indicates whether the command was successful or not a zero exit status indicates that the command was successful while a nonzero exit status indicates that the command failed first of all we will write a script here and we will get random outputs this will be our real life test case we will capture the random outputs produced in the function here with the GP command and continue or kill the process there are many ways to implement probabilistic functions in Shell scripts one common approach is to use the random command to generate a random number and then use that number to make a decision for example the following code defines a function that create two random output false xsx or true xsx what we are trying to do here is to decide whether to continue our operations here by interpreting the message in the previous process yes guys now we can continue our example as we see when we call our function we can produce two different outputs when we produce such an output with a different script in business life we can interpret it while doing this we grap the word we want with the pipe operator if there is a word in this output our output is zero if our word does not exist our output is one accordingly we can write an if else statement and continue our script in a different way now let's test our script and as we see here we can continue with different outputs in different situations it should not be forgotten here that the word we grep is a string and can also be represented this way thanks for listening see you in our next lesson [Music]