 now. Okay welcome back and today we're gonna cover kind of couple separate like two main topics related to the cell. First we're gonna do some kind of cell scripting mainly related to BAS which is like the cell that most of you will start like in Mac or like like most Linux systems as the default cell and it's also kind of backward compatible through other cells like CSA it's pretty nice and then we're gonna cover some kind of other cell tools that are really convenient so you avoid doing like really repetitive tasks like looking for some piece of code or for some elusive file and there are really kind of really nice built-in commands that will kind of really help you to do those things. So yesterday we already kind of introduced you to the cell and like kind of like some of its quirks and like how you start executing commands correct in them. Today we're gonna kind of cover more about like the syntax of what are like the variables the control flow functions of cell. So for example once you're kind of drop into a cell say you want to kind of define a variable which is kind of kind of the first one of the first things you learn to do in a programming language. Here you could do something like foo equals bar and now we can access the value of foo by doing dollar foo and that's bar. Perfect. One quick that you need to be aware is that like spaces are really critical when you're dealing with bugs mainly because spaces are reserved and that will be for separate arguments so for example something like foo equals bar won't work and the the cell is gonna tell you why it's not working is because the foo command is not working like foo is not existing and here where it's actually happening we're not assigning foo to bar. What is happening is we're calling the foo program with the first argument equals sign and the second argument bar and in general whenever you are having some issues some like some files with the spaces you need to be careful about that or you need to be careful about like quoting strings to that. So going into that how you do strings in bugs there are two ways that you can define a string you can define strings using double quotes and you can define strings using single quotes. However for literal strings they are equivalent but for the rest they are not equivalent. So for example if we do equals dollar value is dollar foo the dollar foo has been kind of like expanded like a string substituted to the value of the foo variable in the cell whereas if we do this with a simple string we are just getting the dollar foo as it is and like single quotes won't be replacing and again it's really easy to kind of write a script assume that this is kind of like Python that you might be more familiar with and not realize all that and this is the way you will assign variables then it buzzes a lot like control flow techniques that we'll see later like for loops while loops and my only thing is you can define functions and we can access like a function they have defined here and here we have the MCD function that has been defined and the thing is so far we have kind of seen how to execute several commands by piping into them kind of so that we flee yesterday but a lot of time you want to do first one thing and then another thing and that's kind of like the the sequential execution that we get here here for example we're calling the MCD function and we first are calling it with the make their command which is like creating this directory here dollar one is like a special variable and this is the way that bus works whereas in other scripting languages there will be like RTV the first the first item of the array RTV will contain the argument in buses dollar one and in general a lot of things in bus will be dollar something and will be reserved we will be seeing more examples later and one we have created the folder we CD into the folder which is kind of like a fairly common pattern that you will see we will actually type this directly into our cell and it will work and it will define this function but sometimes it's nicer to kind of write things in a file and what we can do is we can source this and that will execute this script in our cell and load it so now it looks like nothing happened but now the MCD function has been defined into our cell so we can now for example do like MCD test and now we move from the tools directory to the test directory we both like we created the the folder and we move into it what else so a result is like we can access we access the first argument with dollar one there's a lot more kind of reserve comments for example there are zero will be the name of the script there are two to two dollar nine will be the second through the ninth arguments that the bus script takes some of these reserved keywords are more can also kind of be regularly used in the cells for example dollar interrogation mark will get you the error code from the previous comment and which I will also explain briefly but for example dollar underscore will get you the last argument of the previous comment so another way we could have done this is we could have say like in came the test and instead of kind of rewriting test we can access that last argument as part of the using the dollar dollar underscore like that will be replaced with test and now we go into test and there are a lot of them usually kind of familiarize with them another one I often use is kind of it's called bang bang you will run into this whenever you for example are trying to create something and you don't have enough permissions then you can do pseudo bang bang and then that will replace the comment in there and now you can just try doing that and now it will prompt you for password because you have super missions and before I mentioned the kind of the error command yesterday we saw that in general they're like different ways a process can communicate with kind of all the processes or comments we mentioned the standard input which was of course like the lesson like getting stuff through the standard input getting putting stuff into the standard output there are kind of a couple more interesting things there's also like a standard error extreme where you kind of write errors that happen with your program and you want to pollute the standard output it's also the error code which is kind of like a general thing in other programming languages of like some kind of way of reporting how the entire run of something went so if we do something like eco hello and we like query for the value is CEO and it's here because everything went okay and there weren't any issues and a CEO exit code is the same as you will get in a language like see like see what means everything went fine there are no errors however sometimes things won't work like sometimes like if we try to grab for foo bar in our our MCD script and now we check for the value is one and that's because we tried to search for the foo bar string in the MCD script and it wasn't there so like grep doesn't print anything but let us know the things didn't work by giving us like a one error code and there are some interesting comments like true for example we'll always have CEO error code and false we'll always have a one error code and then there are like these logical operators that you can use to some sort of like conditionals for example one way you also have ifs and else's that we'll see here but you can do something like false and eco oops fail so here we have two comments connected by this or operator where boss is gonna do here is gonna execute the first one and if the first one the important is gonna execute the second one so here we get it because the like it's gonna try to do the logical or if the first one didn't have a CEO error code is gonna try to do the second one similarly if we instead of use false we use something like true since true will have a CEO error code then the second one will be sure circuit and we don't it won't be print similarly we have an operator which will only execute the second part if the first one like run without errors and this same thing will happen if the first one fails then the second part of this statement won't be executed a kind of not exactly related to that but another thing that you will see is that no matter what you execute then you can concatenate commands using a semicolon in the same line and that will always print beyond that what we haven't seen for example is how will you go about doing kind of getting the output of a command into a variable and the way we can do that is doing something like this where we're doing here is we're getting the output of the PWD command which is just printing the present working directory where we are right now and then we're storing that into the full variable so we do that and then we ask for full we get our string and more generally we can do this thing called common substitution or but putting it into any string and since we're using a double quotes instead of single quotes the thing will be expanded and it will tell us that and we are in this working folder and our interesting thing is right now what this is expanding is to a string instead of kind of like I am this is just spending as a string and our nifty and like lesser known tool is called process substitution which is kind of similar where we will do and it will hear for example the less than parentheses some command and another parentheses that will do is that will execute that will get the output to kind of like a temporary file and it will give the file handle to the common so here what we're doing is we're getting where a lesson the directory kind of putting into a temporary file doing the same thing for the parent folder and then we're concatenating both files and this we might be really handy because some comments instead of expecting the input coming from the STD in they are expecting things to come from some file that is giving some of the arguments so we get kind of both things concatenated thing like so far that has been a lot of information let's see kind of like a simple like an example script where we see a few of these things so sample here we're having like a string and we have kind of this dollar date dull date is a program so again there's a lot of programs in Unix you will kind of slowly familiarize with a lot of them they just prints what the current date is and you can specify different formats and then we have these dollars here here dollar series the name of the script that we're running then we have dollar has that's the number of arguments that we're giving to the common and then dollar dollar is the process ID of this common that is running again there's a lot of kind of these dollar things they're not intuitive because they don't have like a mnemonic way of remembering maybe dollar has but it can be you just kind of have will be seeing them and getting familiar with it here we have this dollar add and that will expand to all the arguments so instead of having to assume that maybe say we have three arguments and writing dollar one dollar two dollar three if we don't know how many arguments we can put all those arguments there and that has been given to like a for and the for loop will in in time get like the file variable and it will kind of be giving each one of the arguments so what we're doing is for every one of their women's were given then the next line we're running the grep command which just search for a substring in some file and we're searching for the string foobar in the file here we have kind of they put the variable of the file to expand and yesterday we saw that like if we care about the output of a program we can redirect it to somewhere to save it or like to connect it to some other file here but sometimes you want the opposite sometimes here for example we care we're gonna care about the error code about this grip we're gonna care whether the grip run successfully or it didn't so we can actually discard entirely what the output like both the standard output than the standard error of the grep command and what we're doing is we're redirecting the output to dev know which is kind of like a special device in in Unix systems where you can like write and it won't it will be discarded like you can write no matter how much you want to there and it will be discarded and here's the kind of the great in our symbol that we suggested you for predicting the output here we have like a two greater than and as some of you might have guessed by now this is for correcting the standard error because those those two streams are separate and you cannot have to tell us what to do with each one of them so here we run we check if the file has foobar and if the file has foobar then it's gonna have a zero error code if it doesn't have foobar it's gonna have a non-zero code so that's exactly what we check in this if part of the command we say get me the error code again this dollar question mark and then we have a comparison operator which is an E for not equal unlike some other primary languages you have kind of like equals equals bang equals these symbols in buzz they're like kind of like a reserve set of comparisons and it's mainly because there's a lot of things you might want to test for when you're in the cell here for example we're just checking for the two values two integer values being the same but for example here the minus F and check will let us know if a file exists just something that you will run into very very commonly and going back to the example then what happens when we what happens when we go into if the file did not have foobar like there was like a non-zero code then we print like this file doesn't have any foobar we're gonna add one and what we do is we echo this like has foobar hoping this is a comment to the file and then we're using the pattern we just say of like greater gradient to append at the end of the file and here since the file is has been fed through the script and we don't know before and we have to substitute the variable of the file name we can actually run this we already have like correct permissions in this script and we can give a few example we have like a few files in this folder MCD is the one we saw the beginning for the MC function to have like some script function and we can even feed it the own like a script to itself to check that like it has foobar in it and we run it and we first we can see that like this different like variable that we saw that the has have been successfully expanded we have date has been replaced to the current time then we're running this program with three arguments there's random as a PD and then is telling us oh MCD doesn't have any foobar so we're adding a new one and this the being the script file doesn't have one so now for example to MCD has the comment that we were looking for and one other thing to know when you're kind of executing the script is that here we have like three completely different arguments but very commonly you will be giving like arguments that can be more succinctly given in some way so for example here if you wanted to refer to all the dot s8 scripts we could just do something like less as there is dot s8 and this is a way of kind of fine name expansion that most cells have is called globin and here as you might expect this is gonna say oh anything that has any kind of sort of characters and end up with s8 so unsurprisingly we get kind of example s8 and MCD there s8 we also have this project one and project two and if they were like a we can do like a project 42 for example and now if we just want to refer to the project that have a single character but not two characters afterwards like any other character we can use the question mark so question mark will expand to only a single one and we get like kind of we're a less in first pre one and then project two in general kind of like the the globin can be very powerful you can also kind of combine it the a common part is to use what is called curly braces so let's say we have an image now we have in this folder and we want to convert this image from PNG to JPEG or we could maybe like copy it or it's like really common partner not have two more arguments that are fairly similar and you want to do something about something with them as arguments to some common you could do it this way or more succinctly you can just do image dot open curly bracket then PNG JPEG and here I'm getting kind of some color feedback but what this will do is like expand into the line above and actually can like a csh to do that for me and that's what's happening here and this is really powerful so for example you can do something like we could do that's unlike a bunch of foods and all of these will be expanded and you can also do it at zero levels and it will do the Cartesian yeah we have something like this we have one group here there's like one comma two and then here is like one two three and this is gonna do kind of the Cartesian product of these two expansions and it will expand into all these things that we can quickly touch you can also combine the asterisk glove with the curly braces glove you can even use kind of ranges of like we can do like MKD here and we create the full and there were directories and then we can do something along these lines this is gonna expand to full a full be like all these combinations through J and then the same for bar like I haven't really tested but yet like we're getting all these combinations that we can touch and now if we touch something there is different between these two we can again showcase the the cell the the process of solution that was earlier say we want to kind of check what files are different between these two folders for us is obvious we just said because it's x and y but we can ask the cell to do this this for us between the output of one LS and your LS kind of unsurprisingly we're getting like x is only in the first folder and why it's only in the second folder and what is more is right now the the we have only kind of seen bus scripts if you like other scripts like for some task bus it's probably not best it can be tricky you can actually write scripts they interact with the cell in a lot of different languages for example let's see here Python script we'll have a magic line at the beginning that I'm not explaining for now and then we have like in process it's kind of like the Python it's kind of not by default trying to interact with the cell so you will have to import some library and then we're doing a really silly thing of just iterating over the sys.rgv one semicolon which is sys.rgv is kind of similar to what in bus we're getting us like the dollar zero dollar one etc etc it's like the vector of the arguments we're fitting we're printing in in the reverse order and the magic line at the beginning is called a siebang and it's the way that the cell will know how to run this program like you can always do something like oh Python script and then like ABC and that will work like I always like that that will work but what if we want to make this to be executable from the cell the way the cell knows that it has to use Python as the interpreter to run this file is using that the first line and that first line is giving it kind of the path to where that thing lives and however you might not know like different things will have probably different places where they put Python and you might not want to assume where Python is installed or any other interpreter so one thing that you can do is use the nth command so in June you can not know you can also give arguments in the siebang so what we're doing here is specifying or run the nth command that is for pretty much every system that like some accession but like for pretty much every system is called is in user bin where a lot of binaries live and then we're calling it with the argument Python and then that will make use of the path environment variable that we saw in the first lecture it's gonna search in that path for the Python binary and then it's gonna use that to interpret this file and that will make this more portable so it can be running my machine and your machine and some more another thing is that the bus like it's not really like modern I think it was developed a while ago and sometimes it can be tricky to debug and like by default the the ways it will fail sometimes are intuitive like the way we saw before of like full command node existing sometimes it's not so there's like a really nifty tool that we have linked in the electrodes which called cell tech that will kind of give you both warnings and like syntactic errors sort of things that you might not have quoted property and might misfire you have like spaces in your files so for example for extremely simple MCD SH file we're getting a couple of errors saying like hey surprisingly we're missing a c-bank like this minor interpret the correctly if you're running the different system also this CD is taking a common and it might not expand properly so instead of using CD you might want to use something like CD and then and or and then an exit we go back to what we explained earlier what this is what this will do is like if the CD doesn't end correctly if you cannot CD into the folder because either you don't have permissions doesn't exist that will give a non see like a non see your error and common so you will execute exit and I will like stop the script instead of like continue executing as I mean you're in a place that you are actually not in and actually haven't tested but the I think we can check for the example stage and here we're getting that we should be you see we should be checking the exit code in a different way because it's probably not the best way doing this way one last remark I want to make is that when you're wearing bascripts they're like a like functions for for them are this kind of like a difference between writing bascripts in isolation like a thing that you're gonna run I think that you're gonna load into your cell we will see some of this in the common line environment lecture where we'll kind of be tooling with the buzz RC and the CCH RC but in general the if you make changes to for example where you are like if you CD into a bus script and you just execute the basket it won't CD into the cell that you are right now but if you have loaded the code directly into yourself for example we loaded these we source the function and then you execute the function then you will get those side effects and the same goes for defining variables into the cell and now I'm gonna kind of talk about some tools there are things that are like need to when working with the cell if the first we was also briefly introduced yesterday how do you know like what flags or like what exact comments can do like how I'm supposed to know the like LS minus L list the files in a list format of like if I do move minus I it's gonna like prompt me for stuff that what you have is the man command and the man command will kind of have like a lot of information of how will you go about for example here will explain for the minus I flag there are all these options you can do and that's actually pretty useful and it will work not only for kind of really simple comments that kind of compacts with your OS but we'll also work with some tools that you install from the Internet for example if the person that did the installation made it so that the man pages were also installed so for example a tool they were gonna cover in a bit which is called Rick Rick rep and is called with Argy like this didn't come with my system but it has installed its own man page and I have it here and I can access it for some comments the man page is useful but sometimes can be tricky to decipher because it's more kind of like a documentation and a description of all the things the tool can do but not sometimes it will have example but sometimes not and sometimes the tool can do a lot of things so a couple of tools that I use and commonly are convert or ffmpg which deal with images and video respectively and the man page are like enormous so it's like one nifty tool called the LDR that you can install and it will have like some nice kind of explanatory examples of how you want to use this common and you can always Google for this but I find myself saving like going into the browser looking about some examples and coming back where like the LDR are like kind of community contributed and they're like fairly useful then like the the one for fmpg has a lot of useful examples that are like more nicely formatted if you don't have like a huge font size for recording a point in simple comments like tar that have a lot of options that you are combining so for example here you can be combining two three different flags and cannot be obvious when you want to combine different ones that's how you kind of will go about kind of finding more about these tools a on the topic of finding let's try you learning how to find files like you can always will go less and like we can go like a less project one and keep a lesson all the way through but maybe we're we already know that we want to do for all the folders called SRC then this is probably a very common for doing that and that's fine like fine is the tool that pretty much comes with every unit system and fine we're gonna give it the here we're saying we want to call find in the current folder remember that dot stands for the current folder and we want the name to be source and we want the type to be a directory and by typing that is gonna recursively go through the current directory and look for all these files of folders in this case that match this part and fine has like a lot of useful flux so for example you can even test for the path to be in a way here we're saying oh we want some number of folders we don't really care that many folders and then we care about all the Python script like all the things with the extension dot pi there are within a test folder and we also check in just in case it's pretty silly but we're checking just that is also a type F which stands for file and we're getting all these files you can also use different flags for things that are not like the path or the name you could check things that have been modified like M times for like the modification time things that have been modified in the last day which is gonna be pretty much everything is gonna print a lot of the files we created and files that were already there and you can even use odd things like size the owner permissions you name it what is even more powerful is fine can kind of find stuff but it also can do stuff when would you find those files so we could look for all the files now have like a TMP extension which is like a temporary extension and then we can tell find that like for every one of those files just execute the RM command for them and that will just be calling RM with all these files so let's first execute it without and then we execute it with it again as with like the command line philosophy it looks like nothing happened but since we have non zero sorry a zero error code something happened it's just like everything went correct and everything is fine and now if we look for these files they're in there anymore another nice thing about about the selling journal is that like they're like these tools but people will keep finding new ways or like alternative ways of writing these tools and it's kind of nice to know about it so for example find if you just want to match like the last like the things that end in TMP can be sometimes weird to do this thing like this has like a long command and there is like things that like FD for example there's like a shorter command that by default will use regex and you will ignore your git files so you don't like search even for them and it will color code it will have like better unicode support it's like nice to know about some of these tools but again the main idea is that if you are aware that these tools exist you can save yourself a lot of time from doing kind of meaningful and repetitive tasks and another common to be in mind is like fine like for some of you may be wondering like fine is probably just actually going through directory structure and looking for the things but what if I'm doing a lot of finds a day wouldn't be better doing kind of like a database approach and like build an index first and then use that index and update it in some way well actually most most unique systems already do and this is through the locate command and the way that the locate it will use it will just look for paths in your file system that have the substring that you work and I actually don't know if it will work oh it working let me try to do you something like missing semester you're gonna take a while but suddenly like you found all these files there are somewhere in my file system and since I have like built an index already on them is much faster and then to like keep being updated using like the update DB command that is running through cron to update this database finding files again is like really useful sometimes you're actually concerned about not the files themselves but the content of the files for that you can use the grep command that we have seen so far so you could do something like grep foobo in MCD is there what if you want to again like find recursly search through the current structure and look for more files probably we don't want to do this manually we could use fine on the exec but actually grep has like the minus capital R flag that will go through the entire a directory here and it's telling us that oh we have the foobo line in exemplar sites at these three places and in these are two places in four a this can be really convenient when mainly kind of the use case for this is you know you have written some code in some like prim languages and you know it's somewhere in your file system but you actually don't know but you can actually quickly search so for example I can quickly search for all the Python files that I have in my scratch folder where I use the like request library and if I run this like it's giving me like through all these files exactly what line it has been found and here instead of using grep which is fine like you could also do this I'm using rip which is a kind of like the same idea but again trying to bring some more nice these like color coding or file processing and other things like has also very unique code support has it's also pretty fast you are not bringing like a trade off on like this being a slower and there's a lot of useful flags like you can say oh I actually want to get some context around those results if it committed to just so I want to get like five lines of context around that so you can say it like see where that import lives and she kind of go around it here in the import is not really useful but like if you're looking for where you use the function for example it will be pretty handy we can also do things like we can search for example here some like a more advanced use we can say it's you is for like don't ignore like hidden files sometimes that can be then like you sometimes you want to be ignoring hidden files say first you want to search config files of that like by default hidden then instead of printing the matches we're asking to do something that will be kind of hard I think to do with grep out of like my head which is I want you to print all the files that don't match the partner giving you which might be a weird thing to ask here but then we keep going and this parent here is like a small regex which is saying oh at the beginning of the line I have a house and a bang and there's a seaman like that we're searching here for all the files that don't have a seaman and then we're giving it like here like a t-minus sh to only look for like sh files because maybe you like all your Python or text files are fine without like a seaman and he is telling us all like MCD is obviously missing a seaman and we can even they have like some also nice flags for example if we include the stats flag it will get all these results but it will also tell us like information about like all the things that he searched for example like the number of mats the matches that he found the match lines the file died search the vital printed etc a similar as with FD sometimes is not as useful using one specific tool or another and in fact as regroup there are like several other tools like act easy or you know kind of grep alternative that was written it was like the silver searcher AG it was another one and they're all pretty much interchangeable so like maybe you're a system that has one or not the other just knowing that you can use these things with these tools can be pretty useful lastly I want to cover kind of how you go about not finding files or code but how do you go about finding comments that you already saw something like figured out and the first obvious way is just using the app arrow and like slowly going through all your history and like looking for these matches this is actually not very efficient as you probably guess so the bass has ways to do this more easily there's the history comment that will print your history here I'm in seasage and only prints a sum of my history but if I say I want you to print everything from the beginning of time it will print everything from the beginning of whatever this history is and since this is a lot of results maybe we care about the ones where we use the convert command to go from some type of file to some other type of file so image sorry and then we're getting all this results here about like all the ones that like match the substrate and what it was what even more the pretty much all cells will link control R is like a key binding to do like backwards search and he will have like backwards search where we can like type convert and it's finding the command that we just type and if we just keep hitting like control R it will kind of go through these matches and one like it will let us like reexecute it in place and another thing that you can do really to that is you can use this really nifty tool called FCF which is like a fuzzy finder like it will let you this it will let you do kind of like an interactive graph and we could do for example this where we can cut or example is SH command that will print here to the standard output and then we can pipe it through FCF and you just get in all the lines and then we can interactively kind of look for the string that we care about and the nice thing about FCF is that if you enable the default bindings it will bind to your controller cell execution and now you can quickly and dynamically like look for all the times you try to convert the polygon in your history and it's also like facile matching whereas like by default grep or these things you cannot have to write a regex or like some expression that will match the thing here here I'm just typing convert on Falcon and it's just trying to do the best it can doing the match in the lines it has. Lastly a tool that probably you have already seen that I've been using for not retyping these extremely long commands is this history substring search where the as I type in my cell and both if I mentioned but both this which I think was who really introduced this concept and then csh has a really nice implementation we let you do is as you type the command it will dynamically like search back in your history to the same command that has like a common prefix and then if you like it will change as the match this stops working and then as you do like the right arrow you can like select that command and then like re-execute it. I've seen a bunch of stuff I think I have a few minutes left so I'm gonna cover kind of a couple of tools to do kind of really quick directory listing and directory navigation so you can always use the minus r kind of like recursively list of some directory structure but that can be like suboptimal like I cannot really make sense of this easily. There's like a tool called tree that will be like the match more like friendly formal like print all the stuff they will also color code based on here for example who is blue because it's a directory and like this is red because has execute permissions but we can go even further than that there's like really nice tools like a reason one called brute that will do the same thing but here for example instead of doing this thing of listing every single file for example in bar we have this a through j files it will say oh they're like more unlisted here and I can actually start typing and it will again fuzzily match to the files are there and I can quickly select them and navigate through them so I get this good to know that you're not like that these things exist so you don't lose I'm like lots of months of time like going for these files and they are also I think I think I install also like something more similar to what you will expect your OS to have like now to lose or like one of like the Mac finder that have like an interactive input where you can just like use your navigation arrows and quickly explore it might be overkill but you'll be surprised how quickly you can make sense of some directory structure but just like navigating through it and pretty much all of these tools will let you edit like copy files if you just like look for the options for them last addendum is kind of going places like we have CD and CD is nice like you will get you to a lot of places but it's pretty handy if you can like quickly go places you are either you have been recently or that you go frequently and you can do this in many ways there's probably you can start thinking oh I can make bookmarks I can make like I can make aliases in the cell that we will cover at some point a sim links but but this point like like programmers have like built all these tools so the promise have already figured out a really nice way of doing this and one way of doing this is using what is called auto-jump which personally is not loaded here this is concerning okay yeah no I will correct in the common line environment I don't know why it's not like so I think it's because I disabled the the controller and that also affected other parts of the script thing at this point if anyone has any questions that are like related to this I'll be more than happy to answer them if anything was left and clear otherwise there's like a bunch of exercises that we wrote kind of touching on these topics and we encourage you to kind of try them and come to office hours where we can help you kind of figure out how to do them or like some like busquakes they are not clear