 Welcome back, we will continue with shell scripts. So, what we last saw was you can write all this into a file called results.sh convention is .sh extension for bash scripts. Then important is you have to give it execute permission. One shortcut way of giving execute permission is .sh mode and 3 digit number. 3 digit number in which every digit can be only 0, 4. It can be some of different sums of the number 1, 2, 4. It is like some binary number, binary number written in decimal. So, each digit has to be like that. This is another way. So, please see .sh mode. Important is whatever shell script you write should have execute permission. Execute permission can be given either by user plus x or .sh mode 7 0 0 results would .sh. So, we will assume that you will see this .sh mode aspect later. Doubts about that certainly we are open to discuss on the forum. Now, about variables. So, then what we have we only saw this. You can write all this and execute that in one go. When is this relevant? If the same set of commands are going to be used again and again, then you would rather take lot of time carefully write them all into one file and execute that file again and again. That saves us of much time, the time of looking up the correct syntax for example. Now, what are variables? So, variables are just like variables in any other program. Bash also has its own variables and you will assign a variable, you will assign some value to a variable. So, this is like in most like in most programming languages on one side of the equal to is the value on the other side is the name of the variable. So, name equal to value. The name gets assigned the value FOSI. So, please do not interchange this order. FOSI could itself be another variable whose value you want to assign to name. How to do that that we will see very soon. So, you can for example, say count equal to word count minus l 1 l n dot t x t. It will just give you the total number of words, word count. The answer to that when you execute this whatever number comes that number you want to assign to the variable count. Once it is assigned to count, you cannot just echo count. Echo count will just echo the word count. You have to echo the value of the count, value of the variable count. So, let us see this. So, for example, word count minus l 1 l n. Some really big number 4047. To put it in open codes, please note which code I am using. I am using this code that is in most keyboards just above the tab key. Just above the tab key is one character called open code. Right above that is tilde. So, tilde also has a important significance that we will see a little later. Open code is different from closed code. It is different from the closed code which looks vertical while the open code really looks like slanted negative slope. Fund size. Thank you. So, what does this do? Its output comes on the screen and gets executed. So, whatever output comes, instead of just echoing it, we can assign it to count. Now, let us echo count. But as I said if you do this, it will just echo. What does echo do? Echo whatever you want. Echo is also a command which takes an argument. What the argument will just get echoed on the screen. Yeah. Whatever you type between the codes, you get echoed on the screen. For very simple things it is okay if you do not put the codes. It will still echo it. Now, if you similarly just do count, it will echo count. But that is not the purpose. Count is also variable whose value we want to see on the screen. So, we will put dollar. Now, it has shown this. This was the purpose. Now, if you think that Wonderland TXC you do not want, you in fact wanted only the number 4047. How will we do this? So, first let us see this. This was coming on the screen. The output of this, we can pipe it through and cut. Cut minus D paste minus F1. This shows only the number. This is something you would have seen now. You might be comfortable. Now, only the output of this. So, this is all completely executed. If you put this between both open codes, you have to put this entire command between codes. It is not a correct way to say. It should be between open codes. Both codes should be open. Unlike conventionally we write in a document, we open by open code and we close by close code. Not like that. This is open by open code, close also by open code only. Yeah. When you do that, then that output is now available for assigning to something else. It is a count equal to. Now, you would echo count. There was a question earlier that somebody had missed, had missed some recording because of some sound was not okay. Also whatever I am typing here, all this is since it is being broadcast through a view, it is also available in stored form in inside a view. Is that correct Sushant? So, you do not have to see on the screen quickly note make note etcetera. It is all available as a video for you to see later. We will encourage that you spend time now understanding and asking us questions now because whatever is typed is also available later. So, important is no space before and after the equal to sign. Please note many programs do not mind a space before and after the equal to sign. But here when assigning the value to a variable, you should not have any space around the equal to sign. Inside the shell script, how do you write comments? Let us go back to this. Inside the shell script hash means comment. Because hash is a comment I told in the previous slide that when you do hash exclamation mark, there is an important difference unlike some other comments. We also saw that variables can be assigned with the output of commands. We already saw open quote is used for this purpose. Echo is used for echoing the output etcetera. Command line arguments. So, we might want, please see the previous slide again. What does this do? It does cut some cut and then it shows some two second column until the end. Then it will also paste output as a last column of students dot txt, last the next few columns and then it will sort them. Why last I said because of this hyphen. Then the output of that instead of coming on the screen is going through this pipe and it is getting sorted. After it gets sorted somebody had asked how do you save the output? As soon as you do this redirect it will create a file with this name. What is this tilde? All other things is clear. Tilda means home. As I said we have a home directory which is the home directory that we can see using the shell variable called dollar home. So, that same dollar home is another shortcut for that is this tilde. Tilda is this other key above the tab button. You have to use shift to press that tilde. So, this is the variable, this is the file name into which the output has got stored. Now, this is a script you might have written. You might have written a script in which some new mark somebody gives, there is some processing that gets done and finally, the output gets saved into this. Now, suppose somebody gives you a new marks sheet for the next semester. We as teachers have to again again deal with lot of processing of students marks. So, unfortunately correction work is inevitable. How much of you do not like you have to do the correction? At least this processing will write a shell script and do some make the life easier. So, now if we run the same script the new semester marks will override will overwrite this marks, this results of txt file we wrote last time. So, it is possible that we want that this script we should not change only each time this file name we should be able to change. We should be able to specify from the command line that this file we will now specify different file. So, this is how we will do. We want that from the way we are using this particular script, we will give on the command line one particular word. That word will go and sit inside the name of this file. In order to do that and instead of that place we will write something else. What will we write? We will write dollar one. We also want that we will write just name of the file it will automatically go to so and so directory that is how this particular program has been made. So, each time you use this particular command you will write explicitly what dollar one is. It expects you to write dollar one. There is no option here. You have to write this argument it is compulsory. There is no option is not the correct usage because I said there are some options like minus d is an option for the cut command. So, here one argument is compulsory for this shell script that is written here. That particular argument the way this program is made will be the file name automatically inside the marks directory which is inside the home directory. And then it will also echo some results generated word on the screen. So, dollar one means first argument first command line argument the command itself is dollar zero. Please note we are counting one two three as the arguments excluding the command itself. Command itself is not an argument it is that is why called dollar zero. Dollar n is the nth command. You can write a file you can write a own script which requires ten input argument ten arguments to that command perhaps path. I suggest path is something you can look up yourself because we are short press we are hand press for time. So, we want to start the latex session hence path is something you will look up yourself carefully. How do you do if else in bash? How to write if some file is there then do this? If that file is not there then you print on the screen that that file is not there. How do you do all this? So, how do you do for loops, while loops? How do you check whether two things are equal whether one number is greater than another whether one number is equal to greater than or equal to etc. So, all comparisons are done inside a command called test. So, test is a command that will so you can do man test and it will give you a big list of various things you can compare you can see for example whether or not a file exist. Whenever you are doing if else that time you want to evaluate something for example, you want to evaluate whether this number is equal to another number you want to evaluate whether or not a file exist. All these evaluations are like tests. So, there is a big list of tests whenever this test evaluates to true it will do it. If the test does not evaluate to true then it will go to the else thing that is written here. So, the man test gives a big list of tests. Let us see a simple example. So, test minus D all these options for test are extremely self-explanatory. If test minus D dollar 1 then echo yes the directory is present as I said this particular slash is just because in this slide there is not enough space to write it. When you do copy paste then this will come on separate lines and then there will be a problem in executing it. So, what does this do? It takes this particular program takes argument from the command prompt and then it will check whether the directory exist. If the directory exist then you say yes the directory is present. Whenever you start with F you have to end it with F i this is the reverse of F. This is the syntax. I should say a batch script is extremely readable unlike many other languages here the syntax is very much readable and you know exactly what is happening. Notice that between these double quotes also dollar 1 is understood. Dollar 1 is understood as it is evaluated as the first argument that was given to this command when it was called. You can also have a else. If this is true then do this. If it is false then do something else. If the command that is given here is less than 0 then do this. Notice that each time you save it into a file be sure to give execute permissions to that file before trying to execute it. Otherwise it will say permission denied. Permission denied by whom? Who is denied this permission? It is our file. We are the owner of the file and it is permission denied. Just give the permission yourself to yourself. If you do not like this test command there is also the square bracket. This is like shortcut. This also makes reading the code easier. I would recommend using this for various purposes. We should use the square brackets. It makes it easier but this is just an alias for test. Alias means what? It works exactly the same shortcut. As it says here if there is a square bracket then better leave spaces. So, here first hash not just a simple comment line. Second and third are just comments. Hash in bash script is comment. So, how to use a date command? So, please see help date. Date. You can type date. After today's session type date on the screen. We will show you the date. Different computers have different syntax of presenting the date. You can also change it. Whether you want the time to come in ampm or 24 hour clock. Whether you want month first then date or then and then year or like in India we have first date then month then year. You can change the syntax how the date should come out. Once it comes out you can do this cut etc. Please note this open code. When you are typing I expect that most of the questions in Moodle will be because the open code and close code were typed not the correct way. So, open code when you use for the first executing whatever is between the open codes that is very different from using a single closed code. Close code looks as I said only vertical. This is also very self-explanatory. So, dollar hour. Dollar hour this continuation of this. So, hour is getting defined inside this line and there is this now. So, please read the slides carefully and try to understand what is it that it is doing. Is it really doing this good afternoon, good evening, decision business that is what you will check. In some countries exactly at 6 o'clock afternoon changes to evening. One minute before 6 o'clock they will tell afternoon suddenly they say evening. So, this is the program to decide whether it is afternoon or evening. Here it is not so much issue. For new line slash and for new line if you want the echo to not just come in one line, but two different lines. So, this is a syntax for that. There is also for loop for variable in list. So, I should say this particular cat is a, it should not been blue, right. For animal and rat, cat, dog, man. It expects that for variable in a list. This can be any long. It should not be blue. See, we are using something called a listings package. We are here to start latex. Inside latex we have written all these, these batch scripts are directly called into latex and latex has been told that these are batch scripts. That is why all this coloring is also automatically done. We have not spent too much time in saying for should be different color, do should be different color, etcetera. But that batch script that latex also has realized that cat is also a batch command. So, it has put cat in different color. So, this is a little too blindly, doing this blindly. It should not be color blind. Now, this is also very self-explanatory. If you want to have many numbers in the middle, especially for numbers, then curly bracket usage is important and also no spaces there, right. So, you execute this, you will immediately see. Why we have a right to go fast? We expect everybody to have seen the slides before. In any case, for those who registered late, can also see the slides later today. So, that we are able to utilize today and tomorrow time very effectively. Trace and cut, we have already seen. This is, as I said for, for example, replacing something, replacing all spaces. If you have a big list of files and plenty of these files are spaces. Why? Because, because they were not, they did not show the initiative to attend this course and they understand that look spaces are not allowed in file names and directory names. Because of this, that situation, it is possible that they have renamed the files very badly by putting spaces in the middle. I really expect that none of you all will ever put spaces inside file names, not in windows also. Here is a program that takes a big list of files and replaces every dash by, every space by dash using tr command, using the translate command, which we went a little too fast and did not elaborate. So, please see this also. Here is a place where dash hyphen has been used for the cut command. There is also while, just like this for f, there is also while. While some variable is satisfying some condition, just keep doing this. So, please be sure that you do not want to infinite loops. Here, like in that, like in f then else, here also if you are using square brackets, then the space just before the closed bracket and the space just after the open bracket are very important. This space is not important actually. This space is not critical. Prashant the way is helping me with these things. He will come here later, when we start the latex session. What is this exit? Exit is not so important. Try running this program without the exit also. Exit is important if you want to have an exit status. For example, you are executing something and you want to know whether it has worked properly or not. That is when it is possible that it terminates because of some particular problem. If you wanted to tell you that it has exited after completing all the tasks, that is when exit is important. I would like to say that please do not worry about that now. Environment variables. So, we define these variables. Variables are of two types. One is something called environment variable. The count was one of the variables we defined and we gave the value of the word count of insert some file we assigned to the count variable. That is also variable. Some variables are called environment variables. It is very important to know these environment variables to utilize them instead of our programs, but it is important that we do not assign some value to environment variables. Why? Because environment variables are being assigned by something else inside bash. For example your home directory. Home directory is telling you where all your files start from, where all the various configuration files are, where the personal configuration files are. So, those variables should not be touched by your bash scripts. So, that is home as I said OS type. Many programs want to know that that bash script is being executed inside which operating system. What is an example? An example is that you might be using, you might be using Firefox. You might be using some an internet browser called Firefox. This Firefox might be behaving differently whether you started from Unix or from Linux. Even with this Linux it might be behaving differently from different Linux versions. So, OS type is an environment variable that has this precise information. Depending on what the OS is the Firefox can now start in a different way. So, Firefox program perhaps wants this and hence you do not touch this and modify it. Otherwise Firefox running might get affected. So, you can try this and see different environment variables. Path I said is also very important for keeping your programs. If you write a program you want to keep it somewhere, you can keep it in your bin directory inside your home and then set it in the path. So, please read this also carefully. So, path is also already a variable which you can append using the colon. Extremely important tool is find when you want to find a particular file, when you want to find a pattern inside all files. For example, this is one feature we all use much whether it is Windows or Linux that there is some file you wrote 3 years ago. We all do not now spend so much time keeping track of the latest file. In this course we will also do version control which will teach us that. But find is a very useful command that will for example tell all the files in a directory. If you are looking for all the files which have some particular name you can use that as a next option. Through them if you want to look for a pattern then you can use that other open code and look through them all. You can grep through them all. Extremely useful thing to man find. Find is extremely powerful. Its complete capability is not known to me also yet. It is extremely useful I would like to say. CMP. Please read CMP yourself. This just says whether two files are same or not. CMP for compare. If they are not the same you would like to know what is the difference between them. If it is a command that tells what is the difference which lines are different. It does not compare just line by line. If some set of lines are gone down because some new lines are inserted it will tell you exactly these lines are the same. These lines are not the same it will tell which lines are here, which lines are there, which lines are only here, which lines are only there, the line numbers all that information comes out in depth. So, depth takes two arguments, two arguments which are files. Tar. Extremely important when we ask you to submit programs. So, tar ball is like zip. One of the things that irritates many of us much is for example, even if somebody zips a collection of files if they combine them all we need to combine them all. Because for example, we want to send a collection of many small small small files to various people. Are we going to attach them one by one? Instead of that we would like to zip them all, zip the entire directory and send us one tar ball. You would like to send them as one zipped file containing all the small small files. I was saying there is something that irritates me. What is it? Somebody creates a zip file in windows to use Winzip you need a license. Such a simple application, what application? Collection of all files zipped. You want to unzip them you need a license for this. Is this something that one should have to pay for? For just opening a file which somebody has decided to combine for convenience of sending. Best part of Linux, best part of open source is all these simple applications why should one pay somebody else for that. So of course zip is one way of combining all various files for which unzip is the command in Linux. Tar is another way of combining various things. So let me tell you tar just combines them all it does not compress. You can use an additional option of also compressing it. So after you use tar and combine various files in a directory to one particular file it will be called a tar ball. A tar ball is one file containing plenty of files. This tar ball can also be compressed using additional options. So default is not to compress it is just to combine them into one file. This is how one extracts X for extract. Generally we in most programs we for verbose. Verbose means speaking too much. It will give you all kinds of information only part of which might be useful. So as initial users we all prefer that all these commands are verbose. You also please use that. Only grep command I noted v means invert it will invert the output. Instead of showing you the lines which has a pattern it will show you only those lines which do not have the pattern. Except there most other commands v means verbose. This is how you extract out if it is a dot tar file. Dot tar means that it is been created by tar. It means only as far as our understanding is concerned whether it is really a tar or not is what file will anyway know. File command will anyway know. Creating an archive also you can see yourself. It can also compress using different compression algorithms. Which compression algorithm you need to use depends on what option you give. Customizing your shell. So you might want some particular preferences like that prompt. You might want that prompt to have more information. You might want the prompt to have the current working directory inside the prompt. All those additional changes are possible by putting this dot. Dot files. All these dot files are not visible when you do ls. They become visible only when you do ls-a. So these extra files have this information. That completes using Linux tools. We are already running late. As far as the latex session goes. We decided to complete this using Linux tools because this is extremely important. So please spend lot of time on this. Also please spend time on latex. We are banking on you to do much work yourself there. That is why we are going fast. We will take a few questions on using Linux tools. Please ask your questions now. We will take a question from VIT Vellore. Sir, writing a shell script. It should always start with hash symbol followed by an exclamation mark slash bin slash bash. Is it the way we should write? Because I did not clear with that one, that shell scripting. This is very recommended. If you do not type it, what happens? You can try with that file command I said. And in any case if you are executing all this in bash, it does not matter. If you are running it in bash, then do not worry about it. If you are running these in some other shells, then it could be relevant. So if this is the first time you are writing a bash shell script, then do not worry about this. It is not important for the first timer. So this question does not require further discussion. Please tell. Sir, comparison if, if else and then for, we will be using it in C programming for comparison for looping purpose. Sir, in what way it will be useful in this kind of environment like Linux? Sir, you see, just for comparing the lines. In C program you are doing something like some number, manipulating some numbers, equality of strings. Here you need bash script for manipulating files existing in the directory. For example, if some user is the right person, you would like to give him some access. So you want to now compare whether this person has the right access, he is user. You want to for example, if this guy, no, if such a file exists in the directory, then you do this. If that file does not exist, you do something else. So all this manipulation we are typing by commands in the bash shell and hence we want if then else to be possible in bash script. So if you are doing command line manipulations there you might need if then, then you cannot write a C program for just this much. We will have some exercises. The best way to answer your question is, we will have some assessment questions in which you will see that to perform the same objective to write in C would be very, very unnecessary and difficult. To write it in bash would be so much easier. There will be, we will have some assessment questions for that purpose. Okay sir. Thank you sir. Thank you. Yeah, please ask. So how to give the execution permission in the batch program file itself? Your question forced me to ask this answer to Shrikanth. So inside the bash script it is not possible. You have to come out of the bash script and give the permission to that particular file and then try to execute it. Inside the file it is not possible to do the best of our understanding. But you please pose this on moodle for further discussion on this. This is a very good question. Okay, thank you. Yeah. Sir, is there any difference in slash ETC slash profile file and slash ETC slash bash RCE file? The difference is not much. Difference is not much. In some operating systems there is no difference. This is a really subtle difference. I do not know the exact difference. It is more of a convention to keep certain things in profile, certain things in dot bash RCE. Thank you sir. Thank you. Yeah, yeah please ask. Sir, hello. Yeah, hello. We can hear. Please ask. Sir, can you use SH? Yeah, SH instead of bash, SH is self-treated instead of bash. Can you please repeat the question slowly and loudly? So, can you use SH? SH instead of bash, yes. See, as I said SH was the original shell. Extremely simple. Yeah, it is very easy primitive. In many ways it is primitive. And then, then BSH came I think and then bash came. Yeah, then born shell came, then born again shell came. So, we are now interested only in bash. That is the most popular also I should say. So, we will not worry about just SH. Yeah, but they all have the same extension. Okay, okay. But in my system only SH is never working. And I am not getting the bash front. But are you using Ubuntu or which version of Linux are you using? Yeah, Ubuntu. Ubuntu is same thing. We have a central CD. I am using your CD only over DVD. DVD version. See Prashant here says that you just remove the first line. Anyway, due to, you please put this question on Moodle. We will try to see why that has happened. Prashant here says do not put the first line in bash scripts. Even after, even inside SH, you just type bash. Type bash and come and start the bash shell. Yeah, type bash and start the bash shell and then you do everything as if you are in bash. Okay, thank you. There are some questions on chat which we will see quickly. Somebody has asked me to repeat in short about grep. Yeah, grep is very important. Certainly we will do it quickly now only. Okay, now about grep pattern. So, yeah, the question was how do you use grep? Yeah, so grep, pattern, filename. This is how one uses the grep command. Grep, first argument is a pattern. Second argument is a file inside which you want to search that pattern. Then it will tell you every line which has pattern somewhere in that line. Yeah, this is the simplest use of grep and we will need it in this sense. For example, now cat primes, no, marks.txt. Yeah, so now inside this file, inside this plain text file, please note that it is a very plain text file. Suppose we want to see that has anybody got 82 marks, grep, 82 and filename. We have to tell inside which file to search for the pattern 82. This will tell, yeah, here is one line in which 8 and 2 occur and which is the line if you are interested in seeing the line number, these are all additional things minus n. It will tell you third line, yeah. There is no need to repeat more about grep. More things on grep can be done on Moodle. Thank you.