I just built this star tracker for about $30. It's a very simple mechanism. Instead of using a motorized gear to track the stars like in a commercial star tracker, this tracker makes me the motor, and I manually track the sky by turning this threaded bolt and this counteracts the Earth's rotation. Stick around and I'll explain how it works, show you how to build one, and then we will do a real-world test of it by shooting several two-minute exposures of the Milky Way and the Ro Wufiuki Cloud Complex, which is beautiful, with a basic DSLR and a 24mm lens. Hello there, my name is Nico Carver and my YouTube channel, Nebula Photos, is all about helping people learn astrophotography. I'm especially interested in how to approach astrophotography on a budget. Of course, many aspects of astrophotography become easier if you just throw money at them, and it's easy to go down a gear buying rabbit hole I've been guilty of it myself, but I also enjoy getting the most out of budget gear and budget techniques, which is what I'm going to be showing you tonight with this. I do have a Patreon to support this channel, and if you join the Patreon, you can also join my Discord server, which is a great place to ask questions and talk to others in the Nebula Photos community, including me. I'm going to say up front here, I'm not super skilled in DIY stuff, but when I read about this Star Tracker that's basically just two boards connected by a hinge with a ballhead on top and then a threaded bolt that you turn to track, I thought, well, this sounds simple enough that even I should be able to make one. I did and it works. The inventor of this Star Tracker is George Haig, who released the idea for free and described how to make it in this April 1975 issue of Sky and Telescope Magazine. It's sometimes called a Scotch mount because George Haig is Scottish, and probably the most common name for it, though, is a barn door tracker. It's been a popular DIY project for amateur astronomers ever since he published this in 1975, and there have been many different versions of the design improvements to it, maybe, but I'm going to make the original Haig design both to keep the cost low and because I'm fascinated by the simplicity of it. Before we jump into making one, let me just very quickly explain how this kind of tracker works. And it's pretty easy in terms of the math, so this isn't going to take too long. The reason the stars move from our vantage point is, of course, because the Earth is rotating around its axis, and the Earth moves 360 degrees or one full rotation every 24 hours. For the astrophotographer, what this means is that if you just point your camera at the night sky and leave the shutter open for a long time, the stars will trail, meaning turn from little points to arched lines that we call star trails. This of course also blurs out any deep sky object like a nebula or galaxy. But one way to get around this, which I've described in several other videos now, is just to take very short exposures and then stacking many hundreds or thousands of these short exposures together with stacking software. The name for this that's sort of catching on is untracked astrophotography. And untracked astro works well, but it just has some definite limitations. The main one being that you have to take hundreds and hundreds of photos, and so that can wear out your mechanical shutter in the DSLR. And then it also just takes a long time to, you know, transfer all the files and stack them all together on your computer. And another limitation is that it works really well for bright deep sky objects like Orion and Andromeda, but not so well for dim ones. So to solve this, we usually turn to star trackers, which just move at a constant rate that's opposite the Earth's rotation in the opposite direction. And that lets us take much longer exposures with the camera, with pinpoint stars. So as I said earlier, Earth is rotating 360 degrees every 24 hours. We can divide that down to 15 degrees per hour or 0.25 degrees per minute. So if we look at these two boards, what we really need to do is move one away from the other at a rate of 0.25 degrees per minute. And that will keep up with the stars. Now, a very common piece of hardware that you can buy is a quarter inch 20 bolt, meaning it's quarter inch in diameter, and it has a thread spacing of 20 threads per inch. And so that's what we're going to use to drive our mount. Because we can drive that at one revolution per minute. It'll travel one inch every 20 minutes or 0.05 inches per one minute. So we know now, okay, we have to move 0.05 inches per minute, and we want the boards to move apart from each other at 0.25 degrees. So all we need now is to solve this equation. And if you remember some trigonometry, you might already know what's coming here. What we do is we use a simple formula. And what we need is the hypodenduse. And the hypodenduse is going to equal the opposite side of the angle. So we know that's 0.05. We know the angle is 0.25. So what we can do is divide by sine times the angle. So 0.05 divided by sine times 0.25, and we get 11.4 inches or 290 millimeters for those metric folks. And so we want the hole for the bolt to be 290 millimeters away from the hinge. And then if we track at one rotation per minute, we're going to get perfect stars. And yes, for this to work, we also need to be polar aligned. And so that's the hard part keeping this build on the cheap. And so what I did is I'm just using a metal drinking straw on here, taped right by the hinge to site polaris. But really, this is the first thing that I want to improve in my next revision of this mount as the straw to site polaris is really a bit of a pain to use. It's not that accurate. And it won't work for the southern hemisphere. So I'm on the lookout for a used finder scope. I mean, a used polar scope would be even better. But something, a laser, I don't know. But let me know in the comments if you have any kinds of ideas for an accurate way to polar align this kind of mount, a DIY mount as cheaply as possible. OK, anyways, now that we know how it works, let's go through the steps of building it. And I made a PDF guide you can download from the link in the description for building it. I'm going to go through the steps fairly quickly. So the guide will be a helpful reference if you want to look at that while you're building one yourself. Step one, measure and cut two pieces of 1 by 6 lumber to 12 and 11 and 16 inches long. That's 322 millimeters for the metric folks. If you don't have a saw, Home Depot or other kind of home improvement hardware stores might cut the lumber for you in the store. Step two, put your hinges, or hinges if you're using two, along the edges of the boards to mark out where the holes are going to go and then drill some small holes to attach the hinges.  I'll just note here, I was able to find a six inch long hinge and I'll put a link in the description of one online that I found. If you can't find that though, two smaller hinges, like two three inch hinges, would work just as well. It should now look like this with two boards connected by the hinge. Step three, now you want to measure out a spot on the bottom board exactly 11.42 inches or 290 millimeters from the hinge and make it centered in terms of top to bottom on the board. And mark this and then drill a quarter inch hole there. You're then going to glue and hammer in a quarter inch t-nut into the top of the bottom board. And when you glue it or epoxy it, be careful not to get any of the glue into the threads of the t-nut. It's just to secure the t-nut to the board a little bit better than you could by just hammering it. Step four, repeat step three, except this time put the t-nut in the exact middle of your bottom board so that that would be six and five sixteenths of an inch or 161 millimeters. This is where you will attach the tracker to your tripod. Step five, I've lost the sunlight so I'm indoors now. This is how your tracker should look and you can go ahead and screw in the long quarter inch 20 carriage bolt with the round head into the t-nut that is 290 millimeters away from the hinge. This is going to be your drive bolt for the tracker and you can test it out if you want. Step six, print out the pattern I'm providing to make your clock wheel. You can just print it out on regular printer paper and glue it onto a CD. But if you happen to have every CD labels like I did laying around, you can use those, makes it even easier. Step seven, we're now going to secure the clock wheel and handle, which is, I'm just using a sort of little metal brace here I found, to the bottom of the drive bolt. I'd recommend using some nuts, washers, and epoxy and then tighten it all up with some pliers so that it's all secured very well to the bottom of the bolt and very permanent. Once you have all of that done, you should be able to easily turn the bolt up and down with the handle like this and the clock wheel will rotate around.  I put my ball head in the center lengthwise but towards the top widthwise and putting it towards the top of what will be the north end of the board because I'm going to be mostly using my tracker for Milky Way, which will always be to the south here. So I was thinking for balance with lenses that the weight would be more distributed equally this way if the lens is coming out towards the south. I'm not sure if this really makes any difference but that was the thought behind it.  And step 10, attach your polar alignment device and align it with the hinge of the tracker. As I said earlier, the drinking straw works okay but for very wide angle lenses like I'm using I'm going to be doing 24 millimeters but to use this at 50 or 70 millimeters I think I'd need to find a better polar alignment device. And that's it. Let's go test it. Okay it's 3 20 a.m. I'm at a Bortle 4 site. This will be my first Milky Way of the season. I'm really excited. I'm ready to actually try this thing out and the first step is we need to polar align it. And I'm just going to try to site Polaris with this metal drinking straw. Just try to center it in the straw.  And this is just going to be a good enough polar alignment. Not a great polar alignment but since Polaris is actually about a half a degree off from the pole but hopefully for a wide angle lens like this 24 millimeter this will work well enough. So here we go. All right the second step now that polar aligned is we have to find our target by moving the camera around on the ball head until we're pointing at it. So I want to be pointing at the Roe Oofyuk area which is pretty easy to find because there's a bright visible reddish star called Ontaris which is right in the middle and it's part of again the easy-to-spot Scorpius constellation. So here we go. I'm just going to go find it. The third step now is I want to focus on the stars using the live view. I'll just zoom in to 10 times and try to make the stars as small as possible on the screen. The fourth step is I'm going to put the camera into bulb mode and just connect this simple bulb timer or shutter release so I can take two minute long exposures. This has a little lock so I'll basically just start an exposure lock it and then at the end of two minutes unlock it. So now we're all ready to start tracking. Let's see how it goes. Okay my first few tests were not successful. I was still getting sort of trailed stars. I found through a lot of trial and error though that with this camera and lens I'm using here which again are the Rebel T7 or 1500D and the Rokinon 24 millimeter lens at 2.8 that I need to move the clock wheel or the the bolt at a rate of every 2.5 seconds. I thought maybe I could get away with every five seconds but I'm getting trails still. I'm getting much rounder stars by moving it 15 degrees every two and a half seconds rather than 30 degrees every five seconds. So it's not too bad it's just a little bit tough on my neck to keep looking down at it but I can adjust the tripod height to sort of fix that. I'm just using the stopwatch on my phone to watch the seconds go by and I've just done some short tests so far. But let's go ahead and take a full two minute exposure now that I think I have it working and we'll see what we get. Wow will you look at that? I'm amazed at how good this looks. This is just a kind of single two minute exposure with this barn door tracker. Let me zoom in on it so you can see what I'm seeing here. Just really good detail. Stars are nice and sharp and round. I think this is gonna be a keeper. I'm gonna try to take as many as I can before sunrise here so I'm gonna get right back into it. So I've taken ten two minute exposures. They're all looking good but I'm getting close to the bottom of the bolt so I'm gonna go ahead and rewind it by turning it clockwise now this way. Okay I'm done rewinding it. I'm not gonna have to re-point the camera on my object because by rewinding it I probably threw off the pointing a little bit and then I'll take ten more two minute exposures and probably by then the sun will be rising and I'll have to stop. So we'll have 20 two minute exposures for 40 minutes total. Well it's the next day I've gone ahead and processed what I shot last night and I got to say I'm pretty impressed by this barn door tracker. I wasn't so sure if a manual tracker was gonna be able to keep up with a modern DSLR with small pixels but as long as you rotate the bolt frequently enough to avoid trailing I found it works really well. That took a little trial and error. Of course the big advantage of using this tracker over just shooting on a fixed tripod is to get 40 minutes total time on my target like I did last night. I only had to shoot 20 photos at two minutes each. If I was shooting without a tracker to get to 40 minutes I would have had to do 600 photos at four seconds each. So taking hundreds of photos will wear out your cameras shutter faster and take a lot longer to stack on your computer. Of course there's some cons to this tracker too. The manual nature of it made it a bit uncomfortable on my neck to be looking down at it for 40 minutes just slowly moving that wheel and I suppose some people would also find it pretty boring to just manually track that long. So keep those things in mind. What's next for this project? Well I want to buy a used finder device like I mentioned with maybe a crosshair ip so I can better polar align and that will allow me to try longer focal lengths because I've only really tried it seriously at 24 millimeters. I did try a little bit longer focal lengths but I wasn't getting good results. I think it was because of polar alignment error. So once I've done like a version two of this I also want to test it against some of the cheaper commercial options like this Omegan LX2 wind-up tracker and the Move Shoot Move which is a little bit motorized tracker but it's very small. And so you can look forward to a part two eventually where I will show you my improvements and try it against these options. You know $30 tracker versus a little bit more expensive ones. Well the only thing left now is the image reveal. So let's start with the single unprocessed two minute exposure. Here it is cropped in a bit to fill the screen and we can zoom in on some stars here just to show you that they are indeed round. Okay and here is after and all I did in the processing was really you know stack with I think I did 12 darks. I didn't have time to do flats or bias so it was just 20 20 lights and 12 darks matched temperature darks. And I just stacked those in DeepSky Stacker brought this into Photoshop and did my usual sort of playing around with curves and saturation and stuff. I also just want to mention here again at the end that I have a Patreon and another perk of my Patreon is I now have an exclusive video just for people who support me on Patreon and that the topic of that video is choosing different types of DeepSky objects and what kind of gear filters and sky conditions you might need for different types like if you want to shoot a star cluster do you need a filter do you need a dark sky that kind of thing. If you want to shoot a constellation what's the best focal length for that. So I go through some different types of DeepSky objects. So if you're interested check out my Patreon it starts at just one dollar a month. Until next time this has been Niko Carver from NebulaFotos.com Clear Skies!


