This in HPR episode 2,739 entitled Pastips Nighting out as what I call a rain and in part of the series Past Crypting. It is posted by name and in about 26 minutes long and Kariman explicit flag. The summary is a rain in past part 4. This episode of HPR is brought to you by Ananastost.com. Get 15% discount on all shared hosting with the offer code HPR15 that's HPR15. Bit your web hosting that's AnastonFair at Ananastost.com. Hello everybody, this is Dave Morris for Hacker Public Radio. Today I'm doing bash tips number 19 and I'm still talking about a raise and this is part 4. But it's the last in the group about a raise. Do I hear a sigh of relief? In the last show we looked at parameter expansion in the context of a raise. There's more that could be said about that but I think I'll probably not say any more. Unless I'm called to do so in the future. But in this episode I want to look at the command we've already seen in brief called Declare. It's also got a synonym which is Type Set. And then there's some other related commands read-only and local. And there's a couple more that allow you to load data into a raise which are called map file with a synonym read array. And read which we've seen in passing several times. So Declare or Type Set. It's there because the synonym is there because that's what it's called in the corn shell. And so it was been added to bash for that reason. So we've seen how you used Declare to create a raise and so forth. But it's used generically to create variables and to give them attributes. There are a whole list of options to Declare but rather than going through them all. I don't think I need to do really. I thought I'd just spend a little bit of time looking at the ones that relate to a raise. Partly because I found them quite interesting to discover few YouTube back when I first started playing around with the raise. Now there are, this command is a little odd. There's not many commands do this. The options usually consist of a hyphen followed by a single letter. Quite common in Unix to do it that way. But some of them you can reverse their effect by replacing the hyphen with the plus sign, which is all fine and nice and stuff. It's a bit confusing because there's not much else that you can do that with. Anyway, let's start with an option you probably not going to use very much. But I think it's quite cool. So I've already mentioned it. It's the hyphen i, lowercase i option. And what that does is make the variable you're declaring, which is a raise, what we're thinking about here. Behaviors of it's an integer, only an integer. And whenever you assign anything to it, an arithmetic evaluation is performed. As if it was inside the double parentheses, which we've talked about in this series. So I thought the easiest way to explain this was to put together a downloadable script, which is called bash 19 underscore ex 1.sh. And it shows what you can do with an array to clear this way. So just to go through it briefly, because I think it's pretty well self documenting as little comments in it. I'm declaring two arrays. One is called int i and ts and it's declared with hyphen a and then hyphen i and the name ints and integers. There's another one, a hyphen a and I've called the array norm, because it's just an ordinary array. Indexed arrays in both cases. So I put into ints a couple of values in by putting an assignment statement ints equals. And then open parentheses and a couple of things being added there. And the two things I'm adding are a couple of expressions in quote. So they're quoted as strings effectively, but then numeric expressions within them. So we've got 38% 7. So it's using the modulo operator, which divides by 7 and gives you the remainder. And then the next one is in quote 38 slash 7, which is dividing, doing integer division on 38. And the norm array gets the same stuff. Then for each of the arrays I add a string to the end of it. So I've put the string jellyfish onto the end of each one using the append operator. So the plus equals to append another value to the end of the array. Just so you can see what the effects are. Then the script echoes the contents of ints and the contents of norm to show you what you get. Well, with ints you get the values 3, 5 and 0, because the value of 38 mod 7 is 3. And 38 divided by 7 is 5. And then the value of the string jellyfish, because it's not numeric, is treated to 0. Anything that's not numeric, any string that's not numeric is regarded as a 0, which seems sensible. I think we covered this reasonably comprehensively in when we were talking about arithmetic expressions and stuff. When you come to look at the contents of the array norm, then it's just got the string 38, space per cent, space 7 in one element 38, space slash, space 7 in the next and jellyfish in the last one. So that's all really it's pretty straightforward stuff, but quite useful I would say. Then we come to the hyphen L and the hyphen U both showcase letters. These options force any alphabetic characters that you store in this array. It forces them to lowercase in the case of hyphen L or uppercase in the case of hyphen U. So obviously when you declare these things, you're only to declare one of these at a time. So this is mildly useful, I've never used them as I don't see a use for it personally. There you go. The last option I want to mention is hyphen lowercase R. Now what this does is it makes whatever you declare read only. So when you are declaring me also have to initialise them, because you can't change their contents at all afterwards. So it's a way of declaring constant. And I've got an example that sets a simple variable on array in this case. But the same applies to arrays. I've declared hyphen IR. You can concatenate these options by the way. So the I means it's an integer, the R means it's read only. And I've called it 6th prime and I set it to 13, because I used the 6th prime. Then I tried to set 6th prime to 17. I get back the the error message bash call on 6th prime call on read only variable. Now you think well can I turn off the read only capability. So try to clear plus lowercase R 6th prime, which by rights should turn off the read only thing. But because it's read only, I guess the logic is you can't do it and you get back an error message. Just in passing there is a command, which comes from the born shell, which is equivalent to declare hyphen IR. And it is read only. So it takes some of the options that declare takes. And it lets you create arrays and variables and stuff, which are read only. So it's doing the same thing, which is just that bash is a sort of a blend of the older born shell. This is why it's called born again shell, of course, think I've said this before. The next command is the local command. It takes the same options and types of arguments as declare. But you're only allowed to use it within a function. So if you're creating a function declaring a function, then you can use the local command to create variables and stuff within it. Now, I have demonstrated this when I've talked in other shows about creating functions. But in this particular sub-series, I haven't really explained functions in detail. So I'll leave the rest of the use of local to that when I get to that. But the point of it is that it makes the variables that you declare local to the function. So they work within the function, but as soon as the function exits, they vanish. So it's like other programming languages. Now, we move on to what I think is a very, very useful command. So I didn't know about until maybe earlier this year, I think. Never used it much myself, but now I've discovered it and understood it. Find it extremely useful and it's map file, which has the synonym reader A. So what this does is it reads lines from standard input and puts them in an index to ray. It can also read from a file descriptor, which is a way of connecting bits of a script to a file. But I haven't looked at this yet. I plan to talk about this in an upcoming show. It's on the to-do list anyway. So I'll leave that. I won't talk about it here, just to mention that it exists. So I've prepared a command syntax diagram, he thing, which shows all the options and what you can follow them with, which I've grabbed from the GNU bash manual, included in the long notes here, and all of the options and arguments are optional in fact. We also made a table of all of these options where I explain what they do. Just to talk about them briefly, you can change what delimiter terminates each input line. In other words, define a different end of line. A bit like the way you can do stuff like that in ork. You can control how many, and that's what the hif and d delimiter option. You can define how many lines are to be read. So if you're offering map file, a file, and you want to read the first few lines from it, then you can control that. If that is with the hif and n option with a numerical count after it. If the count is zero, then all the lines are read. And if the hif and n option is not used to, then it reads the entirety of the file. You can change where the lines are written into your array, because normally they will clear the array and will start at zero to write values in. And that's done with hif and capital O, and then the numerical origin that you want to write to. You can, if you wish, skip a number of lines from your data source from your file, with hif and lowercase s, and then a number of count. And here's the thing, I think it's quite nice. The hif and t option, which doesn't have a parameter to it, you can remove whatever the delimitory is from each line. So if you're reading stuff separated by new lines, which is a pretty common scenario, using hif and t will throw away the new lines. So when you come to list out the contents you're at the array, you don't end up with new lines after each element. The hif and lowercase u is followed by a file descriptor, but I'll not talk about that for now. But it changes what source the map file reads from rather than from standard input. Then there are two things that I have played around with, but I don't know how useful they're going to be. There's hif and capital C followed by a callback. A callback is either an expression or a function, which is going to be called by the map file command. Every time a certain number of lines are read. The number of lines is called the quantum, and that's defined by the next option, which is hif and lowercase C and a number. So if you don't have the lowercase C number thing, then it's every 5000 lines that you've read in. But you can change that. I've got an example coming up to demonstrate this. So the final argument to the map file command is the name of the array. That's where the lines can be read. You need to have created the array first. If you don't specify an array, then there's a built in array called map file in uppercase into which stuff will be written. It's quite an interesting thing that when the callback is invoked, whether it be a function or whatever, then it is given two values. The first one is the index of the next array element that's going to be assigned, and then the line that's going to be assigned to it. The callback functional expression is evaluated after the line is read, but before the array element is assigned. So you could use that to filter stuff out if you really wanted to. You could do some quite clever stuff in there. I haven't experimented with that very much, but I do have an example of using it coming up. So there's a downloadable script that demonstrates some of the features of map file, and that's bash 19 underscore EX2, which is moderately long actually. Now I come to look at it, but carried away, but I just threw in a bunch of things that you could do to fill an array using map file. And I've alluded to this along the way, because I've been filling arrays in loops, and it's not necessarily the best way of doing it. So in my script, I've got, I'm declaring an index record map, and then call map file with the high from T option and the array name map. And I'm feeding it through a redirection, so it's using a less than sign to redirect from the next thing. And I'm using one of these process substitution things, which is a less than sign, and then commands in parentheses. And what I've got in the parentheses is a for loop, which goes for i in, and then a brace expansion of 1 to 10, so it's going to iterate through values 1 to 10 setting i to each one. And then semicolon do, then the actual meat of this loop is an echo echoing dollar random random being the uppercase variable name, which gives you a different random number each time you call it. semicolon done, and that's the closed parentheses. So what that will do is it will fill the array with 10 random numbers, and it, because we're using echo in the loop, it will be generating a new line. But map file needs the new line in order to tell where one line ends in the next begins, but I've told map file to throw it away when it stores. So then use an echo to print out the contents of this map array using the syntax for print out an entire array using the asterisk as the index. And you can see that later on in the notes, I just run it and put the result, just a list of 10 random numbers on one line. Next thing, define a string, just put in a simple variable called words. And when I did was I put few lines from words with poem in there, where all the spaces had been replaced by underscore. Really just to demonstrate how you could use alternative to limit us. So I wandered lonely as a cloud and all that stuff is in the map file command that plays with that, uses hyphen, lowercase D, and follows that with an underscore. So the delimiter is the underscore between each word, and I use underscore hyphen T to strip off the underscore's, and I put it into an array called DAF's DAWFS, because it's about DAF adults. Oh, I forgot to say I declared it before. So what that does is another process substitution, which simply echoes the string called words, without trailing new line, uses echo hyphen N to do. So that will load up a using map file that will load up DAF's with all of these words, one per element, and we'll have removed the underscore's that's doing it. And there you go. And now echo the contents out again. And you can see after the the script there's some example output from it, where it, to print out the words of the poem, one online. The final demonstration in this script is to declare another index tutorial, which I called big in this time, in this case, because it's going to contain many content, many elements, well, 100, it's worth. Then I use map file with hyphen T, okay, it's T that is, and then hyphen capital C. And following capital C is a print F. So the print F statement has a format, which consists of percent zero to D, space percent S backslash N. And what that will do is it will expect two arguments, first of which it will treat as a number. We'll print it out with two decimal places leading zero, and the second one will just treat as a string. So that's all in double quotes. Then the next option is hyphen located C followed by the number 10. So the quantum is going to be 10 every 10 lines that it gets. It will invoke that call that. Then the name the array is big. We're using the same structure again of a less than sign to redirect and then process substitution, less than sign, and then bracketed list. We're using the F at present printing or extraction or words from user share, and then we're going to create words, this time I've used shuff and asked for 100 words. So the final bit of this script simply echoes the contents of the array. We should have been seeing stuff going into the array as it as it, the map file was being invoked, but I'm printing them out as well. So the first thing that's printed out is the number of elements that we got in this array, and then the second thing is the fall loop which begins setting i to 9. And iterating until it reaches the number of elements in the big array. And then we're incrementing the number by 10 at each iteration. And inside we're using pretty much the same print F command where we're saying format anyway. So I won't read it out again. It's printing a number and then a string. And the number is coming from the variable i, and the string is coming from the array element, the i, the rail. So when you run this, you get running, we're getting random words out of it. So what I'm seeing on the screen won't necessarily be what you will see in the notes, because I probably probably run them again before I, before I, I submit the show. But there's a list of elements that are going into the array, every 10th element. So the first one is zero. First one that will be added to the array will be the zero. And that won't be shown. But then the next one, the 10th element, will be the one with the index of 9. And we get a word out and then the 9th and the other word. And so forth up to word number 99. And the loop does pretty much the same. It says there are 100 elements and it prints out the same stuff. So that was just really a demonstration of what you can do with that file. Personally, I now use map file to load load arrays pretty much all the time. And I'm doing this type of thing. So I find it amazing, useful, I hope you feel the same way about it. So the final bit of this show is to mention the read command. We've used read loads loads time and all sorts of examples in this bash tip series. But we haven't looked at the option, which is, hif and locates A followed by a name. What that does is to load an array. And the array is named with whatever you put instead of name. But because read reads one line at a time, then we're assuming that all of the words that are going to the array are all on that on a line. And more to the point, the array is, if you keep nominating the same array, then the array will be overwritten each time read is called the same array. So if you're doing this in a loop, you'd have to be very careful to do it for that reason. All of the words to go into the array need to be on the one line. And the words on the line are split up using the word splitting process that we described in episode 2045. That was what we were talking about. The whole subject of expansion and word splitting is one of the things that happens when a command or a file spec is being expanded. And it was described in that. So I've created a third downloadable script, bash 19 underscore ex 3.sh, which has a little demonstration of the use of read with this particular option. I'll talk more about the other options later on, I think. I don't have a particular slot to do this, but I'll talk about them in passing, I think, as I go forward with this series. So in this script, we start by declaring an index array, which in this case I call read test. Now, I thought there are ways in which you could present a read with a single line, read is normally reading from standard in, which means you would have to type this stuff. It's not a straightforward thing to put it within the script, though it is possible. But we haven't really covered what this would require yet. So I'm not doing it the way that you might do if you were doing this for real. I've just done a sort of demonstration of generating some data on standard in and poking it into an array. The read consists of read space, hyphen, lowercase r. That's the thing that takes away the meta status of backslash things like backslash n and so forth, basically ignores them in the, it doesn't give them their special meaning. And that's followed by space, hyphen, lowercase a, and then read test is the name of the array. So that's the read command we want to do. We're going to read a line and put it into read test, which is an array. So this one is using the same stuff as we've been using in previous examples in this episode. It's using a less than sign to redirect some stuff from a data source. And the data source is another one of these process substitution less than sign open parenthesis. Some stuff inside the parentheses close parenthesis. And what we have inside this inside these parentheses is a full loop. We're setting 4c in and then we're using a double brace expansion. The first brace expansion is capital A to capital J. That's first 10, that's the alphabet of the case. Then in the next set of braces, 1 dot dot 3. So you'll recall from when we talked about brace expansion that this will generate sequences. Like a, 1, a, 2, a, 3, b, 1, b, 2, b, 3, etc. Each time around this loop, I'm not reading out the minute details of this, we echo the contents of dollar c, the variable c, but we do it with echo hyphen n, which suppresses the new line because we want just one line to come out of this thing. Okay, so this will generate this list of a, 1, a, 2, a, 3 stuff, which will then be fed into the array. So to demonstrate what actually happens, simply echo out the number of elements that are in the array, which will be 30, is 10 times 30, and then echo out the contents. So as I said, we get a 1, a, 2, a, 3, b, 1, b, 2, b, 3, c, 1, c, 2, c, 3, etc. Add north hymn. So hopefully that demonstrates the purpose of this quite well. So with that said, that is the end of this particular episode, and of the sub, sub, sub section talking about arrays. And I'm going to give the bash tips series a little bit of a rest now for a month or two. Well, I do something else because you probably bored sick of it, and I can do it a bit of a change as well. So then I'll come back to it in due course. Okay then, bye-bye. How easy it really is. Heck, a public radio was found by the digital.com, and the Infanomicon computer club, and is part of the binary revolution at binref.com. If you have comments on today's show, please email the host directly. Leave a comment on the website or record a follow up episode yourself. On this otherwise status, today's show is released on the creative comments, attribution, share it like, feed us our license.