Hello, this is a re-recording of the 6 HD, so the Hackertools class lecture on shell and scripting We sort of realized that this is a very Sort of screen heavy lecture and it was really unfortunate that in the original lecture We did not actually have the screencast, so I figured I would just redo it with a proper microphone And with the screencast as you can see Hopefully you find this useful the content is pretty much the same as the original lecture and I recommend that you open up the lecture notes For this lecture, which is basically what I'll be going through adding some detail as we go along So the shell is a really efficient textual interface to your computer And it might be something you're familiar with it might not be But in general you can do almost everything through the shell and once you start becoming productive with the shell You should find that you don't really need your mouse all that much anymore The mouse is pretty much handy for the browser and not much else on your computer When you open the shell you're greeted with something that looks a little bit like what's on my screen right now So this is called your shell prompt And it basically lets you run programs and commands. There are a lot of common ones So for example, there's make dear. Let's just make a foobar directory So this made a directory called foobar In my home directory that I'm then now going to change directory into so I'm going to cd into foobar There are commands like LS which lists what files are in that directory currently there are none And there's move and copy. So for example, if I touch will create an empty file So I'm going to touch foo touch bar if I now LS you see I have the file files bar and foo MV moves a file so I can basically rename bar to bus and now the files are passing foo and I could copy foo Into foobar and now I have three files and the contents of foobar are the same as the contents of foo So this is sort of the very basic shell stuff that pretty much everyone knows But the thing is the shell lets you do so much more than that You can invoke any program on your computer and usually command line tools exist for Doing pretty much anything you may want to do and they're often a lot more efficient than there's sort of gooey counterparts their graphical counterparts And we're going to go through a bunch of those in this lecture The shell provides what is essentially an interactive programming language. It's often referred to as scripting And there are a lot of different shells that all have effectively different languages You might have heard of or probably even used either sh or bash So the prompt I currently have open is a normal bash prompt is what you normally get when you install any Linux system sh is so this is bash BASH the born-again shell There is also sh which is sort of the original shell The two are mostly compatible sh is a little simpler But in general sh you will have on any Linux system bash you will probably have but you're not quite as guaranteed And this gets weird once you go over to other Unix like systems. So Mac OS for example has sh But also has a really old version of bash. So if you're trying to write a script that will work across Different types of unixes you might want to skip stick to just sh. There are also Shells that match particular languages better. So for example, there's C shell CSH Which looks a lot like the C language There are other shells that are more geared towards sort of human use. They often they're referred to as like better shells So there is for example fish, which is the one I personally use Which they have a slogan that's something like finally a shell from the 90s There's ZSH Or Zeish, which is very popular which is basically a someone who took bash and then made a Version of bash that's still backwards compatible, but has a lot of the sort of nice features you will want in a real shell There's also the corn shell KSH that is pretty popular in general. I would say just You can choose any of these will all be fine What I will cover in this lecture is primarily bash because it is the most ubiquitous one But I would recommend that you look at either a zecher fish As sort of your many main shell because they are a lot nicer to work with and come with nice features like really good auto inline auto completion So shell programming is a very useful tool in your toolbox And you can the part of the reason for this is you can both write commands directly out on the command line Like this will in this command line at now will interpret what I write as a program that it's going to execute You can also stick these commands in a in a file So for example, I'm going to create something like a run dot sh file and at the top. I'm going to add this line bin sh And if I do this, this is known as a hash bang line if so here let's So if you try to run a program that starts with the hash bang line What will happen is Linux will take the entire file contents of that file And pass them to the program that follows the hash bang So for example, I'm going to mark this file as executable so that we can run it And here let's say that I do echo which just print something to standard out and say echo. Hello If I now run this file notice that what happened was it printed. Hello, not particularly interesting But it turns out that you can do really fun things with this like I can do a run dot pie where I set the hash bang to user bin Python and Then down here. I do print. Hello from Python And notice that if I now make run dot pie executable and run run dot pie This now ran Python code and so this hash bang trick is really useful if you just have some short program You want to run and you want to run it through some other program Then a hash bang will basically tell Linux that the way you execute this file is take its contents and give it to this program So in this in this lecture will primarily use bin bash Although keep in mind that anything that I put in this file. I could have also just run directly from the command line So let's start actually let's start at the command line with something relatively simple Let's say that I just want to do run some command a bunch of times Well bash has for loops just like most other things although you'll notice that it's a little bit weird Let me just write it out here So this is an example What this is saying let's sort of walk through it line by line and or token by token and unpack it So this is you should read this as for x in list So this is the list. This is the x. This is just any variable And it's gonna actually with the body of the loop with x set to every value in this list Um semicolon terminates a command and New lines are semicolons semicolons are new lines And so this would be the same as if I put a new line right here the semicolon and for loops required that there's a semicolon before the do Also, notice that for does there bash in general does not have curly brackets Which means that any construct that that adds a block whether that's a For loop a while loop an if statement case like switches They basically have a unique keyword for starting the block and ending the block So in this case do starts the four block and done ends the four block In other programming languages, this will be an open curly bracket and a close curly bracket Now the way this actually works in bash is that this list is really just space separated so It's gonna assign I for every space separated element in this list So how does that work exactly? Well, we'll get back to white space splitting a little bit later But basically this thing right here Let's just run it to see that it works If you try to run this command, you'll see that all sec i5 does is print the numbers from 1 through 5 and so this is what is known as This is what is known as program substitution So basically what it does is it executes this program in Here and then it replaces this entire thing with the output of that program Which means that this loop is equivalent to writing this Those are exactly the same and then bash is going to so if I run this it does the same thing Bash is gonna split this by white space and then it's gonna assign i to each of these values and run the body of the loop Which is just echo. Hello, which is why we get hello five times with i set to that value Notice that everything in a shell script is a command so in this case echo. Hello Echo is also just a program on your computer. In fact, if we type which echo Which echo echo is a program that's located right there and what echo does is it prints out its arguments It's very non-fancy And in fact all of the commands that you can run this way without specifying exactly where they are The way the shell finds them is using a variable called path So the path is a colon separated list of directories where your shell should look for programs So when I type echo what the shell really does is it walks this list of paths and in each one It searches for a program called echo and if it finds it it runs that one and stops searching in my case Echo is in user bin so it would look at this one not find an echo look in this one not find an echo look in This one find an echo and then run that program Also notice that we have variables right so I here in this for loop is in fact a variable And what that means is we can also do stuff with that variable So if you go back here instead of echoing hello, we go to echo I and what we then get is the numbers one through five notice that this is basically the same as just running sec I five and We could do something more useful right so in here we could do for example LS which prints the files in the current directory and do something like for F as in file echo F And this will print all of the files in the current directory Right We can also set our own variables if we want in bash so you can do something like foo equals bar and Echo foo Notice though that bash is really really picky about syntax if you do this What bash will interpret it as is? Run the program foo with the first argument equals and the second argument bar Which is probably not what you wanted and so for any variable assignment you really do need to use it without spaces There are a bunch of special variables to that are defined in all scripts. So if we go back to our run dot SH program there is dollar zero which is the name of the current program so the sort of the name of self there's dollar one through dollar nine which is That argument to the program So for example if I now run the run SH with a B and C as the arguments Then dollar zero which we printed first prints the name of the program itself a dollar one printed a and we could Print dollar two to get B dollar three to get C, etc There are a couple of other things like You can echo dollar pound which prints the number of arguments and dollar dollar Which is the process ID of the current shell of the current program, right? So in this case dollar zero was still the same Dollar pounds of the number of arguments is three a B and C and this is the process ID of the shell That's running that program So let's see if we can do something more useful. So we have this for a less Let's say that we only want to print directories. So let's make some Directories here. We're going to make dare one and dare two So if we LS here, you'll see that dare one and dare two are both directories That's what this leftmost bit indicates these other bits are is it readable? Is it writable? Is it executable the first three are for user the next three are for the same group So in this case the users group and the last is for everyone who's not the user and not the group So let's say that we only want to list the directories in this folder Well, this will list everything which is not quite what we wanted and so of course bash also has if statements So I'm going to write it out and then explain what it does Okay, so What we did here was utilize an if statement So we're only going to echo out dur And then the the sort of the file that we were at if this is true now This construct might look a little bit weird the setup here is basically if Condition then oops Then body And then fi right so here we have then is sort of the open curly bracket fi is the close curly bracket Condition is a command that the shell is going to run and in general any program you run Will always but especially in the command line will exit with a particular exit code So for example if we look at And that the exit code will always be printed in or stored in dollar question mark So for example, if I try to remove a file that does not exist So this told me that it does not exist if I echo dollar question mark It says one if I try to remove a file that does exist like foo and Then try to echo dollar question mark it brings zero in general any non-zero exit code means a failure whereas zero means a success and What if condition does is it runs the command and if its exit code is zero then it executes the body Otherwise it does not test here is also a program that's on my path which test and Test I recommend you look at the man page for test But test basically takes an expression that it can be all sorts of things But that it's the kind of things you expect like parentheses and or string comparisons number comparisons Also various kind of tests on files. So you notice I use dash D dash D file Exits with true if file exists and is a directory, which is what we wanted in this case, right? So for this loop if F is indeed a directory then tests is gonna exit with status code zero and then it's gonna do this echo So that's kind of cool. You can also of course do else if So there's like an L if construct Where you give some other command? There's also an else if you want to do else And I recommend that you take a look at man tests tests can do a lot of really useful things one thing that you might have noticed is that Often times you'll see this in bash scripts instead Right notice that it does the same thing. Well Open square bracket is not that special Open square bracket is Also a program on your machine. It is literally called open square bracket And in fact if we look at man test, you'll see that a different alias Sorry a different alias for test is open square bracket So when you what this is saying to bash this statement right here is run the program open square bracket with the arguments Dash D dollar F and close square brackets This close square bracket is the final argument and in fact if you run test with as open square bracket It requires that the last argument is a close square bracket So You might think that okay great We sort of did this correctly, but we run into some really weird cases like what if I make a directory called my documents Right. Let's see what happens if I run this now notice that my documents does not appear in this list Even though we made it as a directory and if I LS It is there Right, and it is a directory So let's think about why that is well remember how the way bash knows how to loop over things is By white space splitting this so LS is going to list my documents But when we loop over all the items of its output My is going to be an item and documents is going to be an item, but not together So in fact we can test this just by removing the if and echoing dollar F Oops Yeah, that's not what I meant to do at all. That's even less what I meant to do You'll see that when I do this loop You see my and documents show up as separate things Right, even though when we actually list files they show up as a single thing And this is a really big source of bugs in bash like this behavior on white space base splitting is Just all sorts of problem. I can it will cause you headache down the line So let's spend a little bit of time on trying to understand exactly how that bit works So bash splits arguments by white space and that is not always what you want and often the way to fix it Is by doing quoting For example Up in this example remember how this is really just process substitution, right? So we could just as well say Der one der two my documents This is what the previous command expands to if I know that my documents is a single string I can do this I can quote it and now Notice that it'll run, but it will exit with a different error and the error is that down here You'll see we have the same problem here with our if If F is my documents F is going to be just put here as my documents Which means that test or open square bracket gets the arguments dash D my Documents and square bracket. It is not expecting two arguments to follow dash D It's only expecting a file name and so here We would have to also quote this variable in order for that to work and then indeed it does work Echo sort of happens to be okay because what echo will do by default is it will print its arguments space separated Right, so it's going to get my end documents a separate arguments But it's going to print them space separated so it happens to work out in this particular case But what if a file contains something like a new line, right? Like what if I make a directory called like bad Documents that Hat M is a new line So if I LS you notice here It gets all sort of like when it tries to print out the files It has to do all this escaping to show me what that file is if I now try to include that sorry here as a Bad documents you'll notice that Echo gets all sorts of confused because this new line is just like tripping it up entirely in fact What happens here is the new line is stored as a carriage return We just go to the start of the line And so it prints dur. My sorry dur bad and they go to the start of the line and then it prints documents Which overwrites the previous stuff it wrote which is probably not the behavior you wanted So Of course as as far as I've showed you so far Quoting is sort of the solution, but that doesn't really help right if I want to let me just remove this bad file. Oh man Great Because it's not always the case that I can just like replace this dollar LS with that right Because as for me, I don't necessarily know the files in the current directory. That's sort of the point That's why I'm running LS in the first place And so I can't just do the substitution And so how do we fix this like how do we fix the script to do what we wanted to even assuming that we quote this correctly? Right this still does not work because of my documents So you might imagine that I can do something like this But think carefully about what that actually means this means run LS Take its output and place it where this dollar open this dollar parentheses is What that means is all of the files are going to be a single string So if I run this I'll get nothing and We'll see why if I try to echo dollar F here Oops like so Well do It just prints all of the files as a single string and that is because here We're just expanding LS into a single quoted string so they will not be subject to white space splitting Which is also not what we wanted Well, so what is the solution to this so the solution to this is a really handy thing in bash called globbing so in basically Bash knows how to look for files that match particular patterns For example, you have star which means any string of characters so match any string of characters You have question mark for any single character So let's see how that works. So for example, I can do LS Star and that will show me that will run LS with the arguments of what star expands to star in this case Expands to all of these Let's do just to simplify a little So I'm gonna make a few files here bar buzz Okay, so if I do LS star then star expands to all of these and LS will just list all those files Similarly, I could also do echo and it would be given all those same things Question mark is handy for when you don't necessarily want to match everything. So for example This matches any three letter file name Right, so it will not print run pi and run SH because they are six letters I also have curly brackets, which let me say something like anything that starts with B or R Star Notice that this did not print foo. So this expands to echo B star R star and B star expands to bar Buzz and our star expands to run dot pi run dot SH and you'll notice that is in fact what got printed out There are a couple of other globbin tools too, but none that are particularly important in this particular setting So going back to our for loop here what we can do is just instead of doing LS We just prints the files in the current directory. We can just do star And bash when you use globbin it knows that those are file names And so it knows sort of what to split by and so this will actually assign F to every file in the current directory No matter what the name of that file is so independently of spaces And if I now go back and make this My documents thing and run it my documents get printed the way we would expect it should be We still need to make sure to quote it when we use dollar F elsewhere right like here Otherwise, this would still get it as two different arguments You can make pretty advanced patterns to with globbin, right? So you can do things like Anything that starts with a or you can do something like Any dot text file in the directory foo relative to where I am or you could do even more advanced things like this So any file that starts with P followed by two characters that ends with dot text in any sub directory of foo Run this command for Right, so you can combine these in interesting ways and basically name exactly the file that you want or files that you want Unfortunately white space issues don't quite stop there. For example, if we go back here Let's do something like if foo equals bar then Echo Z Actually, let's make this I guess dollar one If I run this H with no arguments Sorry, if I run it with bar then it doesn't need print Z if I print it with Baz it does not print Z If I run it with no dollar one set notice that Tests start complaining or the open square bracket program, which is test starts complaining the reason for this is The it is being run the so the test program is being run with no first argument So if this is an empty string bash does not pass it as an empty argument it expands to no arguments So test this is being run with the first argument is equals and the second one is bar, which is definitely not what you wanted, right? And if I this is a sources of a lot of troubles and bash scripts one work around that exists and that is pretty commonly used Is to do this? And maybe you see why this works right now even if dollar one is empty This first argument is not empty and the comparison would still go ahead In fact, we could test this and this is a hack that's actually quite widely used now Bar will still work and the empty string will not fail So this is a pretty common fix, but it's not very satisfying, right?  So if I do oops If I do which Double open square bracket, it says there's no such program in the path It did not find that program and that is because it is a bash built-in and this program or this built-in Understands the syntax underneath it understands that this should always be an argument It basically works the same way Tesla's like double square brackets is pretty much a Reimplementation of tests, but where it it understands the bash systems underneath specifically With double square brackets, even if dollar one is empty, it will still be counted as the first argument So if I now do run a sage bar, it will work correctly bars will print nothing and empty will print nothing And so you no longer need this x-hack But notice that this is a bash built-in. So if I tried to run this with sh and On my system sh is bash. So it doesn't actually work But if you were to try to run this on the system that actually just had sh like if it had ash, for example See, maybe we can just it's not important But so in general if you use double square brackets, you will want to declare bin bash at the top not just bin sh This also if you use the syntax you can also do things like double and Double ampersand for and double pipe for or those kind of things Which are harder to do with the with the built-in test program there. You sort of have to use dash a dash o for and or Okay, so we've talked a little bit so far about Sort of the kind of cool things you can use the shell for and how to do very basic scripting But where the shell gets really powerful is once you start talking about composability in particular The shell is powerful because it lets you compose multiple programs Specifically you can chain multiple programs together Rather than have one program that does everything and the key character for doing this is pipe this character if you write a pipe B what that really means is run a Also run B send all the output of a as input to B and then print the output of B To understand this a little bit better you need to understand that all programs all processes you launch have basically three streams They have an input stream, which is known as standard in written like this they have a primary output stream standard out written like this and they have a Sort of secondary output stream called standard error, which is where you're supposed to write error output if any occurs and By default standard that your terminal is going to say that standard in is your keyboard and standard out is the terminal display Which means that anything a program? So if I write Python program for example, and I use the print function What that will do is send the characters I gave to print to standard out Which just so happens to be my screen and therefore it gets displayed similarly if you use something like read keys Or input what that will give you is characters read from standard in Sorry print goes to standard out input takes from standard in But of course your terminal can change that Basically if you type a pipe B what that's going to do what your shell is going to do is it's going to change A's standard out to be equal to B's standard in so when B reads from standard in using something like input It won't receive from your keyboard It will receive from A's output and similarly when you call print in a what that will actually do if you print or echo or whatever It will print a standard out, but standard out will be B's standard in But B standard output will still be your terminal and a standard input will still be your keyboard You you have some manual control over this too, and you can play around with it like let's say that I echo foo, right? So Actually, let's do Let's do cat. So cat is a very simple program It reads from standard input and writes the standard output. So if I type echo and Then presenter then it printed out echo Notice the reason I see this twice here is because my terminal sort of be helpful to me shows me the characters I'm typing so it shows me my standard in But this is what I type on standard in so that is what cat received and then cat printed back the stuff It got on standard in just it's standard out What I can do is I can do something like cat Open angle bracket or sort of close angle bracket means change standard out to point to this file So here I could do cat dot text now if I type echo and then terminate cat with Control D which is sort of end of file end of input if I now if I now print the contents of Cat.txt you can echo is a bad example here. I am a cat If I cat that file if I if I print the contents of that file, you'll say it says I am a cat So here when I cat it into this file what happened was standard out of cat Became this file and so anything I type to standard into cat Ended up in that file similarly I can use open angle bracket to change standard in to be the contents of a file So here I'm telling cat That standard in should come from here instead of my keyboard and cat just prints whatever comes on its standard in So it prints that I am a cat. In fact, I can redirect both So this is saying hey cat your standard in is going to be the contents of this file and you your standard out should go to this file So if you recognize this is basically copy, right? If I now cat cat text It says I'm a cat and if I get cat to text it also says I am a cat And if this is confusing to you, I recommend you play around with it a little You can also redirect standard error Using to and then that or you can redirect everything in bash using this But those are less important. So so let's go back to pipe because that is where we started and That is arguably the most useful these commands. Why is this useful? Well, what it lets you do is it lets you manipulate the output of a program for example if I do ls Let's say that I only want to Include files that like contain an oh, I could do this with a glob But it might not be what I want instead I can pipe it through the program grep So grep is a program that searches for a pattern in all of its inputs So I could grep for oh And what that will do is for every line in ls It'll pass that line to grep grep would search for the character Oh and only print a standard output if the input contains an oh And I could make this more complicated too So grep supports more advanced parameters than oh like I can write full regular expressions here I'm not going to go into that in this particular lecture because we cover that in the data wrangling lecture a little bit later in the semester But this is handy because now I can write one tool that is good at searching and I can use that across multiple places For example, if I type ps it lists all of the programs I currently have running if I run ps a ux it prints all the processes currently running on the machine And so I can do ps a ux and then I could grep for like I don't know grep for my name and I will show me all the lines in The process table that contain my name anywhere in the line and Notice I did not have to May like implement that searching feature in ps instead ps just knows about printing things Right printing processes and greps know how to search and the pipe Let's us combine those two and that is really powerful because you can go a lot further with that for example I can do journal CTL which shows me the entire system log of my program since of my machine since the last boot I can pipe that through something like grep for Intel So this shows me all the messages I guess I should have done dash B for the last boot search for everything that contains Intel the string Intel Or like Let's say anything that says kernel So that's a lot more messages and then I could say pipe that again to Tail which prints only the last n lines and do you say tail dash n 5 So if I run this program, I get the last five messages from my boot log that contain kernel And you'll notice that journal CTL did not know to have to know anything about searching or about looking at only some lines Grep did not need to know about how to print system logs or how to only select a subset of its inputs and Tail did not know have to know about system logs or searching But still the the pipe lets us combine these in useful ways And I really recommend you look at the the data wrangling lecture for a lot more on on how to build interesting structures like this I Can also combine programs in other useful ways like for example One thing I might want to do is I want to send an email to myself Showing me who is logged into the system. Well, I can do that too who prints everyone is currently logged in I can pipe that to send mail which is a program that takes as input an email message and sends it to a particular email destination And I could say like me at example comm and now if I run this program It's going to send the list of who's logged in as an email to this mail address assuming I've set up send mail correctly Bash also provides a number of other ways to compose programs although many of them are even more Sophisticated and and maybe not something you'll run into it very commonly, but we'll mention them briefly You can group commands with things like a Semicolon B in curly brackets. So for example, I could do Journal CTL be grep I Intel Journal CTL be Actually, it's a better example of this Let's say that I want to search for everything that contains an O in both the files and the Actually, no, let's just say I want to search for my username in both the process list and the list of people who are logged in Sorry, that should be a parentheses I Guess let's do head instead So notice that now it ran who and PS and it combined their output Into grep and then I took the head five and so notice that this will give me these three are lines from who and these two lines are from PS And a lesser known a Lesser known but really useful one is process substitution I think I've mistakenly called the old one process substitution This is not process substitution. What I'm about to talk to about is it's not terribly important what these are called So process substitution is really weird, but it is basically If you write what's a good example of this Let's say that I want to look at the difference between between two different The output of two different programs. So for example, I want to look at the difference between my last two boot logs So journal CTL dash B minus one shows me my latest system my latest boot log Journal CTL dash B dash minus two shows me the boot log from the previous boot If I want to show the difference between these I could do this log one dot text Log two dot text and there's a program called diff Where I can give log one dot text and log two dot text and it will show me the difference Maybe not in a way. That's particularly helpful right now But nonetheless it shows me that difference What's inconvenient here is that I need to give two file names to diff right diff only has one standard in and it it can't It's a little tricky to actually compare to the output of two commands because you can only provide one command on As sort of the standard input to that process and this is where process substitution comes in handy if you write this like so What this will do behind the scenes is bash is going to start this program and start this program Create sort of a temporary file that is going to put all of this program's output into and then it's going to take That temporary file name and give that as this argument instead of whatever is here So if I if I did echo here See that the arguments that echo actually saw itself receive are these Which are just sort of weird file names for things that the kernel created for us or bash created for us rather That happened each one to contain the output of these programs respectively And so if I ran diff on these I would get the same thing as if I wrote them to files individually But without having to create the intermediate files One thing so let's let's move on a little bit from composability and talk more generally about the ability to run multiple things one thing you might find is that Sometimes you want to run longer term things in the in the background a good example of this is if you want to run say A server of some kind and you also want to run a client Well the ampersand suffix if you put that at the end of a command it tells bash to run it in the background It will and it will give you back your prompt immediately. So let's make an example of this. So I'm gonna create server We're just gonna do what most servers do which is sleep for half an hour So if I run server you'll notice it sort of takes over my prompt right this is just gonna sleep But imagine that it's some larger like web server or something. I can't do anything now, right? If I type LS nothing happens. I have to wait for that. I have to kill that server So control C kills the current process and then I can type LS But if I really wanted to like let's do client as well Success because that's all clients ever do so if I run client that print success if I run server But imagine that I have to run the server and now I want to run the client I could open a new terminal, but I sort of don't want to do that like there's no reason why I should have to do that And so the ampersand suffix is really handy if you want to run Two programs at the same time for example I can do server ampersand and now what bash is telling me is okay I started process I started what you told me as Process ID this and this is the job number. This is sort of the background job number of Server and now I can run client right I gave a gap in my prompt back And the client is gonna presumably connect to the server and do whatever it needed to and print success And if I type jobs it'll show me that this is one of the background jobs It's currently running and I can bring it to the foreground with FG and then percent one So percent one is job number one if I just leave it leave off the the percent Then it's gonna assume that I mean percent one and indeed then I get back the server If you have a thing that's currently running in the foreground and you want to move it to the background You can press ctrl Z So what ctrl Z will do is it will stop the currently running process and put it in the background Right. So now if I do jobs, you'll see that this process is in the background But it's currently stopped and then I could type BG percent one to say run this process in the background And if I now do jobs, it'll tell me that this thing is now running again in the background And this is a good way to sort of move between multiple programs in the same terminal session Of course, if you're just trying to run things that are relatively disparate like an editor and a server or something like that You might want to use something like Tmux or screen which we're gonna talk about in the next lecture on The command line environment But this kind of low-level job control is really handy for when you just like want to start a server and a client for example One thing you should be aware of let me just kill this for a second If you run a server like this, you can imagine you can also run this in in a script like run.sh, right? If I run server and and then I want to sort of keep around what hand what What process that was I can do this so dollar Explanation mark is the process ID of the thing that was last spawned in the background under the process that was last run and now I can sort of Echo waiting and then I'm gonna like run the client echo done And then I'm gonna kill so kill sends a signal to a given process Telling it to for example shut down so the default signal that kill will send is the kill the kill signal which basically Tells that program to shut down and you give it a process ID So in this case that would be the PID that we stored from up here And if I now run run sh and then run jobs You'll notice that there's nothing running in the background because the script sort of cleaned up the thing It ran in the background So what about other stuff that's running in your process actually one more thing that's useful Let's say that I run the server and now I'm trying to like I want to log out from the machine Well, first of all, I'm gonna lose all the outputs. I probably want to start this in the background with something like server log Right But let's say I ran it this way and now I want to like log out from my terminal Well control C to put in the background to stop it and put in the background BG to run it in the background But now if I quit this shell the shell by default also shuts down any of its background Processes any of the background jobs and so there's an additional step you have to take disown Which basically says I don't want to be responsible for this process anymore So even if I die let this process keep running as you can do disown percent one and now that will keep running in the background No matter if you log out from your SSH session or whatever Okay, so let's look at Other kinds of sort of similar things which is other stuff running on your computer So now we talked about background jobs and you've already seen ps right so ps lists all the other things that are running on your machine ps-a will show you all the process running from all the users on the machine And there are a lot of arguments to ps So man is sort of your friend on the command line Which will tell you more about the different processes that are or the different commands you have available to you So remember we looked at man test right there's also man ps There's man for most of these commands and usually the man pages will be pretty helpful about all the possible arguments You can give them sometimes it might be a little overwhelming, but if you if you Open this and then type slash you can search in the man page for terms that are relevant to you Like I could do search for something like all and enter and then end to go to the next matching Next next matching line If you want to find a process so Let's say that I run the server in the background And now from some other shell or later on I want to find the process ID of that server Well pgrep for process grep will take the list of processes and Search for anything that contains the given name And so in this case I could do pgrep server and it will tell me that there are two process IDs That have server in the name if you include dash af it will also search the entire command line So it also search arguments and it will print out Which command line that is running so in our case this shows us that I had process ID this 20,000 156 is in fact the server program that we ran just above here And let's say that we want to kill things something's later.  So this signal tells the process to exit right now, so if I do kill dash nine and then a process ID What was our I guess 20 156? The dash nine signal is going to force that process to exit immediately It does not get an option to like clean up anything. It just has to exit right now The nicer way to tell a program to exit if you just pass kill or if you do 15 or term So sig term is sort of a polite way to tell the process like hey, it would be great if you quit right now The way these map to commands if you press control C What that really means is you're sending sig term. So you're saying please exit Sig kill is tell the kernel to forcibly close this process right now and it's equivalent to control backslash So if you press control backslash that immediately kills the current process no matter what it's currently doing You very rarely want to do that because it means it won't get to like flush things out to files and those sorts of things You generally want to stick to control C similarly to with kill. You usually just want to kill So if we look at man kill You usually just want to use Not specify the signal because then the term signal is sent, which is the polite way of asking something to exit and As they say here it's used in preference to kill because you sort of want to allow the process to perform clean up before it terminates So There there's one more thing I want to cover before we finish up here, which is this notion of flags Over the over the course of the commands we've run so far You'll have noticed that in general for most commands like if I type LS or ps or pgrap or kill Many of these take what known of flags. So these are things that start with a dash So for example ps I did dash capital a For pgrap we had dash af in general flags or weights to Modify the behavior of the program you're running. They usually come in both a short and a long version So if we look at man pgrap You'll see here That there's for I can either pass dash dash count or dash C and they're basically equivalent or dash dash delimiter or dash D Usually so this is a short and a long version for every flag and the long version usually has Double dashes while the short version has single dashes Usually you can also combine the short ones. So pgrap dash af is the same as pgrap dash a dash f Normally, there's also a dash dash help or dash h which prints a short version of the man page basically it prints you sort of a Condensed version of the help for that program and this can be a very handy way to Get a quick overview of what you can do with a given program and what kind of flags it supports There are A number of flags that are pretty common that you'll see in almost every application So I already mentioned dash h for help Dash v usually means verbose. So for example if I curl Google.com so curl is a command line program that fetches the Basically the website at this address and downloads just downloads all the html So if I curl google.com, I guess get this value And if I do dash v it'll be more verbose about what it's doing So in particular it'll show me all the htp steps that it did so usually dash v means verbose often You can pass multiple dash v's to make it even more verbose or just combine them because it's a short flag Similarly usually dash capital v means version So for most tools dash capital p v will print out information about which version of that software you're running Dash a commonly refers to like everything we're all So for example, you'll notice if I do ls here I get these files if I do ls dash a it also includes dot and dot dot Which are ways to move up. So so dot is always the current directory dot dot is always the parent directory And normally those are not listed but with dash a which Translates to dash all Dash all prints everything And finally dash f usually means force So normally let's say that you're trying to remove a file It might say that this file is like right protected or something like that So it will refuse to delete it if you pass dash f it will force remove that file even though it would normally not let you do that So generally you should be careful with using dash f flags There are sometimes you want to create files and this is a this is a pretty neat trick Sometimes you want to create a file that starts with the dash and this can sometimes be a little bit tricky So let's look at touch touch is a program that lets you Basically just create so It lets you change the time of a file like when it was last modified But if the file does not exist it creates it. So it's a handy way to just create files But let's say you'll notice that it has an argument Called dash a let's say I wanted to create a file called dash a well if I do touch dash a It says missing file operand because it didn't get file. It thought that was dash a So how do I create this file quoting it didn't help? Well, normally for most programs if you do double dash And then dash a What the double dash means is everything that follows the double dash So everything from there and to the right You should not interpret as a flag You should only ever interpret as sort of positional arguments like file So if I do this you'll notice I now have a file called dash a and I'll have the the same problem actually with rm So if I do rm dash a It's going to think that what I meant was the dash a flag to rm Which there might not even be one in fact if I do rm dash a it'll say invalid option So here I can do the same thing double dash dash a And now dash a is gone uh That's all I plan to cover for the shell lecture. Um, I recommend that you go to the The hacker tools website and look at the lecture notes for the session where we also have a number of exercises That are useful just to familiarize yourself more with commands how you can combine them And sort of interesting additional tools that are really handy to know about I also recommend that you go watch the uh command line environment and data wrangling lectures if you found this interesting Because they go into a lot more depth on how you can combine these tools And how you can make your experience of working on the command line. Um, just sort of better in every way how you can Use other tools to improve your experience on the command line and be more efficient But this at least should have given you an introduction to the what basically what the shell is and the kind of primitives So you'll be working with along the way So thanks for tuning in uh, and we'll see you in another lecture. Bye