So your mom, it's a white tiger.  I'm Rick and you false. It's a good I'm Twitter off more juicy content. I think some people falling for the Kevin Wilson and kids quite There's a tiger in there. Get your nipples out of the shot There's there's no nipple but through the cat scratch like three dimension monetize There's a tiger behind the scratch is scratching his way out. There you go So if you have a guess by now, we're doing a movie review done done done done done Of the newly released, I guess Netflix film the white tiger directed by Ramen Barani, but he was one of the writers and one of the writers obviously starring and produced by Parenko Chopra Raskamarao, but actually the star is Adarsh Gaurav. That's him, right? The actual the white tiger. Yeah, the white tiger. Yep Use my name is Khan. I believe he was the kid. Oh, was he in my name is Khan I believe he was the kid in my name is Khan and he's also in the Shredavi movie. We haven't seen yet mom. Oh He is with no idea. Oh, it's okay. But yeah, any he was the kid in my life is my name is so yeah Just just released. It's not actually an Indian film Technically technically. Yeah, it was made by an American in Hollywood just produced and has quite a few actual Indians in it Which is great. It was it's a blending of of almost the two worlds. It felt like an Indian film to me Yeah, yeah, so we'll give you a little non-spoiler review and then we'll get into just it's very brief That's not how we like to do things, but since it's new a lot of people haven't seen it We'll give you a little non-spoiler and then we'll get into the spoiler stuff So this is the non-spoiler version Rick your initial non-spoiler the initial non-spoiler Review I really liked this. I think this is the anti slum dog Love him. Yes have a lot to say about I think him carrying the weight of this thing I think he is a star in the making absolutely in terms of Interpreter whether he wants to do Hollywood or or continue in Indian cinema or both. Yep, it's great But yeah, he drove this film one hundred percent road was so good so good I love watching him The more he was on screen the more I like watching him and yet quite an arc as a character Sure did and what a story for that character. I mean that this is this is if you like stories that are Centered on a single character and you watch this character's trajectory In the same way and a lot of other films This isn't a spoiler but for example Godfather is really Michael Corleone centric There will be blood as Daniel Plainview centric Yeah, this film it's called the white tiger and it's not spoiling anything that there's a connection between that name and this lead character Everything is from the storytelling point of this main person that you watch on this trajectory. It's not really a pranaka film It's not really rock tomorrow. No, they're supporting. They're very small supporting not not incredibly small But they're small supporting roles. He is the star Yeah, and he is what kind of drives this film along. So yeah, I I would recommend it I think it's a good watch. Absolutely. You would obviously as well, right? I think you should want Absolutely support this watch it. I've already recommended it to somebody who watched it in India and loved it I would recommend this I think it has a broad audience in terms of international cinema as well Yeah, I think it's absolutely worth a watch and I think one of my favorite things about it again We're in the non-spoiler section is that I call it the anti slum dog for this reason at the outset There are some things that are depicted that actually I didn't like and thought oh, where are we going? And I thought no way Priyanka is producing this and it's gonna go this direction Yeah, and I think that was on purpose Yeah, I think we were given some things that were projecting the stereotypes intentionally and then even made reference just long time later even did yeah, and then we really get into some of the more Granted if anybody had a problem with this I think what they might have thought was they wanted to be more no-holds barred the way on your odd would be I Think that would be the biggest problem someone might have with the power of my issue. Okay. Yeah, like I have a few issues But overall I'm much more positive than anything negative I enjoyed it if it was on like Alexis isn't gonna come and spend the night and have buddy time with me Right if she said I want to watch quite I would absolutely turn that right back on and enjoy every minute of it especially for him now Roshan Rao. Yeah, what do you expect? Yeah solid. Yeah Priyanka Solid. Yeah, always. I mean they're just always good. They weren't the stars. No, this is his film Yeah, if you're gonna watch this film you're watching it really for him And I would I would say as much as I like the story we'll get into the spoiler stuff The preeminent reason to watch this is you're you're gonna see a young actor I haven't had this kind of a reaction to a young actor since Alia and gully boy I don't know how young he is. I don't know either, but I mean career-wise. He only has 11 films Resume for sure, but there's there is a caliber to his capacity That I think is what you said at the outset I think this young man is an absolute Superstar in the making and most especially in regard to his capacity as an actor. This is an actor. He's 26 Yeah, this guy is a this guy is an actor. Yeah, he is he did he did a really really good job Yeah, that's the first thing I when we finished I turned to my wife and I said he's really good Yep, I like him a lot like him a lot. So yeah, definitely go watch it if you haven't watched it already And let us so stop now. Yeah, I'm gonna get to this boy now. We're getting into spoilers So if you want to watch it go watch it's already on Netflix It's free and it's trending really well. Is it internationally? Yeah, Priyanka just tweeted about it It's like trending either one or two in India number seven in the UK number six in the United States So I'm hoping I'm hoping this kind of opens the gate for more of the cross like Collaborations between Hollywood and India. Yeah, I would have I would have loved for like a Indian director even though I know he's descendant of sure. I believe he's a descendant of Indian, right? I would imagine so I think I could be wrong But yeah, I want I want more of that me too. I'm hoping this is kind of opening the floodgates me to for something like that And we've started to get that extraction was a little bit of it very little of Our arm in Pankaj, but they have that other actor who was more prominent in it Yeah, there's there's a trend not to get too much on a bunny trail But there's a beautiful trend that's happening one is because of what happened with parasite But secondarily people in the United States when I say people I mean people in the industry movers and shakers who pay attention to money and pay attention to film and studio Have been talking for the better part of a year about OTT and India and what a spectacular burgeoning explosive market this is and it is Boating really well. I really feel like this is the next great pioneering landscape for cinema is the international community Celebrating each other and not thinking about each other is these far distant lands that maybe I'll see one of their films Once a year, but that we watch like on Friday You're watching dark in Germany and the next day you're watching the platform from Spain, you know, so absolutely Here come the spoilers for you. If you haven't watched it already, please go away Yeah, so I had a kind of few issues with it, but the the main I Just I kind of felt at the end. I wanted more I think that was my biggest gripe is that it won it in it very abruptly for me I felt like the whole story. Okay, and then done. Okay, this entrepreneur now Blah blah blah blah blah this could have been this this could be a series. Yeah, this could be yeah I think it was just I don't know. It was just my taste. It's just I want them to go deeper and more raw And sometimes and sometimes they did but not always But yeah, it's not a it's not a huge issue. I'd give this film more of a B-minus David a B-plus. Yeah, than anything. Yeah, so it's not like I didn't like it. It's just those are my I I always look for what I I think could make a film from being good to absolutely great. Absolutely agree That's it's this isn't something that I would put into the conversation for awards categories It wouldn't he may rise me. Yeah, maybe it won't surprise me if he gets some awards for this Absolutely, that won't surprise me and not every film needs to be in the awards conversation I there's a lot of films that are on my all-time favorite list that were never nominated for a thing. Yeah, they're great movies. Yeah, and I I Was waiting for it to happen and it did Right at the midpoint literally like it one hour. I got a call from Andrani She I called her I will woke her up and she said how's the movie and I said it's very good. I Think it's about to turn the corner and become really good Feel it right now and it did. I don't remember the exact moment. It happened, but it's it's around the time It's just after the hitting of a car I think it really started to turn the page when he started to realize The way he was treated was not okay Yeah, and it was just before that moment when he walked into the room and they're being really nice to him And they want him to sign the paper which I thought he he had some beautiful moments He doesn't have a false moment. No, he was never never close when when he has to read the thing and he's reading it And he says I'm and it says his name take full responsibility for the yeah And he's realizing what they're doing to him. Yeah, and at that point I think for him Was the fulcrum point that started to take him in that I love that other moment when he's looking at rush come Around he says sorry I have to confess something. There's a hole. What is it? Want to go see your family?  Yeah, it was being Dead serious. Yeah. Yeah, absolutely.  Yeah Yeah, I thought he had a lot of really great moments Hey, and did you know I actually saw an interview with Priyanka on Stephen Colbert just the other night about this from promoting this film and The director apparently doesn't say action like Clint Eastwood. He also doesn't say cut. That's Clint Eastwood So yeah, and which is a trend. I've actually been on sets where they'll say when you're ready when you're ready Which is what apparently he said, but I've never been on one that doesn't say cut though That's that's a little weird.  I guess that's the signal for everybody But apparently he Really loved that. He thought it was a very Freeing invigorating. I'm sure it is kind of thing and it shows Priyanka Apparently she said it, you know, I've done like 50-something films And so it was a little hard to get out of the I'm used to action and cut Yeah, yeah, and so there's a whole thing But apparently he really loved that at free-ness of it so he could improvise and keep going Another one of my issues some of the dialogue especially between the rich people Was a little I don't know. It seemed a little cheesy at times Hmm, but just between that family like the rich family, right? And I don't know if it's because they were speaking English And that's what I am glad that that's what they did because I was opposed to slumdog where everybody was basically speaking English Right and some of the British accent the rich people were the ones speaking English Which makes sense is how that happens the people in more of the rural Villages were speaking their native languages, which was great And I thought it was another great moment when they were traveling there and kind of something I wouldn't have actually caught and rise Raj Kumar kind of brought it up And then there was this the whole teaching them of that Raj Kumar and Priyanka were doing I think they were trying to do almost an American ish very much I mean her character, you know style born in India, but lived there since she was 12 Yeah, I felt like Priyanka might very well be just as much herself in this role. Yeah She's anything she's ever done. I think this is pretty much Pretty much her as her as you get. Yeah, absolutely with the way, you know, the people were treating him and that is a great compliment that that's If you've been around the channel a long enough time, you know that I think that Priyanka is Probably one of the more underrated actresses around and I think that she has the potential to be the first Indian actress to win an Oscar For supporting or or lead There's a whole handful Another moment I loved and this is where it when I started to go off the rails a little bit I love it when he's in the place where he sees the guy taking a dump And these just they're laughing at each other in a weird twisted laugh, yeah But I think my favorite thing is obviously the message I felt like Again, it didn't go on your acacia. Mm-hmm raw. Yeah, but it didn't Blush over some of the things where I don't know how many times he's like we're not a democracy the reality of Well the carrot this character arc I love how he goes from being in this rural village that he We're loves and respects his dad and then he sees like the dawn come in who slaps his dad and treats his dad like crap He's like wow, what's that guy have almost Mirza per kind of level, right? And then man if I could just be a servant in his house, okay now I'm a servant if I could just be his driver Now I'm his driver Wow if I could just be treated like family Wow if I could just stop being treated like crap Yeah, if I could and The fact that he justifies that maniacal laugh Combined of I can't believe I just killed somebody to I just killed him You know yeah that blend of I can't believe what I just did in the negative and I can't believe what I just did in the positive Yeah, and how he justifies Who he becomes Based on the fact that he got to achieve what he did Despite the man. Yeah, but he has become the man and I actually love that ending frame Well, I like the ending because the ending frame he walks away, and there are all the other white tigers right behind them It's survival of a frickin fittest. Yeah. Yeah in the whole message. Yeah, that's why I lean more toward the B plus Yeah, he said multiple times. He's like it's America's a white people's their time is done essentially, right? It's not time for the brown and brown.  Exactly Especially the white man. Yeah, why asters straight. Yeah heterosexual heterosexual men gross. We're awful But anyways, yeah, I came away with this with a lot more positives their stuff I would have probably changed if I was directing mm-hmm, but I there was definitely it was Way better than extraction, which I think was the one of the first agreed first things that came out in terms of Blending outside of slumdog. Yeah of the two But I think that also comes with prank of being at the helm and yeah, it felt like a nice Blend between a Western film and an Indian film Yeah, and I felt that that was the broader audience because on your I wouldn't get a broader audience If he would actually get some people to not see it So I think I don't I don't think they pulled away from things with a sense of fear I really think that Priyanka and this director would just I think I'm like I would have a broad audience It would just be a different broad audience in this one. Yeah agreed It would be a different he would he would go for the juggler in terms of every single thing He's talking about whereas this I think this could Irritate people sensibilities Mm-hmm just enough to keep them watching whereas on your I would get people some of them would turn away. Yeah, I love it Yeah, I do too Which is the difference between probably an award level kind of film versus one that is still a really good film most especially Yeah What is his name? Forgive me if I'm mispronouncing your name, but yeah, he's a really wonderful this he's a star in the making Yeah, it was almost two characters from the beginning and the end where he kind of ended up, which was great So let us know more from him Obviously, we know Rajkumar Rao and Priyanka Chopra, but they just will watch it a lot more of them I feel bad just to say they're phoning it in they weren't phoning it in they're just that good Yeah, they're just always good But let us know what you thought of about this film down below


