 Well, it's that time of the week again. It's time for chitchat across the pond. This is episode number 762 for March 18th 2023 and I'm your host Allison Sheridan this week Our guest is Bart Bouchard's back with programming by stealth number 146. What are we gonna learn today, Bart? Sorry 147 Yeah, because my show notes until I corrected mine a second ago said PBS 146 of 7 Like it I like it. I still had 146 up and 147 next to it So if anybody's wondering it's programming by stealth 147 And if anyone enjoys snooker, that is the maximum possible break you can have in snooker. Ah Well, that's what we really needed to know Yes, absolutely. Anyway, the number just stuck in my head for some reason. So I I promised you a raise and I promised you Associative arrays and I'm going to have to break one of my promises and I'm very cranky about it I ended up throwing out about an hour's worth of show notes and being Being a grumpy grumpy bunny earlier today, but anyway, we're gonna have a good show regardless because I think I've managed to rescue it But I will explain why When we get that far. So today is a raise All about a raise. But before we get to do new stuff, we get to have a look at our challenge from last time I get the impression from the PBS slack at potfee.com forward slash slack That lots of people had lots of fun with this challenge Yeah, we did Several of us have been chatting and how shoot you put me on the spot here to remember names Somebody started it on of the Ed's Wasn't one of the Ed's shoot. I'm gonna look it up really quick here Not only talked about the challenge, but said, hey, I've put mine up on Github, do you guys want anybody want to look at it? So then I put mine up on Github and and started playing back and forth and started learning from each other Was it Ed shoot at some point? I'm just gonna yell out who it was because I'm having to scroll back and and find it But it's whatever one has a lot of a lot of comments, but yeah, it looks like there was a lot of fun with this one Yeah, it was a big thread which is that was a good sign So the challenge was to write a script that accepts a whole number as an input Either as the first argument or from you from a user prompt then prints out the standard n times multiplication table to the screen and then there was also the stipulation to use the BC terminal command which is the basic calculator and to do the math and Finally for optional credit you could add support for a second argument to specify the maximum number The table should go to so instead of being one to ten it could go to a different number So you will find my challenge solution in the zip file for this installment In fact, it's the only thing in the zip file for this installment And it's in the show notes, so in Inlessing started it. Oh and he and I jump back and forth a whole bunch and then who else jumped in Shoot, I know Ben was interested in playing as well That's the other person that was was playing with us. So Excellent, but it is great to see so for the most part. I think my my solution is pretty by the book I was trying to find, you know, something to pull out of it to draw attention to but for the most part it's just It's the very same building blocks. We were using a lot of the examples So, you know, I Used an until loop to keep asking people for input if they needed to I Used plenty of conditionals with them, you know I think the most exciting thing was a not minus z to look for things that are not empty to see if argument Two was not empty Let me see. I have a standard for loop I use the sequence commands to make my sequence But the word I guess the only thing I did want to draw attention to was that I had never used the BC command before and I just did a really really really quick Google when writing the show notes last time to make sure it was possible to do math in some sort of Easy way and the answer was yes, it is With the BC command and so I was like fine. That was a challenge So I had to go learn it myself and I discovered two things The man page is terrible it makes this command seem overly complicated, but Googling for the answer made it really really easy just Pipe a string with the equation you want into BC and it will give you the answer So if you just type echo the string one plus one and pipe it into BC, it'll give you two No, that's easy So that allowed me to just echo, you know, double quote $n star $num and then pipe that to BC and that allowed me to save my product into a variable I called prod So no, that's not to be easy, but I'm curious what you used a Seq sequence for did did you teach us about sequence? I'm drawing a blank I did so the you can make sequences either if you know the full range You can use the dot-dot the dot-dot syntax or you can use the Seq command It was literally the pretty much the last thing we covered in 146 with sequences Okay, so if you used a while to do that Also fine, like I said, there's an infinity of correct answers to all of these things I was lazy, so I used a four in loop with a sequence But it has exactly the same effect, right? I'm going from one to ten However, you get from one to ten is entirely up to you. Um, I don't know either. Oh go ahead Was there another I was about to start something new so I'll let you go first. Okay, so the I'm not always good at reading instructions So I actually did more than I was supposed to but that turned out to be really fun I didn't just set it to how about how far above one it would go I said you get to define both so you can say I want the multiplication tables from 26 to 2428 and then as I was looking at whole numbers and such I thought well What if I want to use a negative number or what if I want to use what if I I'm not good at reading instructions And I give you the the maximum is smaller than the minimum. I Decided to let them have the table counting down instead of counting up So I gave them a lot of freedom on that and I you can use a multiplier of a negative number with mine And I just I just kept having fun. So what else could I have them do? It was really it was really a gas I enjoyed it I Am so happy to hear that because the idea of the challenges is not that everyone does exactly the challenge It's just that I help you get over the empty page syndrome Mm-hmm. Yeah, and so If you run with it, that's that's a feature not a bug. So that that's fantastic And another small thing to draw attention to in my solution is a little trick I use very very often when I want to make sure I'm getting a greater than zero whole number And I don't want to do any math You can cheat that with a regular expression which is starts with one to nine followed by zero or more zero to nines That means it will be one up So I use that in my solution Yeah, I just it's a little cheese. I thought of I did the same thing I did a square bracket starts with plus or minus and Then a question mark zero to nine with a plus at the end of it Okay, so you're having negative numbers, right? But zero to nine with a plus after it does the same thing as what you said, right? It can be any number of zeros to nines Right, but I don't want zero. I Want a greater well because it doesn't make sense to go from one to zero Because I'm my table was going from one to n numbers. So I wanted a number from one I was a greater than zero whole number, which is why you know It starts with one to nine followed by zero or more zero to nines is the little trick Because it means you get a number greater than one without having to do math. I Also put in some stuff to say. Hey, do you want to define the min and max? I ask them if they want to and if you hit enter It means no if you hit y or yes or capital yes, then you get to you get to define it So I said yes say type yes, but I know people would type a Y. So I just let them do it. That was fun But then I'm curious using the operator. Sorry. I Was curious how you decided to implement the matching of yes or why a common approaches use a regular expression starts with Y and starts with n and basically ignore the rest of the string and do it as a case Insensitive regular expression. So if they type yellow, they would get they would get yes Yeah, it's close enough, right? No, so I did I played with the double pipes for or and I I don't know if I'm saying this exactly correctly, but it had to say if square bracket quote Dollar yes. No, which was my variable. I was looking forward. Did you say yes or no? So if Square bracket dollar yes. No inside quotes equals quote Yes, and without the quotes around the variable name It said I got I found errors on that and it said There was something about posix compatible stuff and so by putting those quotes It seemed to work and I lost track of which way was posix or not posix what I was doing But well, were you using single square brackets at any point double? Okay, I know it was if it said without the quotes if the value if you're doing it with single brackets If the value doesn't exist the value the variable vanishes. So it becomes score a single square bracket equals yes It doesn't it loses the variable itself if you don't put it inside quotes when it's single brackets that's what I did and Yes, and so single brackets were what we said not to use because it's the legacy old sh stuff And therefore it use your double square brackets. So you have modern posix and your life will be easier Yeah, there was a really yeah, I played around with that too. I ended up with a single bracket So I did it backwards, but I did do it both ways Well, I guess one thing just I would suggest that you get into the habit of always using double Just because it will make your life easier. I'm gonna run it again right now. Just to just to enjoy myself with it with it with the double square brackets and No, I'm Going to use this as a a teaser point for something we're about to come to soon so It's having Having the user enter in stuff isn't really the normal way that terminal commands work, right? If you have an optional second parameter in the terminal the way that's normally implemented It was a named argument. So you maybe like table dot sh 5 space minus m space 11 for max 11 or something So those minuses that's terminal nor tournament. Yeah, excuse me can't speak That's normal on the terminal, but we don't know how to do that But it's everywhere in the terminal. So it must be easy, right? There must be a tool for that and the answer is yes, there is it's called get up and we're going to meet it next installment I'm lost. You just said minuses aren't there. What do you mean, right? So your script should be able to work like every other terminal command and if you have something optional You should be able to have it do a minus m for max or minus capital M for max and minus lowercase M for min. I Don't know what those terms what Where did you come up? Okay? M minus capital M. I made it up I am saying terminal commands use minus and then some letter to take optional arguments Our scripts should be able to do the same We should be able to write our script so it looks like a terminal commands would behaves like a terminal commands with supports Minus whatever we choose to make it support Okay, and I don't see the value of that What's not good about the way it is now about what we've written it It's nothing like terminal commands. It's gonna confuse people. It's it's not it's it's it's out of step with the norm of the terminal So you should be writing your shells. I would make the argument that good shell scripts that won't confuse users Or ones that do things the shell script way So you mean when you're giving me the minimum value by which you want to multiply I should instead force them to say minus M and type a number That would be the terminal way. Yeah Okay That seems a lot less human friendly to me, but well, no because if you're making people type input It's not just not in the style. It's also makes your your script or not amicable Because you can't use stuff that prompts on the terminal inside automation. So now that you're in your big buzz about automations Your shell script now can't become part of an automation I also can't become part of a sequence of piped stuff, right because the whole joy of the terminal I always think of Tim for Portland, right each terminal command does one thing and does it well and you connect them together with the pipe But if you're expecting people start typing stuff, then you can't use the pipe to flow the input through Because the standard in is either the keyboard or the pipe as we learn in the next install Okay, I'm catching up to you then but it's so right now the way it's written it You call the shell script and you can put three values after it Which would give it the min the max and and the number you want to multiply by with my script But it doesn't say dash lowercase M Dash capital M, you know, there's no way to tell it which one you mean It's just the order that they're in so they're like they're like dollar one dollar two dollar three Which works, but you're right. I see what you're saying Well, that's okay for two or three possible arguments, but for four arguments it becomes a bit Ooh, and for five arguments, it's really ridiculous. Yeah, right. Okay. Okay. I caught up. So Yeah, so basically what I'm saying is we're gonna make our script work like other terminal commands using the wonderful get up Command then we can have our little minus signs. It's just a little teaser. Okay, so Today's actual lesson is all about a raise or lists in bash and all of the little snippets in the as we go through the text are designed to be copied and pasted into a bash terminal and They all if you paste them all in they will all work. So they all build on each other And they're just illustrative examples as well. So they're doing double duty here So you can play along or you can just look at the commands either way, you know Up to up to the listener slash viewer slash reader slash whatever Yeah, so many ways to consume our content. Okay, so Step one to make an array exist We can create an an empty array using the declare command with the minus a flag for array So we can say declare space minus a space desert list and that will make an array called desert lists That is empty We can then add values to that array by appending to that array with the plus equals operator So we can say desert list plus equals waffles and we now have an empty array that isn't empty anymore Can now has one value waffles We can also use the square bracket syntax or a very familiar with from JavaScript So we can say desert list one equals pancakes Note that they are zero index. So desert list one would be the second element. So our array is now waffles followed by pancakes And if we want to get values out of an array We have to use the dollar sign to pull a value out of a variable but we need to use the dollar sign with curly braces so that the dollar sign applies to The search list open square bracket zero close square bracket You want to say I want the value of all of that So you say dollar open curly name of your array square bracket your index close square bracket close the Curly's So you basically have to wrap the dollar around the whole thing is how I mentally remember it That's a lot of I'm sure to get real tangled on square brackets double square brackets squirrely braces quotes Dollars and I'm gonna make it worse So, well, let's not yet until I finished stressing on this one. So We say dessert list square bracket at and apparently at says give me Whoa, whoa, whoa, whoa. Sorry, you've skipped ahead. You've jumped ahead. Sorry Okay, you say Okay, now I don't know what I'm reading so we we've put Waffles into zero and pancakes into one. So you said to read it So you said dessert list square bracket zero so that should be talking to waffles the first thing we put in But since it's since it's an array you got to use square brackets around the zero So at this point we can't it we don't want to use square brackets We go with squirrely brackets because that makes it less confusing No, the okay, so the difference Okay, so when we're assigning to a variable whether it be a plain variable or anything else You don't use a dollar sign, right? It's name a variable equals value So sticking pancakes into the array was just dessert list open square bracket one close grow bracket equals pancakes Right pulling values out of a variable. You have to use the dollar symbol Right. I'm not talking about the dollar yet. I wasn't there. What do the squirrely brackets mean? That's how we pull things out No, we pull things out with the echo beforehand No, the echo just prints what we have Okay, so you put things out by putting squirrely brackets around The no step back so how do we get if I have a variable called boopity if I have a variable called pink How do I get the value of pink? I'm waiting for you to tell me. I don't know what you're saying if the okay if I have a very Right, but I'm asking about the squirrely brackets not the dollar Dollar is asking for the variable. Okay. I know that right if you say dollar dessert open square bracket You will get dollar dessert which is undefined and then you will get it appended to the string open square bracket zero Close square bracket the dollar has to be told You're not applying to the word. You're applying to this bigger thing. I need the dollar has to be told to stretch its application Okay, so they could have done it with quotes. They could have done it with dashes They could have done it with anything but they chose squirrely brackets to say When you've got dollar and then you start a squirrely bracket look for the other closing thing and I need I mean Find what's inside all of this Right, and they couldn't use the other simple because they already have other meaning so they needed to find something That doesn't that doesn't have a dual meaning right because you can't give a computer a mixed message If you have to have a unique meaning for every symbol in every context So the the curly braces are there to stretch the dollar symbol Okay, you know to wrap it around The full name of quotes after you've got the dollar squirrely bracket close squirrely bracket you got to put it in quotes Well, that's because we want to use it as the input to echo right if I want to echo any string with the space in it I have to quote it right the echo command wants one argument My dessert list could have a space in it. It doesn't in this case. I could have said pop spaced hearts, right? Okay, then I wouldn't eat right so the coating is there like it would be at any other variable Okay, I'm glad I dragged you through explaining it slowly No, it's important. It's very important. I was hoping you would remember. That's why we that's why that's your job here Mickey we do this right? It's very valuable and I really appreciate it So you always say that but I always feel like such a pest but okay. No to the audience. He gives me permission to do this Yes, not just permission. I ask Okay So that gets you a specific element in the array But you often actually want the whole thing In fact, one of the coolest things in bash is that you can turn an array into a list of arguments and You do that by using the special Array index at symbol so when you say name of array open square bracket at close square bracket That means give me every element in the array as a list of arguments and so you can just shove that As a single argument on the end of a command and it will explode Into all the elements of the array as each one a separate argument So if you've an array with four elements and you use the this syntax that becomes four arguments to your command Okay, okay, because it's it's just strings separated by spaces Yeah, which is why the terminal works, right and that means you can use this in Oh, there's a strange. Sorry. I have a very very silly typo here So for dessert in you don't think there's no echo there for dessert in dollar dessert list So you can use this as the you can use an array as the input to your loop Right because a loop expects you to have a list of arguments to loop over Well, an array can be exploded into a list of arguments with the at symbol So we can loop over our desserts by saying for dessert in dollar open squarely dessert list open square bracket at Close square bracket closer curly. Why isn't it quotes part of came after an echo? The echo is a typo the echo is the echo needs to disappear. Oh, oh, that's what you were just saying Okay, so for dessert in and we don't need to Okay, so for dessert in dollar squarely bracket dessert list square bracket at Okay, so that will explode it arguments Will pop or these two values in the array will pop out as separate strings and then what are you going to do with it? Well, they're now coming out. They're now for dessert in It's effectively as if we had tied for dessert in pancakes Waffles or waffles pancakes, sorry So our loop will happen twice, right? So the array has become the values to loop over because we've exploded it out Okay Be cool You will do that all the time. You'll just explode arrays out with the square bracket at close square bracket. It's not short syntax It's not obvious syntax, but it is powerful It fact it's very confusing if I'm completely honest, but you do get used to you And so you can also create an array without this whole declare minus a and then plus equalsing and all that stuff You can just take a list and make it an array, right? We just turn an array into a list where you can go the other way by using round brackets So you can make an array in one go by saying name of array equals open around bracket And then as separate arguments all of your values close round bracket So because it's a list of arguments your space symbol is treated as the separator So if you want to have a space you either have to escape the space or quote the string So to make an example you did both. I did both to prove the point So breakfast list becomes equal to or breakfast is equal open around bracket porridge space pop backslash space tarts space single quote French space omelet close single quote Will make a three item array the first item is porridge the second item is pop space tarts and the third item is French space omelet Okay, I like seeing all both all those examples together It doesn't matter single or double quotes Not in this case if I needed to have the value of a variable inside My string then I would need to use double quotes so that I could have dollar name a variable Okay, but French omelet could have been in double quotes Sure, there's no dollar symbol in there So the double quotes would be telling the will be telling bash to look for variable names to expand and giving it none to do So you it's mildly more inefficient Okay, but it would recognize French space omelet as the new as the value for the third spot the array It would yes, so double quote means interpolated string. So process the string looking for the dollar symbol Okay, okay And single quote means I'm just giving you a string don't even try to interpret it It's just a list of characters. So the double quoted you said is interpreted what? Interpolated the actual jargon is interpolated, which is not a word that means things to human beings That's a computer science word. Well, it's a mathematician word, too But is is interpolated what interpolated quotes So interpolated means that it's the content of this of the double quotes is checked for special characters like the dollar sign and then those characters get interpreted as if they are what they are which is commands So if you say Double quote name of a variable with a dollar sign than the value of the variable goes into the string if you use single quotes The dollar character Followed by the name of the variable will be in the string Okay, okay, I got to make a note of this because I'm gonna I'm never gonna get that straight the next time So just a string is the single quotes and double quotes will will deal with the string But it's really used for looking for a variable name with a dollar sign Yeah, okay. It's like it's like a smart string. It's like it's like more of a string hence It's two quotes. That's how my brain remembers it more of a string. Okay. I like it. I like it Okay, so So far I have already thrown a sea of symbols at you This syntax is not this syntax is not obvious. There is one more piece of there's one more symbol I can throw at this How long is an array? How do I how do I get the length? You get the length by using the count operator, which is the octothorpe symbol and You shove the octothorpe symbol after the opening curly brace and Then you use it in conjunction with the at symbol So to find out how many items on our breakfast list We have dollar open curly brace Octothorpe breakfast list open square brace at close square brace close the curly That will tell us how many things are in the array breakfast list That's exceedingly annoying syntax Yes, I'm not against the octothorpe because it's often called the number symbol Right so the number of the length of yeah, I can live with that But putting it inside the squirrely braces, but I guess I get okay. Let's see if I can get it So breakfast list square bracket at means everything in breakfast list We're gonna put a number in front of that that says I'm not talking about the breakfast list itself I want to know how many things there are in it But then since it's a big long thing that's going to be interpreted as a variable We got to put squirrely braces around the whole thing in the dollar sign beforehand to make it a variable Yeah, that is the logic you've perfectly articulated how bash is working under the hood That is how the bash compiler is looking at that wall of text, right? So you've perfectly explained why it is what it is as a little bonus though If you want to get the length of a string you can actually use the same syntax We just haven't mentioned it before so if you want the length of the first element in the array If you replace that at with a zero you will get the number of characters in pancakes or waffles Whatever one is first. I can't remember where we're at. I think I did waffles first So if you want to see that the length of the word waffles you can replace the ash with a zero It's mostly useful and actually we'll work with any variables So if you have a variable named cat so in the example here We have cat equals Felix then you can echo inside double quotes. There are dollar open curly Octosorp cat close curly letters in dollar cat And that will print out there are five letters in Felix Oh shoot. I wanted it to be three you're right though. That would be five. Okay Yeah And that is what your double quote is doing right your double quote is basically saying all of those dollar this and dollar that Do all the work and turn them into five and Felix If you run that same echo command with a single quote you will see the difference Uh, wait a minute. So if you said echo single quote there are dollar Whatever yeah octothorpe cat you're saying it wouldn't work But it won't work. It wouldn't know what that that variable was in there It will do what it's supposed to do It will make the string but it will literally come out as the character dollar sign the character open curly Okay, okay because it's saying but you gave me a string because you put it in single quotes I'm gonna echo that string right back to you so it's not going to interpret it. I'm sorry. It's not going to interpolate it Yep, that is that is the jargon. That is the jargon. So if you're googling for it, that is the jargon You have to know whether you agree with it or not. That is what it is um, okay And then one last very useful thing so because the at sign explodes your list Into individual pieces and because you can make an array out of individual pieces You can use that to create arrays from arrays So if you want to make a brunch list that contains everything that's for breakfast and also has some burgers and some fries You can say brunch list becomes equal to open the round bracket to start a new array And then inside double quotes for reasons we'll get to in a moment. I haven't forgotten And I did it very intentionally inside double quotes dollar open curly breakfast list open square bracket at close square bracket Closer curly space burger space fries. We get a new array which will contain French omelette, uh, what did we say pop tarts and wherever the hell else was in that array of breakfast list items We're gonna make that array. Anyway, it will contain there were three things in there already now It will contain five Okay, so let me say this again So you said brunch list equals and you did round brackets because that's how we can one of the ways we can create an array And we're going to put three things in that are spaced to limited the last two are easy burger and fries was just spaces between them But the first argument you're calling the array that we already created which was breakfast list square bracket at But we wrap it in squarely braces so that the dollar symbol knows what to consider the variable And then you've put it in double quotes because If I didn't I would end up with a seven element array because French or sorry french omelette would become french and omelette So the double quote says treat everything inside me as being individual items So the quotes kind of get magically multiplied around each element in the array Okay, so again, it's it's interpolating What's in there? It's not just taking the the string values that are inside it because it would have that space in french omelette Yeah, exactly. So and in pop cards as well Yeah, exactly. So and I It takes a bit of working out to make sure you have a right But you can use the pound symbol to make sure you've made your array correctly And if you count them, it will come out as it does come out as five I checked While writing the show notes to make absolutely sure and then I left out the quotation marks and I did it again And it came out as seven Proving that I understand Okay, that's a good idea to use that to say did I am I doing this right without having to go through and Figure every step of it out. You can tell exactly Yeah, right exactly. I thought I was going to get five when I got seven, huh Or I wanted five and I got five. Yay Take your pick So that is all of the pieces of arrays. So we can make them we can add stuff to them We can read stuff out of them We can get the whole array as a nice list of arguments which is really useful for reusing the array and we can And combine multiple arrays into a single array to build up bigger arrays Which is actually way easier here than it is in javascript So that is all of the pieces of using arrays And at this point, I was hoping to tell you about the power of dictionaries in bash or associative arrays as the syntax is called Which would basically be exactly the same but instead of zeros and ones we could have like, you know Donkey and horse as the indexes But alas Although they were added to bash in version four, which was released in 2009 We cannot have them on the mac They also exist in zsh But because they were so late arriving in bash zsh implemented them its own way So they are one of the few things where bash for code Does not work in zsh Almost everything around in bash works in zsh apart from associative arrays So apple so as well as changing Adding new features to bash four that didn't exist in bash three including these associative arrays The other thing that changed between bash three and bash four is the license bash one two and three or gpl two Or gpl one. Anyway, not gpl three bash four is gpl three and apple have a philosophical Problem with gpl three and they are not alone in that it is a controversial license Which is why apple have held back bash to version three on the mac even though version four has been out for a decade on a bit Can you install bash four on the mac? Three guesses how did you want to just have a guess which of our friendly friendly tools would do that for us X code or something off home brew home brew Okay brew install bash will give you the latest bash Okay, so you you can bring your mac up to the latest version. There are also associative arrays in zsh, so you could You choose to use zsh for your scripts, but if you want to have a portable script that works everywhere Then if you use zsh You've cut the windows people out and a whole bunch of linux estros who don't ship a zsh by default If you use bash four, you've cut the mac people out unless you make them install something non-standard So either you make someone install something Or your script isn't going to work for some people Or you avoid using associative arrays. They're all horrible options Now I went and sort of did a bit of mental math I was like do we actually need associative arrays for what we're trying to do here Which remember is to port my my library and we're doing this because we're going to be writing build scripts Well, we don't need associative arrays for the build scripts So I'm just going to not do them because if I do them in bash, you won't work on the mac If I do them in zsh Well, we're using bash for xkpasswd So basically they exist the documentation is out there If you're going to do them know that they're not portable So you writing scripts for you No problem at all use zsh or whatever you like But you writing scripts for everyone If you choose to use associative arrays you have to redefine everyone to exclude someone So how important are associative arrays? Well, they weren't added until bash 4 They what? They weren't added until bash 4 so literally decades of use of bash Right, but that was 14 years ago Right, but bash is from the 70s So bash had three decades of life without them Yeah, but We had a lot of calculus and things didn't work out so well, right? I mean, there was a lot more we could do when we had calculus So as I recall we use dictionaries quite a bit in javascript. They seem fairly important Yes, but in javascript you tend to be solving bigger problems than you're solving in shell scripts Okay, all right You know difference in a programming language and a scripting language so On the whole I actually don't think it's a big deal It just may be really cranky because I I I use associative arrays from time to time with my work hat on but in work We use red hat enterprise linux, which means we have bash 4 and I hadn't realized What was missing from my mac? So your scripts that you're using at work only need to work on red hat enterprise linux 4 Okay, they're they're portable across all of our servers, which are red hat 6 7 and 8 and 9 Not so much sex anymore because that's now obsolete But they used to work on sex and now they're you know, so they've been portable to me in my little My little island universe. They've been completely portable And I just assumed bash is bash is bash and then on the mac I got very badly bitten when I pasted in the syntax. I'm so used to using and I just went You're talking rubbish What do you mean? I'm talking rubbish And I did a bit of googling and then it exploded with lots of cranky people all over the internet Ah Oh, dear So Now luckily you start the show notes three weeks in advance and you learned this like uh 18 days ago, right? That's that's when you found out right? Oh 18 hours at most Oh, no today just after lunch You know, I talked to people who who like get podcasts in the can weeks ahead of time Yeah, we're just in time delivery jit Yeah, absolutely. Well, you know, the teacher is like an hour ahead of the students sometimes maybe two hours Yeah, well, it's anyway Very fresh. So, yeah, I didn't quite go to plan today. But anyway, I still think trust me the arrays are really important Um, and I'm going to try to prove it to you by giving you another challenge Which I didn't dream up until I went cycling Um, and one of the things that was niggling me last time in the challenge is I didn't get to find a way to make You use the select loop. So I've back ported that into this challenge so that we can You know play with more things we learned previously in the series because hey, it's it's all important So I'd like you to write a script to take a user's breakfast order So your script should store the menu in an array And then it should use a select loop to offer the menu To the users along with an extra item not in the original array, which is I'm done And every time the user chooses an item, I want you to append it To a second array representing the order Until the user says i'm done And then I would like you to loop through your Assembled order and print it out. Hi, I'm going to make you I'm printed back out. So lots of looping lots of arrays, right Can I let them have bacon when bacon only canadian ham is on the menu? Well, you're right in the menu. So your choice if you want I'd say go with the menu No That will be bonus bonus credit. No, so they can add to their order not to the menu So the menu will show them a list of what's available and they'll click one for bacon two for eggs And I'll get on to the array of their order. And so their order will end up being, you know, bacon eggs coffee I'm often I don't know. Whatever you like for breakfast yourself And then at the end I'd like you to print out their order If you'd like some bonus credit Then I would like you to rather than Defining the menu in the script I'd like you to read the menu from a text file one item per line And ignore empty lines and lines starting with the opto thorb because if I was Thinking about it, that's much more maintainable to have my menu as a text file and my script as a separate file Instead of hard coding one in the other There's your bonus and also it gives us an excuse to remember How to use a read command in conjunction with cats to pull things out of a file Which we did mention but again, I didn't squeeze it into the previous challenge So I'm I'm trying to backport as much as possible into the challenge No, I like this I like this because you did teach us how to ignore empty lines So I remembered that that was in there somewhere. I don't remember how to do it But I'm pretty sure I could figure it out. That's that sounds fun I don't know why these little I like these little challenges with with small scope That uh exercise just what we've just learned the best when when they start getting Complex and pulling things that we learned three years ago or whatever. That's when I kind of It's too hard That the little ones get me in practicing this one thing that we've learned there this set of things So that that sounds really fun. I'm totally doing it. Excellent. And there's a lot of scope here for you to Improvise and go do many cool things, right? This is very expandable Yeah, so if you are having fun doing this go over at our slack at podfeed.com slash slack join the pbs channel If you haven't already and I'm sure you'll find a discussion If nothing else Ian and I and ben will probably be in there playing and ben ben asked a good question He asked if there was a way for us to have a Some sort of place where we could all put our answers to the challenge so we could look at how each other had Solved the problems and uh burt and uh, I forget who else was helping us look well me and helma were definitely In the mix. Yeah, we but there was somebody else in slack and I'm sorry. I'm so bad at names But maybe that's where ed comes into it because there was there was definitely an ed One of the eds was definitely involved in something. I was reading this week that now is it down, isn't it? Ed well ed to bias and ed ross are both crazy in this category. So it could have been either one or both at this point But I wish it figure anything out But if you if we have figured anything out, it'll be in in slack and the the tricky bit Having these discussions in slack is fun for people who are listening right now But if you're listening to this two years from now You're in 2025. This is not going to do you any good to try to find the thread That's no longer there back in a slack that who knows we've moved on to another tool But we're going to try to see if we can figure out a way to do this in a in a more evergreen method Excellent. So yeah, let's a more time teleporty sort of way. We really won't need to be why me Bart Bartner you're both seeking a solution that requires us to do virtually no work To maintain. Well, I mean, I was I think I was pretty blunt as I ain't got the spare time. So yeah Can't involve me doing time because I don't got it bank empty Right, right, right, and I'll I'll put a little time, but I won't put a lot of time So we'll see we'll see if we can figure it out Yes, okay. Um next on our agenda is to do our terminal plumbing So basically next time I would like to make our scripts behave like other terminal commands Because that way we can do everything with our scripts that we can do with the terminal command that we can pipe things into them and out of them and All of that goodness and therefore all of the skills particularly the skills you've been practicing We're using terminal commands as part of your bigger automations All of that suddenly becomes open to you having that with your shell script So instead of you having to inside some sort of automator action or inside a oh, what's that little tool you adore? Thank you Inside keyboard maestro having to type in like, you know 400 lines of code You can just have a shell script where the code is And have keyboard maestro or whatever reference of the shell script And just use it in your commands like you would use any other terminal command, you know echo or cat or whatever And that's powerful like that is Proper powerful also means if you're more in the server space cron jobs and all these kind of things Your your custom stuff becomes a first-class citizen I can just do everything else and that makes me very happy with with my work hat on But I really like when I can do that. Yes You know what else I like about what you've done here with with this series and very much of the the Git series is someone can jump into programming by stealth and just learn the the shell scripting piece That you don't have to listen to all of java script and all of html and all of css to get get up to this point You know, you don't have to listen to the first 140 episodes to learn shell scripting So I like these little series within a series. This is this is very fun Yeah, it was a complete accident, but I don't think we've ever had as much traction as we've had from the git stuff I guess yeah, oh people love the git stuff Yeah, and there seems to be a lot of people on planet earth who are confused by git and who seem to enjoy our Discussions and seem to find you asking me questions the really good way to get the answer Which is great yet One of the things I was talking about in the back channel is working on these shell scripts that I'm sitting there in the terminal working on a shell script and It is perfectly natural to then do git from the command line Where everything else I've ever done. I've always done it using a GUI And so I'm getting much more comfortable I I did run into a problem of I didn't remember if I forgot how to or I forgot to put in a git commit message And I was like No idea how to amend a commit message. Let me open up git kraken and fix it But I'm sure you you taught us in there somewhere. Oh, yeah, but uh, you know, at least I'm doing the rudimentary stuff I'm getting very very comfortable doing it from the command line And I I feel somehow more powerful than I'm typing everything out by hand than opening a GUI It just feels good. I like it I think the dirty secret of every sys admin is that they use both guis and terminals It's not either or it's yes on that moment Right What is the least Effort to get me from where I am now to where I wish I were five minutes ago If it's a GUI, I'll use a GUI if it's a command line and use the command line I'll probably use both within the next five minutes that that is the life of a sys admin And that is that is fine. That is normal. That is good. Don't feel bad about using guis. Don't feel bad about using the CLI They're all great. They're all helpful. So I uh since this was a short show remind me Have I talked about keyboard maestro and git together on pro get together on programming by stealth? Have I mentioned that I don't believe you have and you Well, no because you only published it in last weekend's regular show So you definitely should plug that installment because it's very relevant and very good Yeah, so, uh, what I was looking at keyboard maestro is a really powerful automation tool for the mac and I really like it It's great and everything but it's got it's got one missing piece You basically can't go backwards if you mess up a script if you do a bunch of changes and you can't remember what you did You just can't do anything about it If you like let's say you delete a step accidentally you can hit command z and go backwards one That's fine. But you can't really there's no version control. There's no dial me back But uh a guy named dan thomas wrote a keyboard maestro macro two of them actually One that takes the entire database of keyboard maestro. That's that's one problem. The whole thing is one giant database It's not separate files for every uh every macro you've written So this tool separates them out into into individual files. That means you can then track them and get so once you've got uh, you you you um, um, You authorize what is it called? You authorize a repo you make a repo. I can't initialize initialize initialize a repo out of This database that gets spit out and then as you make changes you could do, you know Get status you see which one you change to do a commit message and you can either just keep it locally Or you can push it up to get hub whatever you want to do But then it also he's got another macro that allows you to go back and get one that you've messed up and go back to a previous commit I haven't had the nerve to Test the second part But i'm feeling pretty good that I now have a history of the ones that I when i'm changing them It does require a lot of discipline to Run his macro go out and do get run his macro go out and do get you gotta you gotta be rigorous at it But I've had so many macros that were working and then I screwed them up And I don't remember how to get back to where I was and I'm Really glad that this exists, but it shows that Get is part of my brain now that that I expect get to be there to save me and and that kind of made me happy And I would expect that would make Bart happy as well It absolutely does because If we if we teleport as I was back to episode one of pbs, which is I I dread to think how long ago episode one was It's many many moons ago But I think I would have very strongly made the point that the whole reason to become A coder is to make the computer work for you Don't be a slave to the computer make this computer a slave to you And these kind of automation tools git. This is this is the power you get from being able To make the computer do your bidding and it's very satisfying when you can make the computer do what you want Yeah, yeah, that's it if you it's a cross between that power and solving a puzzle Right, which is I feel like very dopamine rewarding. Yeah. Yeah And that is why I'm gonna get very I'm gonna jump on the soapbox But if you're thinking of a career The reason you want to be assisted men is because you are paid day in day out to solve puzzles That's your job. You go in every day and you solve puzzles and when you get it, right people think you're Superman We get it wrong not so much Yeah Right, um, well next time as to say we are going to do our terminal plumbing to make our scripts We have like normal terminal commands and then we have a few more little odds and ends to wrap up and what we're going to finish with is a sort of a A crib sheet for bash That is going to be like a one page summary of all the weird syntaxes white double square brackets What does that mean single round bracket single curly bracket? What? Right a little quick crib sheets that you can jump back to and we're going to do that as an episode of taming the terminal not Yes taming the terminal so that basically we can link to taming the terminal and say Here's the summary And here's the detail And then the taming the terminal feed will have a single entry that encapsulates bash With jump outs That points back to all of the detail we've just done here That's going to be good. I like that. So then I'm going to have to print that out with my new laser printer and stick it inside my paper bound book Well, to be honest, some of these things are actually worth printing out I do I do have a whiteboard in work where I do have printouts of some very useful vim commands Although actually I've picked up a great new tip. I found a sticky notes app That stays on my desktop and I have a link to 100 cool vim commands and every month I put a different command in the sticky and then I make it my business that month to use that command So this month I learned g and g capital g to go to the bottom of a file and gg to go to the top of a file They were my learnings for this month. Oh interesting interesting So next month I'm going to pick something else and then at the end of every month. I know a little bit more of him I like it. I like it. That's a way to learn So until next time lots and lots of happy computing If you learn as much from bard 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 I'm just the stooge that listens to him and asks the dumb questions 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 pod feet or check out all of the shows we do over there over at pod feet dot com Thanks for listening and stay subscribed