have been discussing on flow past bodies of various shapes and if we recollect that in our previous class we identified that there could be bodies of different shapes which are generated out of considerations of various elementary flows if you start with a potential flow consideration. Maybe one of the important things of concern for such cases is that if you have a wall or if you want to represent the existence of a wall how do you represent that? For example say you have a wall like this and say you have some source located at this point say a which is basically on the y axis where wall is the x axis. So you have a source of a given strength located at a wall is not given to you say only the location of source is given to you now you are told that now we have a wall we should have a combination of superposition of flows which would represent the effect of this wall. So how do we do that? Given this source is already existing we have to keep one thing in mind what is that? We are talking about a potential flow in this example when we are talking about a potential flow it means that no penetration boundary condition is the only boundary condition that we have to satisfy at the wall. So if you have a source like this see the source radially emits flow in all possible direction that means it also emits a flow radially in a downward direction. Now from this do you have a clue that what we should do to make sure that there is 0 normal component of velocity at the wall no phi equal to 0 or rather streamline or a stream function equal to 0 represents a body of a particular shape because the surface of the body is a reference streamline that is true. But how do you ensure that with this physical example? What more you have to get to ensure that yes this is such a line where you have no penetration. So it is the inverse problem it is not that you generate a body of a given shape given a body or given a wall what extra thing you need to have with this to ensure that you satisfy no penetration at the wall. Let us say this is a plain wall just as an example just think like this it is a very common sense thing. Say you have just like a reflection you have image source located at this point. So if you have a image source located at this point see that it will also have its own radially diverging field and one of the velocity directions is this I mean it will be perpendicular to the wall. So if these two sources are of equal strength and located at equal distance from the wall then these two effects will nullify each other to have result in normal component of velocity 0 at the wall. So this is a very simple concept but maybe implemented in practice very easily. So it will now become a superposition of a particular say source and an image source where the image is with respect to the reflection as if the wall is like a mirror. So a superposition of that so this is known as method of images very convenient method and one can generate the effect of the wall using this. Now we have also discussed that not all flows are potential flows in fact no flow in reality is a potential flow but potential flows are important because if it is a boundary layer theory that you are applying then outside the boundary layer you may apply the concept of potential flow and whatever pressure gradient that you calculate out of that the same pressure gradient is imposed on the fluid within the boundary layer. But because of the existence of the boundary layer you have the viscous effects also important and that dictates what should be the drag force on a body. Of course if you have very high Reynolds number flow and a bluff body or a body of such a shape that flow separation occurs quite quickly then the form drag or pressure drag remains to be the more important concern than the skin friction drag and then the drag coefficient sort of may become independent of Reynolds number at a very high Reynolds number. Now if you have say a flow where viscous effects are important then you represent that with a drag coefficient where the drag coefficient is a combination of the skin friction drag and the form drag or pressure drag. Now the drag coefficient will have in general a dependence on Reynolds number. In certain cases for very high Reynolds number flow because the form drag effect is very important so it may become virtually independent of Reynolds number. But otherwise because of the skin friction effect it becomes a strong function of the Reynolds number otherwise. So let us look into 1 or 2 problems where we illustrate or we try to see that what are the consequences of that. So let us consider 1 example. Say you have a flat plate but this plate is oriented in a bit of a special way. The dimension of the plate is given. The dimension of the plate is given as the height is delta and the width is l and you have a free stream velocity. The free stream velocity is not a uniform velocity but the incipient stream is like a boundary layer already developed by itself and assume that this is a turbulent boundary layer where you have the velocity profile given by u by u infinity is equal to y by delta to the power of 1 by 7. This is given. You have to find out that what is the total drag coefficient or the net drag coefficient because of the interaction between the fluid and the solid plate. What is given is that for a turbulent flow you may assume that the CD is 0.031 by rho ql by mu to the power 1 by 7 that is given. This is the CD based on a local velocity, this is the CD net that is the net effect on the plate what is the equivalent CD that you have to find out okay. So to find out the CD what you require? You require to find out what is the total drag force. So to find out what is the total drag force which is there acting on the body how you should go about it. See if you take a thin strip say at a distance y you take a thin strip of width dy. What is the drag force that is acting on the strip? See why we are taking such a strip because we want to use our knowledge of boundary layer for flow over a flat plate. For flow over a flat plate which is oriented just instead of this say oriented in such a way that a uniform flow is flowing on the top of that then the reference drag coefficient is based on uniform u infinity. Here as if the u infinity which is coming on the top of this plate is varying it is varying from 0 to u infinity as you are moving along the height of the plate. So if you take a small strip over which the local u infinity is the local u. What is there? What is this u? So then if you calculate that what is the local drag force? The local drag force is the local CD into half rho local u square into the area l dy. Of course in practice if it is a slender plate like this fluid is flowing on both the front and at the back. So you may multiply this by 2 for the 2 sides and you can substitute CD as a function of u and the total drag force you may integrate dfd from y equal to 0 to y equal to delta and u as a function of y is given. So that you have to substitute for doing the integration. So I am not going through all the integration but if you calculate the total drag force it is 0.031 into 49 by 62 into rho nu to the power 1 by 7 l to the power 6 by 7 u infinity to the power 13 by 7 into delta this is the final answer to this problem. And of course the next CD is something which is artificial what is more important is the drag force because you may just use any reference velocity say you may use the reference velocity as u infinity and very easily find out the CD by the drag force divided by half rho u infinity square into delta into l is the area of the plate. So but important is what is the drag force effectively that is what is more important. Let us work out another problem. So you have a sphere which is moving in a liquid the sphere has the density as rho s and the liquid has the density of rho the radius of the sphere is r. So you have to find out how the velocity of the sphere varies as a function of time if it starts from a velocity v0 at time equal to 0. Assume that it is moving in the vertical direction okay. So now if you see just if you consider the free body diagram of the sphere. So what are the important forces that you see on the sphere what are the important forces that you see on the sphere if you draw the free body diagram you have the weight you have the buoyancy force and drag force. So if it is tending to move upwards the drag force is trying to make it move downwards. So when we say that when the sphere is tending to move upwards it means relative to the fluid. That means it might be so that the sphere is stationary but the fluid is moving downwards from the top it is all the same. But when we are writing equation of motion for the sphere we are writing its velocity as velocity of it relative to the fluid that means as if the fluid is stationary. So if the it is moving upwards relative to the fluid then you have the drag force. So you have the buoyancy force – the weight – the drag force is equal to the mass of the sphere into dv dt that is the Newton's second law right. So of these things the buoyancy you can very easily write what is the buoyancy 4 by 3 pi r cube into rho into g – 4 by 3 pi r cube rho of the sphere into g drag force it depends on the relative velocity that is what is important. So you cannot say what it is you can just write it as say cd into half rho v square into the reference area. So that is pi r square right again the projected area is the reference area for these cases so not 4 pi r square that we have to keep in mind. So that is equal to 4 by 3 pi r cube into rho of the sphere into dv dt. So the whole attention now is that what is the cd right and that depends on the Reynolds number of flow. So this is a function of Reynolds number if the Reynolds number is very very low say Reynolds number less than 1 then the Stokes law is approximately valid. So if Reynolds number is much much less than 1 you have the cd is 24 by Reynolds number based on the diameter of the sphere and there will come another velocity. If on the other hand Reynolds number is very very large very large Reynolds number cd may be approximately a constant independent of Reynolds number. Because at very high Reynolds number we have seen that the effect of the form drag for bodies of with curvatures become more and more important. And the reason is as you as you say as you increase the Reynolds number what happens what happens to the boundary layer boundary layer becomes thicker or thinner. If you increase the Reynolds number the boundary layer becomes thinner. So when the boundary layer becomes thinner then when the surface effects of the fluid are there then of penetrate into the outer fluid outside the boundary layer and they may perturb it sufficiently and that of course is it becomes more and more important. So form of the geometry of the surface tend to become more and more important as you increase the Reynolds number. Now at very large Reynolds number cd is approximately a constant independent of Reynolds number. So if you draw say cd versus Reynolds number for flow past a sphere. So what you will get out of this? So what you get is something approximately like this. So let us say we plot with the log of Reynolds number. So cd equal to 24 by Reynolds number if you plot it as a log of Reynolds number it will be like a straight line but that is valid only for very low Reynolds number. Then there is a significant deviation from this and so it will come down like this and there will be a certain transition as at a particular Reynolds number when the cd goes down. For this transition the cd was approximately as a constant. So it is not really a exact constant but it is almost like an asymptotic like it is almost approaching a constant then suddenly there is a transition again it increases. This is roughly say for a sphere maybe it is of the order of 10 to the power 5 this Reynolds number. Now what happens here? Here there is a transition to turbulence because of a transition to turbulence what happens? The boundary layer separation is delayed. We have seen that like when the shape of the body is important in terms of the form drag boundary layer separation is what matters because early separation will induce a lot of form drag but later separation will reduce the form drag. So boundary layer when the boundary layer becomes turbulent the separation is delayed. So it reduces the form drag and since at very high Reynolds number form drag is what is important the reduction of form drag is what is prominent here. But beyond that if you increase the Reynolds number further then many interesting things may occur because I mean it is a change of the state of turbulence from one state to another state where the point of separation again shifts more towards the upstream. So and then that is why so this is all related to the boundary layer separation. So what we are trying to understand is that towards the high Reynolds number for flow past bodies with curvature the boundary layer separation is a very important phenomenon that dictates the total drag force. And the similar behaviour is there for flow past cylinders and let us try to understand what happens for flow past circular cylinders as a special example. So we now look into some of the animated representations or movies related to flow past circular cylinders. So one by one we will see and we start with a particular visualization where it is almost like a potential flow. So if you see that these green coloured lines these represent sort of stream lines and if you see that these stream lines are very symmetrically coming from one side and they are also symmetrically merging in the other side the front and the back side. So almost a perfect symmetry is maintained so just like what a potential flow could predict. Now if you increase the Reynolds number what happens? So let us say the Reynolds number is increased beyond say 4 or something like that. See at very low Reynolds number the solution is almost like a Stokes flow solution. So the solution does not consider the fluid flow or the advection terms in the left hand side of the Navier-Stokes equation. So the advection terms advection effects are totally neglected in that solution. But in reality the advection terms may be important typically as you go far downstream and what happens is see physically what happens the wall is a source of vorticity because wall generates a velocity cross velocity gradient and that vorticity is transmitted within the fluid. So if there is a strong advection then the advection transmits the vorticity from one place to another place and creates an asymmetry in the flow. So if advection effects are totally neglected that asymmetry is totally neglected then vorticity is just diffused in all directions equally. But if the advection effects are there then vorticity is preferentially transmitted or advected towards the wake side or the low pressure or the back side and what is the consequence so whatever we showed as a visualization example let us see it as a simulated example. So if you see 2 eddies of counter rotating nature are formed at the back or in the low pressure region or in the wake side of the cylinder. We are talking about still low Reynolds number cases. Now if you say increase the Reynolds number you see certain interesting things. So if you see these are like staggered vortices which are forming. So there is one row of staggered vortex interacting with another row of staggered vortex and these are sort of rolling in the opposite direction. So if we want to look into it in a bit more detail manner let us try to look into another movie and with that movie what we will try to see is that what are the implications of these alternately rotating vortices. So these alternately rotating structures they have sort of an impression of footprints on a road and this was first observed by von Karman and that is why this is known as von Karman vortex street. So if you see that basically what is happening is these vortex lines so to say are interacting with the small eddies the 2 counter rotating eddies which were formed close to the cylinder because with increase in Reynolds number these vortices are coming very close to the back of the cylinder and they are interacting with those small eddies and so alternately you are having sort of staggered rows of vortices rotating one against the other. Now so we will look into one interesting flow visualization of vortex stretching by using sorry vortex shedding this is this phenomenon is known as vortex shedding that is as if you have flow in a in a there is a flow past a cylinder it is not just for a cylinder it could be for body of any similar shape but cylinder is just a demonstration. So what we want to see first before going into the phenomenon of vortex shedding in more details that what types of vortex rolls are formed in the back of the cylinder. So in the wake region or the low pressure region so like animated description of the von Karman vortex street so just look into this very carefully because this gives you an idea that what sort of vortices are there not in a quantitative sense but at least qualitatively what is the nature of rotation of this individual vortices and these vortices may be of quite complicated nature and one of the important things that comes out of these experiments is that although the flow that came towards the cylinder was steady but there is an unsteadiness in terms of appearance and disappearance and the frequency of the rolls which are appearing. So although the incipient flow is steady but flow at the back of the cylinder is unsteady and it has a sort of a frequency or it has a sort of a periodicity in terms of the happenings that is the vortices are appearing, disappearing again appearing, disappearing. So vortex of one particular directional rotationality is appearing then vortex of an opposite rotationality is appearing and the previous one is disappearing. So alternatively vortices of opposite signs are appearing and disappearing and this phenomenon is known as vortex shedding and is one of the very important phenomena that you can see in flow past bodies of these shapes. Now you can generate these types of flows in using computational techniques also and this illustration will give you an idea of like how the vortices of different rotating nature are shed from the cylinder as if they are being shed from the cylinder towards the fluid. So you can see this clearly from this illustration. Now we will see some more examples of the vortex shedding. This is also an animated description. You can see that like alternating vortices are shed in the fluid from the cylinder and that occurs for quite a large range of Reynolds number and it is important for many reasons because we will see through one example that what effect it has on the cylinder itself. So with a bit of a higher Reynolds number, this oscillation frequency this also changes and we may have different experimental visualizations of these ones. So this is one experimental visualization unlike the computational visualizations that we saw in some of the previous examples. This is an experimental visualization of the same thing. So we are just trying to look into this vortex shedding phenomenon from different visualization perspectives. This is a computational way of like generating the similar type of behavior and you can see from this example clearly that these vortices are as if rolling upwards and downwards upwards and downwards like that and that creates an alternating sort of frequency in the flow. So this is a flow itself is inducing some nature of rotationality or some nature of frequency in the system and if you see that because of this sort of transients in the flow, the cylinder may itself start oscillating. So this is called as flow induced vibration of the cylinder. So it was a steady flow, it was not an unsteady flow. So if something vibrates because of unsteadyness in a flow that is intuitive. But here the flow that was incipient to the cylinder was steady. But because of this physical phenomenon of vortex shedding, you are now having some phenomenon which is occurring with the frequency and that is forcing the cylinder to oscillate and it may be very critical if that forcing frequency becomes same as the natural frequency of the cylinder because that can give rise to a very high amplitude and sort of resonance and that is one of the important consequences that may occur in a very strong fluid structure interaction. So you may have even multiple cylinders and with multiple cylinders these types of effects you can see with like as if one effect is getting superimposed on the other. Here the symmetry sort of is very well preserved because the Reynolds number is not that large but the symmetry may be destroyed by advection of vorticity if the Reynolds number is substantially larger. Now if you want to look it in the form of a 3 dimensional structure. So you can see that by the example that we will be seeing here. Let us try to see this example, maybe we just play it again. So if you see this is a 3 dimensional structure. So here the vortex shedding phenomenon is shown in 3 dimensions but the basic phenomenon is very very similar to the 2 dimensional flow visualization observation that we have seen. Now if you want to say see the behavior. So these like the vortex shedding frequencies are very very critical for the design of a say structural system and it may be that it may be possible that there is a wide range of Reynolds number to which the flow is subjected. So if you want to look into the flow past a circular cylinder we could see that first there were there were some small vortices which were formed close to the back of the cylinder. Then these vortices almost periodically appeared and disappeared and interacted with the main flow and created a vortex shedding phenomenon. Now if you increase the Reynolds number to a very large value then beyond a critical Reynolds number which is roughly like maybe of the order of 5 into 10 to the power 5 or so. And there may occur a transition to turbulence. So if we want to plot the pressure distribution around the cylinder. So when we plot a pressure distribution we basically plot the Cp versus theta. So Cp is the pressure coefficient p-p infinity by half rho u infinity square. So the physical situation that we are considering is there is a circular cylinder and there is a flow past this circular cylinder. Flow is coming with a velocity of u infinity and theta is a sort of angle measured with respect to the incipient flow. Now let us try to make a sketch say from theta equal to 0 to theta equal to pi. The variation of the coefficient of pressure which is basically a non-dimensional variation of pressure as a function of theta. First of all let us plot it for a potential flow. So for the potential flow if you recall that it was 1-4 sin square theta. For flow past a circular cylinder without any rotation. So if you want to make a plot of this say this is pi by 2 and this is pi. So when theta equal to 0 this is equal to 1. So let us say this one when theta equal to pi by 2 it is – 3. So let us say maybe this one. So if you make a sketch of it like this and then when theta equal to pi 1 again. So something like this okay. So this is potential flow solution. Then will the potential flow solution be valid in terms of pressure distribution? If there was no boundary layer separation then it would have closely followed this because whatever is the pressure that is imposed from outside the boundary layer same pressure distribution the boundary layer is supposed to feel so long as the boundary layer theory itself is valid that is so long as there is no boundary layer separation. But if the boundary layer separation has occurred then so if the boundary layer separation has occurred so it all depends on whether the separation is early or late. And for that let us consider say 2 Reynolds number one is Reynolds number these Reynolds number are based on the diameter D of the cylinder. So RED let us say one value we take as say 2 into 10 to the power 5 another Reynolds number we take as say 7 into 10 to the power 5 and let us say in between there was a transition. So let us consider that 2 into 10 to the power 5 is the laminar case. So if it is a laminar case let us try to make a plot of that. So if you compare these 2 in which case there will be earlier boundary layer separation in the first case right because the turbulent boundary layer has a greater momentum to sustain the adverse pressure gradient. So its separation will be delayed. So we expect usually that the separation will be there in the region where the adverse pressure gradient is failed. In reality where in the region where there is an adverse pressure gradient that is failed there occurs a back flow and the effect of the back flow is propagated even a bit in the upstream direction. So that the separation in the practical case for a flow of this Reynolds number regime occurs at roughly theta equal to 82 degree that is the practical observation. So till the separation has occurred maybe it will not show a lot of deviation from the potential flow theory but as it comes to separation it will show some sort of deviation from the potential flow theory. So this is Reynolds number equal to 2 into 10 to the power 5. See after the separation has occurred the CD the Cp is roughly a constant that means the pressure in the low pressure region or the wake region after which the boundary layer separation has occurred is almost like a uniform pressure which does not change further with theta and it is typically a low pressure region. When it is 7 into 10 to the power 5 say it is beyond the critical limit or the threshold limit. So then the separation will be what? It will be further delayed and typically this separation may occur close to 120 degree or something like that maybe 180 degree this type of angle and till that limit. So if we draw that line so till that limit it will it will follow very closely to it will follow very closely the line which was there for the potential flow solution and then when there is a flow separation it will come to a constant CD which is independent of the constant Cp which is independent of the angle theta that is a constant the pressure in the wake region is almost like a constant. So this is Reynolds number equal to 2 into 10 to the power 7 and these angles are like roughly for the angle from which this change of behavior is observed depends on the separation for laminar close to 80, 82 degree maybe for turbulent close to 120 degree something like that. So you can clearly see that depending on whether the boundary layer is laminar or turbulent you have a different pressure distribution right and this all is because of the different points of separation. So that means if you have on one side of the cylinder a laminar boundary layer on another side of the cylinder a turbulent boundary layer because of their difference in the point of separation you might have a different pressure distribution around the 2 sides and that is what is a very important understanding that we get out of that. It is not only true for a cylinder the similar behavior is also true for a sphere and because of this difference in pressure distribution maybe around the 2 sides of a sphere because of maybe the boundary layer is laminar in one side or turbulent in another side it is possible that the sphere experiences a lateral force. So if it is a ball it starts swinging because of that and we will try to now see that how the dynamics of the sports balls are associated with these sort of observations that we had till this time. So let us look into some examples related to sports ball dynamics. So we start with an example of a tennis ball. So let us say that there is a tennis ball which is being tested in a wind tunnel. See if you want to test the behavior of say pressure distribution around the ball you may have the ball stationary and the air flowing past it because what is important is the relative velocity between the ball and the air. In reality it is the actual motion is the other way because you throw the ball and the ball is moving in the air. And here you are keeping the ball stationary and blowing air past it. So if you want to see that you now compare the flow past similar tennis balls one with a laminar boundary layer another with a turbulent boundary layer and you can clearly see that in the laminar boundary layer because of the early flow separation there is a large wake or the low pressure region that is formed with a sort of rotating structure of flow in the back. In the turbulent boundary layer that separation is somewhat delayed and you do not have such a large wake region. So that gives rise to a different kind of pressure distribution in these 2 cases.  Now if you see in terms of the rotation. Now rotation of a ball it will depend on unlike the lateral or the side force which depends on the on the drag force I mean here it many times may depend on a lift force not always but in certain cases because we have seen that if you have a body of a given shape and if you have a vortex which is one of the important constituents that is generating flow past a body of that shape then that will create a lift force and we could get an expression for the lift force as a function of the circulation and the free stream velocity and that applies equally to the sports balls as well. Now because there is a lot of cricket that goes on in our country maybe we will spend a bit more time on how these cricket balls have different types of motions. So we will briefly look into the fluid mechanics of cricket ball swing. So the whole idea is not to like be very detailed in how a cricket ball should swing it is one of the very important and active areas of research but what is more important is to give a broad overview of whatever simple things we have learnt in our boundary layer theory and flow past bodies of different shapes then how that may be applicable to the movement of the cricket balls that we very commonly see in different matches. So I mean everybody knows what is a swing but let us just very briefly look into it. So swing is a sort of lateral movement of a ball and this lateral movement is influenced by many things and we will see that what are the factors which influence the lateral movement and but what is important is to have a basic idea first of what is the speed of the what is the effect of the speed of the ball on this lateral movement and that is very important because we have intuitively seen that many of these phenomena are strong functions of the Reynolds number and given the diameter of the ball as fixed. So the velocity of the ball relative to the fluid or in other words velocity of the fluid or the air relative to the ball is what is sort of important. So on the I mean depending on the size of the ball it may be different. So for the size of the cricket ball it is at speeds of around 80 miles per hour or roughly 130 kilometer per hour the usual transition to turbulence may occur. So at speeds of around say 90 miles per hour which is considered to be good speed of a first bowler or 145 kilometer per hour you will have the flow as turbulent. Now the question is that if the flow is turbulent and it is equal in all sides of the cricket ball then it will generate and it is equal I mean it is turbulent in the same way in all the sides then it will generate no resultant side wise thrust or side wise movement. So what may be important in one case say with a brand new cricket ball is to have a turbulent flow in one side and maybe a laminar flow in another side and how that is possible it depends on many factors we will come into those factors one by one. But just let us first for the sake of clarity try to understand that we will be first talking about a conventional swing in terms of out swingers and in swingers. So typically the out swingers as you know are the swings where the ball deviates away from the batsman and in swingers is it goes towards the batsman. So we will try to see that how these swing balls are going to be affected in terms of a scientific perspective. So the science of swing the broad picture. So as we have discussed earlier that the key to have a cricket ball swing is to cause a pressure difference between the 2 sides of the ball it is not just true for a cricket ball it is true for any other ball that we may look for and this pressure distribution depends on the sort of local velocity of flow of air on each side of the ball. So the swing of course maybe sometimes generated by which sometimes by accident also and we will see that like accidentally even if you are not gripping the ball for swing how it may swing and we will see that. Now but to have an important effect what we can say is the boundary layer sort of it may separate differently on the 2 sides of the ball and that is what is the key thing. So the boundary layer separation dictates the pressure distribution. The boundary layer separation may occur differently on the 2 sides of the ball and the location of this point of separation determines the pressure distribution on the 2 sides of the ball and because of the differential in pressure the side force is generated on the ball because of the ball moves from one side to the other. So now the question is that like as we first were discussing that what should be the effect of the speed of the ball. Say the speed of the ball is 145 kilometer per hour and say it is a new ball it is equally turbulent in all sides. So the resultant side force is 0 so to say that means if you have a very fast bowler and is holding a straight sort of seam or maybe is not holding a straight seam but with an inclined seam what is bowling in such a way that the flow is equally turbulent in all sides it will not have any swing. So it is not necessary that very fast bowlers will be able to generate a swing. So what is the key for generating a swing that I mean it may be a fast bowler but the thing is that you have to have your speed in such a way that one side of the ball the flow will remain laminar and how that may be possible let me show that to you with the one example of a cricket ball. So if you look into this as an example so let us say that the bowler is bowling from the side in which I am standing. So let us say that the bowler is trying to bowl an out swinger. So the out swinger the typical grip is like this so assume that I am releasing the ball from this direction towards you. So it will the seam will be inclined or oriented towards the direction in which the ball is expected to swing. So typically in the direction of the slips for a right handed batsman. So now you grip the ball in this way so this is the typical way in which you grip the ball for your out swinger. Now consider that you have the ball moving in the forward direction. So if you look into it in the other way just think that the ball is stationary and the air is coming from the other side because the aerodynamics is what which is governing the behaviour. So when the air is coming from the other side see this seam is acting like a parter bar or a disturber on the flow. So when the ball is when the air is coming towards the ball when this seam is present so the seam will disturb the flow and if it is close to the critical Reynolds number because of this effect of the seam the ball will have a transition towards turbulence. On the other hand on the other side see because you have oriented the seam in the other way on the other side when the air is first coming it is not encountering the seam. So it is coming on it is just falling on the surface of the ball where the seam is not present. So that means if it is a bit less or it is close to the critical Reynolds number because of a very smooth surface the disturbance will be very less. So it will tend to be laminar. So that means the seam here is a sort of actuator of the turbulence in the flow. So because of this one you may have a net resultant force because now on this side of the ball you have a turbulent boundary layer. On the other side you have a laminar boundary layer this we are talking about a brand new cricket ball if a opening baller is bowling with this type of a ball. So then because of the resultant force on my left side this ball after pitching it will move in the air in that direction so that is the out swinger. Now if you want to baller in swinger it is just the difference is just in terms of the grip because the same policy holds so wherever is the seam and the ball is coming towards the seam there it will try to have the seam will create a disturbance and have a transition towards turbulence. So if you want to swing the ball in the other way you have to just the grip of the ball remains the same but the seam is now pointed towards the other end. So when the ball comes like this it faces now first the seam in the right side where it becomes sort of the turbulent flow on the other side it still remains laminar. The important thing is that you have to bowl it close to the critical speed. So if you bowl at a very high speed you cannot get this effect because both sides it will become turbulent and if you have very good cricketing captains sometimes they make use of these things in a very nice way. One of the very classical examples that I can remember is like it was 5-6 years back 2001 or 2002 so it was a Australia-South Africa test match and in that match what was happening is like I mean Australia started bowling and they opened with Glenn Magra and Brett Lee and what was happening is that Brett Lee was bowling from a direction in which the wind was blowing and he was not getting any swing. The reason is he was much faster than Glenn Magra and the air speed was helping the ball so that it was always beyond the critical speed so that it is turbulent in all the directions and despite using the new ball it was generating some pace but no movement and usually best bowlers best batsman will always be disturbed more by movement than just by raw pace. On the other hand for Glenn Magra he was bowling against the wind so his natural speed is not that high and therefore he was not able to disturb the batsman with a threshold speed and when Steve Orr realized it he quickly switched the ends of the two bowlers and it was done in just 2-3 hours very quickly he did it and then Brett Lee was bowling against the wind so when Brett Lee was bowling against the wind what was happening is he no matter with whatever speed he was releasing the ball because of the air resistance it came down lower than the critical speed in one side and then like it was possible to have laminar flow in one side and the turbulent flow in the other side and because of that sometimes he was getting a late movement or a late swing because it depends on the relative velocity between the ball and the air and that was being modulated by the air which was blowing by itself. So sometimes if you if one is bowling too fast then it is not so good for a swing. Now but this we are talking about a new ball and for the new ball which is being used for a swing it is important to see that it is not just a new ball that one utilizes for a swing it is also important to have a swing even with the older ball and we will therefore talk about 2 different or maybe 3 different types of swings with the older ball. So when you have a older ball the thing is that you are not solely relying on the seam because with the older ball if you have one side rough the roughness of the ball may itself trigger turbulence. So then you are not depending on the effect of the seam alone of course you grip in the ball in the same way but no matter whatever is your grip I mean say you are gripping it in a traditional way for a particular swing say out swing but now this side of the ball say is roughened. So the contrast between the roughness of the 2 sides between the 2 sides of the ball is if it is very very strong because of the high smoothness in one side it may be ensured in many ways we have seen many illegal ways in which many of the teams have tried to do it. So the key is in one side you keep it very smooth another side you keep it very rough and then the rough side will have an automatic triggering 2 turbulence and that using the even with the older ball you may be able to swing and of course again it will matter that what is the relative speed between the ball and the air. Now there are many factors which might affect the swing one of the important factors people have people believe is the humidity and we have always heard that in a highly humid condition the ball is expected to swing much more but there has been no evidence of that from a fluid mechanics perspective because the density change or the viscosity change with a relative humidity change is so small that the Reynolds number change is not sufficient and from a fluid mechanics perspective Reynolds number should have been the dictating factor. So perhaps something else happens in that situation and still it is an unresolved question in fluid mechanics. Now if you consider something called as a reverse swing. So the reverse swing the whole idea is that the ball will now swing in the direction not in the traditional way but in the other way and the whole idea is that if you now see that you grip the ball in the same way say as an out swinger like this but now you make this side of the ball you make this side of the ball rough. So you grip the ball like a out swing delivery but you make this side rough and the other side smooth just the opposite to the normal swing case and then what will happen is if you consider the flow that is there on the ball then you will have. So in the side where you are making it rough it will become turbulent on the side in which you have kept it smooth because of the effect of the seam it will become turbulent but the triggering of turbulence is different in the two sides. So it will not the flow will not separate in the same symmetric position in the two sides. So in two sides the flow will be turbulent but there will be a difference in the point of separation and that will generate a net side wise force which is just opposite to the way you are showing it by your grip. So a good batsman will know by looking into the side which side is rough and which side is polished that like what should be the nature of the swing. Now of course the reverse swing one of the things is that it is also a strong function of the condition of the ball and therefore like issues of tempering of the ball and roughening of the ball using illegal means have come into the play. A third type of swing which has not come into the picture till very recently is known as contrast swing. So contrast swing is such a swing which is not so much dependent on the seam of the ball. So unlike the traditional swings where the seam is held in an inclined direction I mean where you want to move it in the contrast swing the ball is held with the seam straight. So the seam is held perfectly straight and for a novice bowler it is much easier to hold a seam straight rather than holding it in an appropriate direction for the swing. So what happens in a contrast swing if you see that just look into this visual where you have in the left side a ball with a contrasting surface roughness flying through the air and the seam is straight up. So the name contrast comes from the contrasting roughness on the 2 sides of the ball. So in this case the boundary layer over the upper surface separates relatively early in the laminar state whereas because the upper surface is smooth whereas on the bottom surface it separates later. This asymmetry results in a side force and it moves in the direction which is facing in the other way. On the other hand if you just reverse the roughness and the smoothness you may make it move in this way and since the effect of the seam is not there even in a torn out ball which has got a lot of wear and tear and the seam is not very prominent you can have this type of swing and this is known as contrast swing. But like before closing our discussion let us just look into like some of the very good swing bowlings, great bowlers who perhaps never knew what is the fluid mechanics of swing but could produce the swing in a much better manner than anybody else in the earth would perhaps do. So let us look into this like Sarapraj Navaj was considered to be the inventor of reverse swing. So we will just slowly see some of the examples like you can see before this we saw Imran Khan then Wasim Akram and like these were great exponents Vakar Yunus these were obviously great exponents of reverse swing bowling and like this is a typical in swing yorker and you can see that what were the conditions of the batsmen during that time. Akip Javed was also one of the very good bowlers and of course you can see Shreya Bhakthar one of the devastating bowlers of the modern times. Now just finally we will see one clip like it was 2005 Ashes series which was known for a series where you had reverse swing, contrast swing and all these things. So you can see this like one of the balls delivered by Flintoff and another ball in the same clip delivered by Simon Jones and these balls will illustrate that how these types of swings are there. So the second ball will illustrate in a bit more interesting way so just look into it so like these are these are these are typical cases of reverse swing bowling and perhaps this is the first time that Englishman could find out that how to bowl good reverse swings and it was good enough for them. So look at the deviation in the ball and it was the ball was gripped entirely in the opposite direction it was it was gripped as if it was going to move in other direction. So maybe we just stop here today in this lecture and from the next class we will continue with the with the new chapter that is pipe.