Episode: 2302 Title: HPR2302: Bash snippet - nullglob Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr2302/hpr2302.mp3 Transcribed: 2025-10-19 01:02:08 --- This in HPR episode 2,300 and 2 entitled Bash snippet, held on, and in part on the series Bash Cripting. It is hosted by Dave Morris and in about 7 minutes long, and carries an exquisite flag. The summary is, after learning about the held on option, I have started to use it. This episode of HPR is brought to you by AnanasThost.com. Get 15% discount on all shared hosting with the offer code HPR15. That's HPR15. Better web hosting that's honest and fair at AnanasThost.com. Hello, welcome to Hacker Public Radio. My name is Dave Morris, and I've got a, well, I hope it's a short show about a Bash thing. Now, I've done a lot of long, long shows, but Bash is more to come probably when I get to it. But I recently used a feature that I talked about in one of those longer shows, and I thought that I would talk about how I used it. The feature is NullBlob, and it's part of the shopped command that I handled that I described and introduced to you in show 2278. I've called this particular show a Bash snippet, and it's about NullBlob. I was thinking I might do more of these snippets if I come up with some things that I think might be useful. So this option NullBlob, when it controls what is returned from an expansion, when no files match the expansion. So when NullBlob is switched on, and a pattern doesn't match, nothing is returned. When it's disabled, which is the default, then the pattern itself is returned, which is a nuisance, or can be anyway. And when I was talking about these back in that show I mentioned, I didn't think I was going to use many of them. I think I even said as much, but today I wrote a script where I used NullBlob, and I thought I'd just share a bit of the code, just a few lines to demonstrate what I did and why. The script I was writing is for handling mail messages that are sent in containing tag and summary updates, had one recently, and that prompted me to do this. I used Thunderbird to receive and manage my mail, and I've configured it to drop these messages into a directory so I can process them. I use Thunderbird's message filters to do this. I think all that I'm using is standard Thunderbird stuff. I don't think there's any add-ons there. And because we get spammed this address as well, I'd be surprised if we didn't really. Then sometimes it's good just to go through this stuff and throw it away or whatever. And sometimes the messages, the valid messages we get, need a bit of work before they're ready to be processed as a typo in them or something. So the directory where these messages are saved, I call the spool area, and in my script I set a variable mail drop to hold this directory. So diving into this bit of code, which is in the in the notes I've put line numbers on them. These are not line numbers in the script, they're just in the snippet. And I left the comments in case I helped at all. So I use a variable called ng to hold the state of null glob before the script actually modifies it. And I do that by using shocked minus p null glob, which returns a list of one command. In this case, which can be used to revert the option that you're dealing with to its state at that time. Then that's followed in line 6 by the shocked command, which actually switches on null glob. Then I have a variable messages, which is an array. And I set that to whatever comes back from expanding a file expansion pattern, which consists of the the mail drop variable, because that's a directory, slash asterisk.eml. So I'm looking for all files of any name, which end in dot eml. And that's what Thunderbird calls them when it does this business of saving them as files. So where are the get back a list of files or a get back nothing? If I didn't use null glob, then when there was nothing in the directory, I would get back the pattern, which is not useful. You probably see in a moment. Then on line 8, I use the eval command, which simply takes the string that it's presented with, and executes it as a command in the in the current script. And I valve the variable ng, expanded variable ng. And that contains the shocked command to turn null glob back on again, assuming it was it was off. I should say turn it back off again, assuming it was off when when this whole thing started. So then in my script, I have a test for the contents of the messages array. And it uses an if command, where in it, it's comparing the number of elements in the array with the number zero. So if it's greater than zero, then what happens is a message is printed, which files in the spool area, and then the contents of the array are printed out. So a list of all the files is listed. If there's nothing in the array, that is the number of elements is zero, then the message the spool area is empty, is output, and the script exits. So after this snippet, then the various files are manipulated. So for example, there's a list of files, and one of them is a spam message. I can simply go through them and delete them. It just saves me explicitly typing the RM command to delete them. Sometimes I stash them away in a holding area. But the script also lets me edit the files and various other things. But I've not gone into that because I just wanted this to be quick. So that's really it. That's it. So the whole point of the null globe was a way of getting a list of files, storing in an array for later manipulation. The script uses the array. I've got this list of files and goes through them in order. So this isn't the only way of doing what I've done. But I like to write scripts that reduce the number of sub-processes as much as I can. So I'm not calling other commands like find and WC, and that sort of thing to count stuff. And storing stuff in an array is useful for scripts where you want to process a list of things I find anyway. It's just the way I do it. I mean, it's not the definitive way of doing it. It's just the way that I do it. But I thought I would share the fact that I'd use null globe and how I'd used it in case you were puzzled as to why you would ever want to use these things as I was when I was talking about them. So hope you find that interesting and maybe you have a snippet of Shell script of your own that you like to share sometime. Okay then, bye now. You've been listening to Hacker Public Radio at Hacker Public Radio dot org. We are a community podcast network that releases shows every weekday, Monday through Friday. Today's show, like all our shows, was contributed by an HBR listener like yourself. If you ever thought of recording a podcast, then click on our contributing to find out how easy it really is. Hacker Public Radio was founded by the digital dog pound and the infonomicon computer club and is part of the binary revolution at binwreff.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. Unless otherwise stated, today's show is released under a Creative Commons Attribution ShareLife 3.0 license.