 Okay, welcome back for another Linux shell tutorial today. We're going to be looking at Calculating how many Friday the 13th are in a given year so Let's go ahead and get started there is a command that's probably on your system called Cal and if you run that command and just hit enter It's gonna tell you the current month year and highlight what day it is And I guess I know you're probably watching this months later so Here we go. We have August 2017, but if we give it a year for example 2017 it will print the full year for you There's 2015 2014 want to see the calendar for 2100 yet 2100 there it is So that's a quick and easy way, but we're gonna use this to calculate Friday the 13th Now, you notice that the call the way it's organized with each month is that we can count columns So let me count with me one two three four five six is a column for Fridays seven eight nine ten eleven twelve thirteen is another column for Mondays or Fridays 14 15 16 17 18 19 20 is the fall Final column for Fridays. So what we're gonna do is we're gonna take that calendar command and we'll put in 2017 and we will pipe that into Ock Which you can do a lot of stuff with Ock and inside? Single quotations and some curly braces. We're gonna say print. So we're telling it what we want to print it We're gonna give it columns if we say dollar sign six it will print column six dollar sign 13 will print column 13 and 20 will print 20 if we enter now we have now printed out Every column that is a Friday Let's go ahead and add a little bit to that though We're gonna go back in here and in between each one of those we're gonna add quotation space quotation quotation space quotation So now we have a space in between each one of those columns again creating columns rather than having everything in one column so what we can do now as We can pipe that command into the TR command And we're gonna say TR quotation space quotation space quotation backslash and Quotation so the same TR if you've watched some of my tutorials in the past is a command that will replace one character with another So we're saying find in the output of this every single space and make it a new line So what that does is it puts every single one of those lines on its own line Now we can use grep So we have only Fridays and every single Friday is on its own line So now we can say grep 13 and it displays how many Thirteens appear in that calendar year. Let's go ahead and give it another year. Let's try 2016 There was only one Friday the 13th go 2015. There were three if we go 2000 let's say 14 there was only one Let's say we're trying to get the number of them So with grep we can say dash C which instead of giving us the output of lines. It just gives us the count So for 2014 it will say there was one For 2015 it says three 2016 one and 2017 will be two So now we can put this in a loop if we wanted to see over the years how many Friday the 13th there are so let's go ahead and since this we're going to get into a couple of lines here Let's go ahead and copy the line We just created and I'm going to open up the Vim. I'll just call this F13.sh And of course, I'm going to start with my shebang line, which just tells our system what Interpreter to use and we're going to use the bash interpreter for this and That's important because we're going to loop through some numbers a sequence of numbers and That varies from shell to shell I'll try to give you an example to see if I can get to mess up using a different shell here in a little bit But let's go ahead and say four year in and we're going to say Brace expansions will say two thousand dot dot to 2050 so that will loop through those numbers one at a time do Done and if I just come in here and say echo I can say echo Dollar sign year so year is the variable we created Let's go ahead and save this make that executable with change mod Plus x we only have to do this once on this system and then dot slash f 13 and we run that you can see it's looped through Every number from 2000 to 2050 Let's go ahead and go back into our script here And again, I'm using Vim you can use whatever text that are you prefer and And I'm going to now add in our line of code from earlier, but instead of saying 2017 I'm going to paste our variable of year in there so now it should loop through each year and we're gonna say this year has and We're going to put this the output of this command into a variable So the way we do that is you get it in the name, so we'll just say num for the number output and then we're going to say Inside quotations the quotations aren't necessary, but I like to use them But dollar sign and then inside parentheses our command what that does is it runs these commands and then it's going to put the output of those commands Into that variable there and then we can use that variable so we can say year whatever has dollar sign num Friday the 13th And if we did everything properly we can run that command again With dot slash the dot slasher says we're writing a script that's in the current directory and there you go You can see which year each year and how many Friday the 13th it has but let me go ahead and giving an example Let's see if this works If I get rid of my shebang line And I exit out of here And I say that oh We got an error. Why did that happen? But if I'm like this It works Well, what just happened there? And this is why I stress not just with shell scripts But with all scripts whether using Python or Pearl make sure you have your shebang line and don't just trust the system To figure out what you're doing. So the command works here, but if I type exit and run it again It doesn't work and the reason is the default shell on my system is not bash It's z shell and z shell doesn't use brace expansions like bash does so it's looking at this and going well I don't know what to do with that. It's actually taking that and putting it into a variable kind of That's why it says This output it thinks it's a string has zero Friday the 13th and the Cal command doesn't know what to do with that Because that's not a valid year So If we go back into vim This again, make sure you have your shebang line But actually let's try something else. Let me remove that and instead of using the brace expansions what we can do is We can use a command so I can say Backs back tick which is next to the one on your top row of your keyboard if you're using a standard Quarty keyboard and I can say two thousand Two thousand fifty so what this is this is a command. It's a separate command That will iterate through count through these numbers and that's a little more universal and this command the sequence command is on Almost every system. It's built into busy box, which I mentioned a lot in the past if you're on a router You can get this up and running it probably has it if not you can copy a busy box binary over and it's there So now I should be able to run that and it works So brace expansions are nice because it doesn't start up a new process to calculate those numbers But using the sequence command makes it more universal to pretty much every system I've ever worked on so I used to use brace expansions before I realized that which is a couple of years ago I started using the Z shell and then I switched to using the sequence man But you should still tell the system what interpretive you use in this case bash if you if you're writing a bash script Use bash if you're writing in some other shell you use that shell And if you use just that s or not dot sh but bin sh usually that defaults to the systems default Which again on my system is not bash so Stuff to think about there, but I hope you found this useful, you know I should I calculate the Fridays like that that was um an Example and I gave you a little more than what this tutorial was on So I hope you found that useful little tidbit on on the brace expansions there and stuff Please visit films by Chris calm that's Chris the K There's a link in description as well as a link to my patreon page I appreciate any support you give me be sure to like share subscribe comment all that stuff helps me out a bunch And you can also go to my website films like Chris calm to support me using PayPal if you can't use patreon for some reason I do appreciate any support that is given to me and as always I hope that you have a great day