Well, it's that time of the week again. It's time for Chitchat Across the Pond. This is episode number 759 for February 18th, 2023. And I'm your host, Allison Sheridan. This week, our guest is Bart Boo Shots with Programming by Stealth number 144. Are we going to keep playing with shell scripts today, Bart? We are. And I'm actually in the middle of rewriting the name of this show. No, it's just as we start to record because we are talking about conditionals. I initially gave the episode a more conservative name because I wasn't sure we'd get through it all in one installment. But we did. So we get to go all the way from last time we learned that the terminal does Boolean-like things in a unique and interesting way. These things called exit codes where up is down and down is up, or zero is true and anything else is false, which is pretty, it makes my head hurt still. But they are the basis for controller flow. So by controller flow, that's like a superset of conditional statements, which is do something on a certain condition or unless a certain condition. And the other obvious flow of control is looping. So we're going to do half of that today, but it's not really half because the first thing we have to figure out is how to turn logical expressions into exit codes. Because exit codes are the key to everything. So how do I turn some sort of concept like, is this variable empty, or does this variable equal that variable? I have to turn those kind of questions into exit codes. Because that's the only thing that can spit out. That's the only way we can do any sort of logic is with exit codes. So that's the first half of today's installment. And it all revolves around what the BASH documentation calls testing. And there's a little bit of history. So I'm actually going to take you on a journey. And we're going to arrive in a happy place. And then once we arrive in that happy place, you only have to do things the nice way, but it is kind of useful to get there the hard way. Because otherwise it won't make sense, frankly, unless you see how we got there, it won't make sense. And then we're ready to do the closest interval it gets to an if statement, but it's not really a statement. Anyway, it's interesting. It's fun. So we will be able to do conditionals by the end of this installment. That is what we are doing today. And that sets us up really well to do loops and arrays next time. Because I'm slightly pleased you didn't notice me utterly, utterly ignoring the existence of arrays as I talked about variables in the previous few weeks. I was rather hoping you wouldn't make me say, yeah, pause that for now because they're hard. But next time we'll get there. Okay. So way, way remember, just I guess a quick reminder that we are writing in bash because it's a really good, very well supported middle ground. Bash is a second attempt at a UNIX shell. And the first one was called the Bourne shell or just SH. And then bash has actually been superseded now by ZSH, the Z shell. But bash is a really good baseline for really wide level support. So that's the one we're using. But way, way back when SH was being invented in the 70s, the 1970s, the way you did test to get an exit code was with a command called test. So there was an actual command for making these exit codes that you would use for all of your flow of control. And the command was called test. And the command took either three or two arguments. So if you remember way back to our early JavaScript stuff, we talked about most operators are binary operators because they take, like the plus is a binary operator. It's something plus some things, that's binary. But there's also unary operators. Exactly. So there's also unary operators like plus plus, which just has a thing. And the same is true for the test command. So if you have a unary operator, it's test, space, the operator, space, the value to test. No, that's a binary. That was two things. No, test, the word test, test of the command, which takes two arguments, the unoperator and the value. One operator, one value. You're saying it in the opposite order of the show notes. I thought you said that. I am. Yeah. Okay. Okay, say it again so I don't interrupt you. So if you're having one of the unary operators, it's the command test and then the first argument is an operator, which will start with a minus sign. And the second argument is the value to test with that operator. Okay. The other way is you have two values. So you have test, the first argument is the first value. The second argument is the operator, which will start with a minus probably. And the third argument is the second value. So it's test value operator value or test operator value. There are the two modes of operation. And for backwards compatibility. Yeah. For backwards compatibility, that still works. Even in ZSH, that still works. So if you'd like to play along, if you fire yourself up a terminal on your Mac, then you can use the test command to check to see whether or not you are currently in your home directory. So test, space, and then the variable PWD in all caps contains the present working directory. It's one of those magic environment variables we learned about intending the terminal. So if you do echo dollar PWD, you'll see it shows you your present working directory. So we can test dollar PWD. So that'll be our first argument. And then we want to pass an operator as the second argument. And the operator we're going to use is the equals sign, which does not mean make equal to. It means is equal to. And that's going to make a lot of JavaScript programmers heads explode. But I'm afraid to say equals is equal to. And then we pass us a third argument. Tilda, which is the universal expand or the bash expansion for my home directory. So we're saying is my present working directory the same as my home directory? That's what that test does. And then we're using our trick from last time where we use the and to say, well, if that's true, also do this other thing, echo your home. So if you do that way during your home directory, it should print your home. And if you did that from any other directory, it won't. Yeah, that works. I was going to hear you say that when it's, let's see, when it's got two, when it's got two arguments, two values, two values, sorry, two values, the operator sometimes has a minus sign in front of it. So it's not minus equals. Luckily, no, no, most of them are minus something. Yes, a lot of them are minus a thing, but no, the equals is just an equal. Well, thank goodness. So there's a few things to draw your attention to. So if you look in the show notes, you'll notice I had to quote the value dollar pwd inside double quotation marks. And the reason for that is if the present working directory contains a space, then it would be seen as being multiple arguments because space is your delimiter for arguments. So if you didn't quote that variable, then if you were say in your, let me see, what one has a space? Mac doesn't call it my documents, does it? There are folders on the Mac where there are spaces in them. I know this because I've landed in some of them. Or maybe that's my new invention. Well, I make lots of them. Yeah, maybe that's what maybe I do too. Maybe that's how that always happens to me. I know if I was a good little Unixy person, I wouldn't do that, but. Oh, no, no, the Mac is the Mac is Unix plus plus spaces. Yeah, I happily use them. So that's why you have to quote because it's just an argument, right? It's a command with arguments. The other thing is you absolutely positively cannot cuddle your two values to your operator because then it becomes one giant big argument. The string pwd equal to tilde as the first argument. And then test goes, you've only given me one argument. I literally have no idea what to do. That is not an operator. That is just what are you talking about? So you have to space them out. So there are the two things I want to draw your attention to. Another thing I want to draw your attention to is that because these are just arguments to a command, if we tried to compare something to the string star, we would have to be absolutely sure to quote the star because otherwise Bash would expand star into every file in the current bloody well directory. And then you would have lots and lots and lots of arguments to your test command and it would get very, very cranky indeed. So these are just, it's a command with arguments. So you have to do all of your escaping and you have to think about all of these possible things. Is there a space in there? Am I using any special characters? So it's a lot of faffing about. It's also, if you see a wall of code, like if you imagine just seeing a page of JavaScript code, you'll figure out where the controller flow is quite quickly because the if statements will jump out at you. It's like two characters open a roundy bracket, like just a shape of your code would jump out at you. That is not true if you do test. Because there's no, there's nothing to make it look any different. So if you see a wall of code written this way, your conditions will not be obviously conditioned. It'll just look like a whole wall of text. And so very early on, someone had the smart idea to simulate the shell, not Bash, but this is the old shell, having some sort of a structure for conditionals. They made an alias for the command test named open square bracket. And they updated the test command to ignore a close square bracket as a final argument. So you can optionally pass a close square bracket as an extra argument and it will ignore it. And so now you can write your test as open square bracket, your condition close square bracket. And so now when you look at a wall of code, the conditions jump out at you because they're bracketed in these nice square brackets. It's a dirty, dirty hack, right? But if you look at the, actually, so I'm one step behind myself here. So we have another example I should have done. I skipped an example, which is an important example. So the present working directory example was an example of two values, right? Where testing is one thing equal to another. That's two. An example of a unary operator is the minus e, which stands for exists. Really useful. You give it any file path. And if there's anything there, whether it be a folder or a file or anything at all, it will minus e to success. So test space minus e, a path will tell you whether or not there's something there. Really useful. So our second example is test space minus e space tilde slash dot bash underscore history, ampersand ampersand echo you have a bash history file. So if you have a bash history file, it'll tell you you do. And if you don't, it will be silent. I don't know if you do or don't have a bash history. I do have a bash history because you've used bash. Now I'm wondering whether I have a zsh history file. I can't remember if it's called zsh underscore history or if it has a different name. It apparently is, because I have one. There you go then. That answers that. So again, nothing too archattering minus e for minus exists. Nice and easy one. And then you just give it the value that it should be testing. But it's very hard to read.  It's exactly. It really is a wall. You can't tell where pieces of it are. So even just that dirty hack of aliasing the command test to open square bracket and making it ignore closed square brackets, when we rewrite those two examples, they genuinely are more readable. Open square bracket, dollar pwd equals tilde, closed square bracket, ampersand ampersand echo your home. That like breaks it up. Here's my condition and here's what I do. The second example, open square bracket minus e tilde hit bash history, closed square bracket, ampersand ampersand echo you have a bash history. Again, you've broken up your what, what am I testing and what do I do into two really obvious separate pieces. So it actually does work. But there's still lots of little gotchas here, right? So square bracket is a terminal command. It's a weirdly named terminal command, but it is a terminal command. So you cannot cuddle because again, then you will be running the command open square bracket, quote, dollar pwd, close quote, which is not a command that exists. The fact that open square bracket is a command that exists is weird, but that would just be really weird. So you have to say there's a man page. You can type man space left square bracket. It tells you. You can. Yeah. So it's open square bracket space because it's command space. And then it's arguments. And then all of the caveats I gave you before still apply. You still have to worry about your stars and you still have to quote your variables. Because again, if there's spaces in there, it becomes multiple arguments. So the command named open square bracket. And you can't cuddle the closing square bracket for all the same reasons because it has to be an argument all by itself. So it can't be cuddled. But again, less icky. But thankfully we live in the world of bash. And in the world of bash, it was decided that one of the problems to be solved when making the shell better was to have an actual native construct for doing tests. And so there's a key word instead of just the terminal command we use for doing tests in bash. And they decided to double down on the convention literally. So the bash and above it also works in zsh. The bash and above keyword is open square bracket, open square bracket. Wait, I didn't catch what problem we've solved by having two of them instead of one. We are now not running a terminal command. We are now running a bash feature, a built in functionality of bash. Okay, so this two square brackets would not work at the command line? It works on the command line, but it's not a terminal command. It is in fact something that tells the shell go into a different mode. So it's syntax for the shell, not a terminal command. Tilda is syntax for the shell, right? It's not any old command sitting in your slash user slash bin folder. It's actual keyword. Like in JavaScript, if is a keyword, right? So it is a special incantation. It has special meaning. There's no man page for square brackets, square brackets. So I'll remember it that way because it's not a terminal command. Because it's not a command. It is actually a feature of the language. So it's a fundamental part of the language. It's a language construct, a keyword. And it puts the terminal into a different mode between the opening square brackets, the opening pair of square brackets, and the matching closing pair. So between those two, the rules of the universe are different. It is not a terminal command with argument. It is actually a bash test, which means that you are freed from having to quote variables. Because you're not in terminal command with arguments, you are inside square brackets. And so bash is like, don't waste your time quoting, I'll do the right thing. Because we're now in a special universe defined by those square brackets. So no more quoting of variables. No more worrying about star expanding out into a bunch of file names. File globbing is disabled. So that expansion is called file globbing. File globbing is disabled inside square brackets. But special characters like tilde still work. Because they don't multiply things. Tilde is a thing. Star could become infinity things or no things. Tilde is a thing. So tilde we get to keep. So no more quoting of variables. And we still get to keep tilde, but we don't have to stress about our stars. And so now you can rewrite the above again, even simpler. So it's square brackets, square brackets, space, still can't cuddle things. Square brackets, square brackets, space, $PWD, space equals, space tilde, space, square brackets, square brackets, and then whatever we want to do. So no quote around $PWD was the main thing to get from that. But we still have to not cuddle anything anywhere within those double square brackets. Yes, because again, you do still need to tell it a thing, an operator, a thing, or an operator, a thing. So our minus e example is still as it was. So open square bracket, open square bracket, space, minus e space, tilde bash history. But again, we've now ended up with, because we live in Bash and ZSH world, we've ended up with a much nicer construct. And the thing to say, you will find answers on Google. Use single square brackets. Do not use those. For you, I want everyone listening to this show to remember, for everything we do in this series, always, always, always, always use square brackets. I very strongly advise you adopt the same for all of your scripting, because otherwise you'll go nuts, because special characters will sometimes do weird and wonderful things and bite you in the backside because you were accidentally using single square bracket instead of double square bracket. And it's one of those things where 90% of the time you'll get away with it until you accidentally use the wrong special character and then boom, it explodes. Or worse still, you don't quote a variable and 90% of the time there's no spaces in the value. So your script works almost always, part of the second Tuesday of the month when it weirdly explodes. And that's what causes these kind of weird inconsistencies. So always, always, always use the double square brackets. We live in the future. Make the most of it. Okay, so I'm not going to list. I'm still going to quote my variables because I'm going to have trouble remembering that I don't need to inside square brackets, but would it hurt? It won't do any harm. No, it won't do any harm whatsoever. No harm whatsoever. It's just extra characters. Okay, we have the disk space. What's two bytes? Really? So there are a lot of operators. So I have a link in the show notes to the Bash documentation with the full list, but I will highlight my favorites. All right, no, not my favorites. The one you're most likely to need. So the first set of operators that you probably are going to use more than any others are actually the unary operators that do file-like things. They are for interrogating file paths on your disk.  Is the thing here throw an error? You know, can I write to this file? Can I read this file? Those are the kind of questions you will have as a scripture. So we've already seen minus e to check if something exists. Minus f is like minus e, but stricter. Not only is there something here, is it a file? So if you do a minus f on your home directory, you'll get back fail, because yes, your home directory exists, but it's not a file. Minus d checks for a directory, which is what Unix and Linux and the shell call folders to us modern people. So minus d for directory, minus or is, is it readable? Minus w is, is it writable? Minus x is, is it executable? And minus s is, is it not empty? Does it have a size greater than zero is why it got that name. So not only is there a file here to use as input for my script, does it have anything for my script to ingest? Now you may have some sort of process that expects something else to have spat something out. Well, if the file exists, isn't actually the right test if you're going to ingest something? Is there something for me to ingest is actually a more meaningful test. So minus s is often the right thing to do to check, am I good to go? Can you pile these together? Like, can you say minus rwxs to find out if it's readable, writable, executable, and non-empty all in one go? I'm not sure you'd have to test that out. If not, you can use the or operator to stick between multiple tests, worst case scenario. But given they start with minuses, my guess would be that you can chain them together, but I didn't, honestly didn't try. Okay, I'm going to do something with your pronunciation of the letter r. You said or, like or, correct? As in, you can put on, so the or operator pipe pipe, like we saw last week, great. You can stick between, so you can have square bracket, square bracket, blah, blah, blah, blah, close your square brackets, or, so pipe pipe, more square brackets, you can chain them together that way. Okay. So the next thing then is strings, right? So files are obviously a big thing for shell scripts, but strings are kind of bash and zsh and all these things are, strings are their first love, right? Numbers to them are actually just strings of characters, which is why they do very dumb things with numbers, which we'll get to in a moment. So string operators are the most important as well. There are two very useful unary operators. There's minus z for is an empty string. So if you want to test is this empty, minus z will tell you if it's empty. So if you have a variable or the output of a command, something you want to test is this empty, minus z. Okay. If you want to test if it's not empty, it's minus n. For not empty. Yeah, there's only 26 errors in the alphabet. They had to get a bit creative. And remember, I'm leaving most of them out. We've already seen the equal sign to check for equality. Exclamation point equals is not equal. So that's, that's, we'll feel at home there as JavaScripters. The next two are going to make you cranky. The less than sign does not mean a numeric comparison. It means an alphabetical, a lexical comparison. Does this sort alphabetically before the other? Oh, that's handy. Which it isn't, it isn't. Because if you, if you do any of these operators, right, four is less than 10 because, sorry, four is not less than 10. Four less than 10 will return fail because one zero will sort ahead of four. Wait, isn't 10, doesn't it start with a T and four starts with an F? One zero, no, no, as in the digit four will, is not less than the digit one zero. So you write four space, that's what I mean. That's what I mean. So this operator looks like, like the minus sign looks like a mathematical operator. In most languages, it would be a mathematical operator. It is an alphabetic operator. Right, right. I didn't know that you meant one zero. I thought you were meaning TEN because you said strengths, but you also said that it treats numbers as strings. It does, yeah, exactly.  So four less than 10 is fail. The other thing that fails is zero equals 0.0. They're not the same because the equality is a string comparison. They're not the same string. You and I know that 0.0 is zero, but the shell is like, nope, those strings are different. One long, three long, not the same. So I'm still having trouble with this. If you have the number four and the number one zero and you alphabetize them in a, say for a directory name, doesn't the one zero go after the four? Only on the Mac, which does non-standard alphabetization. It's a human-friendly alphabetization everywhere else. It goes the other way. One of the best things about the Mac OS is that it alphabetizes wrong in all the right ways. I find it very pleasing. So if you want to tell Bash to do actual mathematics, you have to use effectively named operators that are way harder to read. Minus EQ for a numeric equality. Minus NE for a numeric inequality. Minus LT for a numeric less than. Minus GT for a numeric greater than. Minus LE for a numeric less than or equal to. And no prizes for guessing minus GE for a numeric greater than or equal to. That is annoying that the ones that have to do with strings are look like math symbols. And the ones that have to do with math are the text symbols. That's great. Yeah. And in most programming languages, it's exactly the opposite way around. It's also, it makes me, it is a real common cause of bugs because this is Bash being very odd. So it's a very common form of a bug. And then there's one last useful one that doesn't fit into any of those categories. Minus V checks if a variable of the given name exists. So minus V pancakes, is there a variable named pancakes? It's potentially useful. Okay. Okay, so I now want to, before we go into how we do our conditionals, I want to take a moment in time to just get you used to the idea that things are about to get weird. You're about to go through the looking glass. Although, given the previous few minutes of conversation, we kind of already feel that, right? You're not, you don't need much convincing most probably. So we've only seen one language in detail, which is JavaScript. But if I show someone who knows JavaScript, a bunch of C or C++ or Java or Pearl or PHP, you wouldn't be able to write it from scratch because there's subtleties in the syntax, but you could look at some analysis code and go, ah, yeah, there's, you know, it loops over this and then it does that. You get a gist of the code quite well because it looks quite similar. And you have code blocks inside curly braces. You feel at home. None of that applies to Bash, to any of the shells really. They are just utterly different ways of thinking. So don't think blocks of code with keywords defining what they do. It's commands and only commands. Commands, commands, commands, commands. Some of them are special commands like the square bracket, square bracket. It's a special command, but it's still, it's a one line. It's still command, something, command, something, command, something. So there isn't an if statement. There's a collection of commands which together give you the behavior of an if statement. At least three commands are needed for an if statement. The if command, the then command, and the fee command, which is if backwards. This is a pattern. Because you don't have code blocks. So how do you signal the end of if-ness with a command that's the opposite of if? I've seen that in AppleScript does that. There are other places of copy the idea. Yeah. So if and fee are basically stirred. Yeah. So if basically the if command tells the terminal to go into a new mode and the fee command says, I'm done being in this mode, now go back to normal. Makes me so happy to know what fee means. It's just if backwards here. So that's how we have to think about these things. So let's start with the equivalent of an if statement in JavaScript. So in JavaScript, it's just if open around your bracket, condition close around your bracket, open curly bracket, some stuff close curly bracket, right? That is a unit. The if statement takes that form. Well, we're not working that way. So we have the if command starts the shell into a new behavior. So the if command takes as its first argument something that is a command, something that it can execute in some way. And if you give it any extra arguments, it will take those as arguments to whatever the first one is. So you know the way with pseudo, it's a command that treats its first argument as another command and then all the other arguments as arguments to the command. Right? So you've pseudo do something arguments. Well, it's if do something arguments, but in reality, they're all arguments being given to the if command. Like they're all arguments being given to the pseudo command, but the pseudo command decides my first argument is really a command. My second argument is the first argument. My third argument is the second argument. It just shifts them all. Git is kind of like that too, right? Well, no, because git is a command and then it, I guess it takes an instruction to git. Yeah, it takes an instruction. But in this case, it's terminal command, right? So the second argument, the first argument is a second terminal command that will be executed by the shell, which can be and usually is square bracket, square bracket. Right? The most common thing to want to do, but it could be anything because all that matters is it makes an exit code and everything makes an exit code. Right? So the if just says whatever you tell me to do, all I care about is the exit code, right? You could ask me to format drive C as long as it gives me an exit code, I will quite happily accept that as a condition, right? It's just, I don't care what it is. As long as I get an exit code, I am going to either do something or not do it because I have an if statement. That's my role in life, right? So you say if and then you give it some arguments and it will execute those arguments as a command and it will remember the exit code. And the next command you give it is the command then. And the command then can take either, for pure convenience, it can also take another command as its arguments or it can take no arguments because you are now in alternate if universe. And basically the then and everything that follows the then until you do something else is not really executed yet. It's basically whether or not we execute, it depends on the exit code the if got. If the if got a success, everything after the then is executed and this keeps happening until one of two things happens. Either you meet the fee command that says we're done or you meet an else command, which obviously flips it into a different mode, or you meet a command we'll talk about in a minute called elif ELIF. So this doesn't sound any different the way you described it from normal if then else. Well, except that if, right, but the if statement is you have a code braces around things, right? It's if something then it's also how it works because there's no concept, right? There's no kind of a code block. If a is greater than b then do this else do this other thing. That's just the way you described it just now in these bash conditionals. If a is greater than b returns an exit code of zero, then it would do the then part. Right, but you actually have to give the command then to start into then mode whereas that's not. You're doing JavaScript too, you have to say if then else. I don't know then in JavaScript it's if open bracket, some condition close bracket, open curly, close curly. It's actually in terms of how the compiler works, the if is one statement which has parts which are a condition closed in. And you know something, if it works for you, I'm not going to talk you out of it. If it makes sense to you, I'm not going to talk you out of it. You haven't done a course in compilers, your brain hasn't been broken by doing how compilers work. Yeah, why am I trying to convince you it's hard? No, it's fine. It's easy. If you keep working, I bet you can get there. No, that's not. That's a terrible waste of time. So the then command puts you in this new mode where we keep executing these stuff based on whether or not the if statement, the if command got a success. And then the easiest way to finish up is with a fee, which is just basically if this do that, end the story. But you can also end your then block with an else block. And of course no prizes for guessing that you then have as much code as you want followed by the fee. So you can if then fee or if then else fee. But you always end up with a fee. Can you if then, else then, else then, else then? No, what you can, you're going to get one else. But what you can have is as many as you like LFs, which is LF is effectively a shortcut for else if then. Sorry, else if because you go. Else if, okay. So LF is else if because you still have to do a then. So you can say if then, LF then, LF then, and you're allowed to have one else at the very end. Perfect. So yeah. And you always, always, always end up with a fee. There's always the fee is always sitting there at the end. Just to tie it, that is always your like your closing curly bracket is your fee at the very end. So it is actually quite sensible. So if we look at, we have some examples this time, they're actually available in the zip file because they're a bit longer. So our first example is PBS 145 a it's called simple if.sh. And what we're going to do with this little shell script is we're going to basically we're going to greet the user and we're going to expect them to put their name as the first argument. But if they don't do that instead of shouting at them, we're going to use the read command and there's a typo when the show notes I'm now fixing, but the actual shell script in the folder is correct. And so we're going to use a read command to ask them their name if they haven't told us their name, and then we can greet the user. So in code, we have our usual she bang line at the top. And then we're going to just assume that the first argument was given to us. So we're going to say name equals dollar one, not cuddle together, because we don't cuddle our assignment. And then we're going to use our if statement to test whether or not the name is empty. So if space square bracket, square bracket space minus z space dollar name space, close square bracket, close square bracket. So we're passing if the one argument, which is our square bracket statement. And then on the next line, we say then read space minus B space. What's your name? Fee. Okay. Hold still. I finally caught up to you because I didn't know there was a zip file. I've now opened it up and I'm now looking at the file. So minus z again was, is it an empty string? Yes. So if minus z dollar name is an empty string, so they would have had to have given us the name for it to exist. Correct. So we're saying, so the line above we say name becomes equals. So in other words, if they pass us an argument, we'll take it as the name. Okay. Then we're saying, do we have a name?  So you would, someone would run this script with an argument of their name or not. If they ran it with quote, it's going to say, what's your name? Because it's an empty string, dollar name is an empty string. Okay. Correct. I'm now caught up. And then we just echo, well, hello there, dollar name, smiley face. So hang on, hang on, slow down cowboy. So you've already feed us. We're done, but you can keep going after fee because the fee only ends, finishes the if your script is still running. I've just realized I've gotten away with something there that I got very lucky because of how the shell is very forgiving. That should be read minus P whatever into name, but it defaulted to going into the right variable because the shell does some really weird defaults. That's, that's bizarrely wonderful. Okay, I don't know what you've done wrong. I don't know what it should say. What should, what should the script say? So between the then and the fee, we have our read command, which has a prompt telling us what text to put up. And then we are supposed to have the name of the variable to put the text into. So it should say after the closing quote, it should be space name, but the shell does some very clever defaulting and it actually defaulted the shoving into the last variable it remembered being told about. So it actually defaulted the shoving into name by spooky action at a distance, which I strongly advise never relying on spooky action at a distance. Okay, so let, let me, let me try to say it from, from scratch now. So we're going to store the first argument as the name. So name equals dollar one. So if they executed the script space and a name, then when we test with our if statement, we say minus, minus z dollar name, it checks to see if it, let's see, it checks to see if it's an empty string. If it is an empty string, then read and minus P. And I don't remember what minus P is. It's the prompt. So in other words, what do we put? What text do we tell the user before the pinkie cursor? So read a prompt that doesn't exist yet, but we're going to say dash P, because we're going to tell it, we're going to do a prompt. What is your name, space, and whatever they get back from it, we should have put it into dollar name. Into name. Ah, the name of the variable. No, name, name, name, name. Yes, I did it. Fell right into the trap. You said I would fall into every time. Okay. Yep, well, because I do too. Okay, and then, and then we fee, we stop our if, but the script is still running. Fee isn't, we're all done. It's just, it's not like a, an exit or anything. It's a, it's just saying we're done with the if statement. And at that point, the next line executes, which is echo, well, hello there, dollar name. Correct.   So you can see in the show notes, the output from my terminal for running it both ways. So I did it with an argument first. And I just said, well, hello there, Bart. And then I did it without an argument. And I said, what's your name? I typed Bart. And then it said, well, hello there. Oh, it didn't say hello there Bart, because of the typo in the script. Ha ha. Ah, so it didn't spooky action at a distance. It wasn't spooky action at a distance. I just looked at the, I just got a prompt on my brain. I was like, okay, that works. Because the if statement was correct. So in his show notes right now, it says, well, hello there blank smiley face. Yeah. So I'll just fix it. I'm guessing you're fixing it too. Which is fine. Just get one more. Change is all good. Okay. Well, I'll fix it. Okay. I have fixed it. But that is, that is good reminder. Okay. That is very good reminder. So there we go. So that's a pretty good basic example, right? It's not very exciting, but that is, that is the simplest form of conditional. Only do this thing if something is true, or sorry, if something is success. The next most complicated type is basically your AFLs. So our next example is number B, which is called PBS 145 B, AFLs.sh. I'm very, very much that if I am. Can I repeat just a minute to just make the audience a little bit crazier? Crazier. I just put it in with my name, and it did echo it back. It says, well, hello there, Allison. Before or after you fixed the typo. I didn't fix the typo. Okay. So in the waspukia, there is spukia action in the distance somewhere. The shell does a lot of defaulting, which can be really helpful or just mystery. And I don't like mystery. So I tend to be a very explicit shell scripture. Right, right. Okay. So, so now we're looking at B. Let me open that one. So example B is basically if some condition is true, do one thing, otherwise do the other thing. So this is a very simple script. It's going to basically print out whether or not we have customized bash with a custom bash profile. So we have Russia bang line, and then we say, check whether or not the bash profile exists. So we say if space square bracket, square bracket, space minus e space, tilde slash dot bash underscore profile, space, close square bracket, close square bracket. Then echo, you've customized bash with a profile. Else, echo, you haven't customized bash, or customized your bash profile yet, fee. So depending on whether or not you have a file called tilde dot bash underscore profile, you either will or won't. You'll see one of those two messages. In the earlier examples in your echo, and then you put a sentence in there, you like putting an exclamation point at the end. So you've, the backslash that you've escaped it out. But when I run that command, it shows the escape character and the exclamation point. Why would that?  Okay. Yeah, I could, let me try. I could probably take it out and then stick single quote, single quote, exclamation point on the very end. Because remember, you can catenate strings while just shoving them next to each other. So that would probably do it. So close. So after the E of profile, close the double quote, then immediately start a single quote, exclamation point, single quote. That should work. I think I would just not use exclamation points. That all sounds too hard. But I like exclamation points. I do too. I do too. I'm a big fan. But yeah, no, they are very troublesome in the terminal. Yeah. Very troublesome. I struggle with them a lot. And it's a silly thing for me to spend time debugging, but I often do spend time debugging exclamation points. Okay, well, by the time I see that, Bert will have fixed that as well. Yes, he will, because he just did. I'm getting better at this multitasking thing. So the final example we have, and I'm very much doing this as a setup for where we're about to finish. So the final example we have is our equivalent of LZ, which is LF. And so our final, our second from final example is PBS145C called LF.sh. And again, we have our shebang line. And what this script is going to do is it's going to tell us the etymology of the current day of the week. There are seven of those, so we have seven conditions to write. So if you want to know how you can look up the man page for the date command, but if you call the date command with a format string of plus percent A, it will give you the three-letter abbreviation of the current day of the week. If you don't believe me, type date space, single quote plus percent A, single quote into the terminal, and it should say SAT. Unless you're Alistair right now, which point it's a different time of the year. It's probably already sun, yeah. Or yeah, probably. So anyway, we basically, we execute that command and we shove the output into the variable name D-O-W for day of the week. All right, so that's the first thing we do there. So that's just going to be a three-letter abbreviation. So now we want to print the appropriate etymology, okay? Why is Dow not have a dollar sign in front of it when you're defining it? Because as we learned two weeks, two and so months ago, you don't give, the dollar means get me the value of. Okay. So when you're assigning a variable, you don't use a dollar. Just like with the read command, you don't use a dollar. Yeah, yeah, okay. I got it backwards again. Okay, good. Oh yeah, I told you you would. You told me. Shockingly, this may come up again. Okay, so we say Dow equals dollar and then this date plus ampersand A or whatever is percent A that gives us the today's date. So Dow is always going to be the day of the week. Actually, dollar Dow is going to be the day of the week. Correct. So now we basically start a whole bunch of if statements. So if space open square bracket, open square bracket, space dollar Dow, space equals space, the string mun, which is basically going to test if the value of the variable dw is the string Monday or sorry, the string mun, then echo Monday is named for the moon. Elif, oh, space, square bracket, square bracket, space dollar Dow, space equals space, the string tue, space, close your square brackets, then echo, Tuesday is named for the North God tier. Elif, space, square bracket, square bracket, day of the week equals wed, then echo, Elif, right? You get the way this works, right? So it's basically if, then, Elif, then, Elif, then, Elif, then until you get to the very end. And I'm a firm believer that you always have a final trailing else when I do this in every language, which I often have very snobby messages in because in theory, they should be impossible. This is one of those things where you often see on a shell script, impossible error, right? It shouldn't be possible for this not to meet one of these criteria. But anyway, my echo in this case is, what calendar are you on? With three question marks. I didn't know all those things. Thursday's name for the North God Thor? Yeah. Wednesday is Odin? I didn't know that. Yes, because Odin is Odin, right? Odin is an alternative spelling of Odin. So yeah, so Odin's day. How did Saturday come from Freyja? And that's Friday. Oops, it says Saturday twice. Don't gun it. You can tell I didn't copy and pasting. Okay, so who's Thor? Thor must be Thursday? Yeah, Thor's day is actually. Oh, I see. I see. I'm just reading it wrong. Yeah, because it's this. Yeah, it says if then and then you have. So Thursday is actually Friday. Okay, right. I'll fix this. Yeah. The copy paste, copy paste. I had a lot of copy paste. Now, all right. So A, this shows you how LF works. B, this shows you why there is a second command for doing conditionals because it is actually quite normal to want to compare one value. So we were comparing DoW in each of those comparisons, right? DoW equal, DoW equal, DoW equal. Every single time is DoW this. It's really common in terminal commands to want to do that because you're often dealing with what flag did they set or what option did they pass?  There is actually a command specifically for checking multiple values of one variable. So you basically say, I want to check for this variable if it has this value do this, if it has this value do this, if it has this value do this, otherwise do that. Okay. In JavaScript, that is the switch command. And so it's switch name of variable case, case, case, case, case. But on the shell, the command is called case, which again makes my head rather explode because that's not how normal programming languages work. But hey, that's there we go. That's that's how these things go. Right. So the case command is another example like the two square brackets of something that is new in bash world. And when you say case and then some stuff, the terminal goes into a new mode. It's like between case and it's you're immediately going to know why the last command is ease, esac, case backwards between case, between case and esac, the universe changes. And there are special syntaxes that only exist between case and esac. And so there's extra things your terminal can suddenly do between those two commands that it can't do anywhere else in your script. So the way it works is you say case space and then you give it some sort of value, space the word in. And you have to say the word I end. Right. It just has to be that way. So case something in and that's something could be a variable name. It could be command you execute whatever it is. Whatever it is that ends up as a first argument of case is what you're going to be checking all the way down. So we're going to say case dollar because we want to check all the way down. And what you then have is a collection of one or more pattern followed by the closing bracket, a whole bunch of code as much as we like followed by two semicolons.  And the code between the pattern and the two semicolons only gets executed if the value of the thing at the top of the case statement matches the pattern. So this is harder to say than an example of this part. Oh, absolutely. We're rewriting our days of the week is what we're doing with all of the typos to fix again. Okay. So basically it's once you say case, the next line must be a condition which ends in a closing round bracket. And if you want two conditions, you can separate them with the pipe symbol. And the pattern is in the same format as the grep command. It's called B or E, basic regular expression. But anything that works for grep will work here. So A star means an A followed by any amount of anything. A question mark C would be an A followed by one other character followed by a C. That is how these patterns work. Normally you just put a value here. But so the pattern is just a bloody straight. 99.99% of the time. But you can use star and question mark and a few other things. Okay. So let's just look at our... Oh, yeah. Sorry. One final thing to say. The equivalent of your final catch anything else is the pattern star, which means zero or more of anything. Okay. No idea what any of this looks like right now. This is just a bunch of... Scroll down. ...semicolons and starters and brownie brackets. The next example, pbs145D is the same code rewritten. So that way it makes a lot more sense because you do need to see this. Okay. Let's yeah, let's walk through that because it's jumbly right now. Yeah. So case space something space in. So case $dow space in. In other words, everything between the case and the ESAC, the thing we are checking, the thing the patterns are matching is the $dow. Right. So the first pattern is M-O-N and then we're ending the pattern with space closed around bracket. So M-O-N is the pattern. In other words, is $dow and M followed by no followed by an N. If that is true, we execute all the commands until the two semicolons, which is just echo Monday is named for the moon. Then we have our next pattern, T-U-E end the pattern with the rounding bracket, some commands, Tuesday is named for the North Scott tier, two semicolons, next pattern, more commands, two semicolons, next pattern, more commands, two semicolons, next pattern, more commands, two semicolons. The pattern is always being matched against $dow. So if doow matches the pattern, run this code. If it matches the pattern, run this code. If it matches the pattern, run this code. If it matches the pattern, run this code. And then the last pattern is star. If anything else has happened, if we have anything else, if we're still going, Echo, what color are you on? Oh, okay. Okay. So star is anything that has already been described. What if these patterns were mun space is space good? You wouldn't have been quotes or? You shouldn't actually need to quote them because you're in regular expression land. So as long as your pattern doesn't contain a roundy bracket, which you'd have to escape, you're fine because they're patterns. They're nuts. Remember, we're in special. We're in between the case and the ESAC we're in weird universe. Right? That is, that is a thing that Bash added to the shell that didn't used to exist in the old SH this idea that you can that a command can take you into an alternate universe until you get to another command. So the case command puts us in an alternate universe until you meet ESAC. So it leads to nicer codes. When you can see it, then it does in describing that was that was a tough one to to. Oh, you have no idea how hard those notes were to write. So we're, we're the exit codes involved in this part of the, the, the code, where are we exit codes happening? So the case command doesn't really make use of exit codes because it's checking a value against patterns with the if command. Okay. So the case command, no, no, it does. The case does return an exit code because everything returns an exit code. So the case commands exit code is one, if none of the patterns matched. And if you have a trailing star, that will never be true, but you don't have to have a trailing star. Right? So if you, if no code gets executed, it will return one, which is fail in all of the situations, the exit code of the last executed command will be the exit code of ESAC. Oh, okay. So if at the very end, it just said, well, Oh, you're saying the last thing, the pattern. Hang on. Did you say the last thing before ESAC? The last thing to be executed. To be executed before ESAC. Yes.  Okay. If our week was, if our day was M-O-N, the last command to execute will be Echo Monday is the name of the moon, which would have an exit code of zero. So our ESAC would have an exit code of zero. If somehow our day of the week was none of those things, and it got all the way to the star, then our exit code would be 404 from the exit 404. Okay. Okay. Interesting. So yeah. And again, the if statement, it's all about whatever the exit code is of the thing that comes after if determines whether you go to the then or the else or the LF, etc. And in fact, the LF does a whole new exit code, right? Because the LF checks to see, well, what does this condition give me? So I wish we had Bart. I wish we had homework, even if it was something trivial to do. I should have thought of that. Yeah. I'm out of practice because I really should have thought of that. You're dead, right? I should have thought of that, but I didn't. Because we've been going for quite a while learning new things where we haven't had any sort of assignment. Yeah, I'm out of practice. I'm out of practice. That's sorry, Allison. That's my bad. That's okay. Again, I'm the student in front of the class that everyone else hates. Yeah, but I'm going to dream. I will bear it. I have two weeks to think of something because where we're going now is the next obvious step, which is, okay, great, we can use our exit codes to control conditionals, and we can use the case statement to simplify a lot of LFs. So the next obvious thing that you control with Booleans and any other language would be loops. While something is true, do something. Which in this case is, while something is success, do something, because remember, success is true. And so that's where we're going next. But of course, what's the most likely thing to want to loop over? It's an array. So let's learn about arrays and bash. That seems like the really sensible thing to pair next time. So next time we're going to learn looping and arrays, because they just go together, like as the old sound goes like love and marriage. And then we are almost done of our tour of bash, because the only thing left to do then is to deal with the pipe, to deal with the plumbing. So our piping stuff to and from our shell scripts, which is very useful and very important, but that is we're not ready to do that until we've done all the other stuff. So next time, loops and arrays, and then our final installment will be the plumbing, as I like to call it. So our pipes. All right. Well, this is fun, even though my head did hurt a little bit in the middle of it. I think we powered through and the show notes are perfect. Well, except for all the typos, but they're very helpful. Friday. Well, we'll we'll go over that after we're done recording here, Bart. But this is good. This is fun. I like it. Excellent. Excellent. Well, folks, until we speak again, happy computing. If you learn as much from Bart each week as I do, I'd like you to go over to let's-talk.ie and press one of the buttons over there to help support him. He does 98% of the work here.  If you go over to let's-talk.ie, you can support him on Patreon, you can donate via PayPal, or you can use one of his referral links. I really hope you'll go over and help him out. In the meantime, you can contact me at Podfeet or check out all of the shows we do over there over at podfeed.com. Thanks for listening and stay subscribed.