Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at


hpr2302 :: Bash snippet - nullglob

After learning about the nullglob option I have started to use it

<< First, < Previous, , Latest >>

Thumbnail of Dave Morriss
Hosted by Dave Morriss on 2017-05-30 is flagged as Explicit and is released under a CC-BY-SA license.
Bash, shopt, nullglob, filename expansion. 4.
The show is available on the Internet Archive at: https://archive.org/details/hpr2302

Listen in ogg, spx, or mp3 format. Play now:

Duration: 00:07:08

Bash Scripting.

This is an open series in which Hacker Public Radio Listeners can share their Bash scripting knowledge and experience with the community. General programming topics and Bash commands are explored along with some tutorials for the complete novice.

Bash snippet - nullglob

I recently did an HPR show about Bash filename expansion and described the 'shopt' command and its options. One of the options I talked about was 'nullglob' which controls what is returned from an expansion when no files match.

When 'nullglob' is enabled, and a pattern does not match, nothing is returned. When it is disabled (the default) then the pattern itself is returned.

Although I didn't think I'd ever need to, I recently wrote a script where I used 'nullglob', and thought I would share a snippet of the code to demonstrate what I did.

The script is for managing mail messages containing tag and summary updates. I use Thunderbird for my mail and have configured it to drop these messages into a directory so I can process them. I use Thunderbird's message filters to do this. A certain amount of Spam is also received, and sometimes valid messages need a bit of work before they can be processed.

The directory where the messages are saved (the spool area) is stored in the variable 'MAILDROP' earlier in the script.

  1 #
  2 # Find the files and store their names in an array. Use 'nullglob' so we get
  3 # nothing when there is nothing, then revert to the original setting
  4 #
  5 NG="$(shopt -p nullglob)"
  6 shopt -s nullglob
  7 MESSAGES=( $MAILDROP/*.eml )
  8 eval "$NG"
  9 
 10 #
 11 # Exit if there's nothing to do or report what's there
 12 #
 13 if [[ ${#MESSAGES[@]} -gt 0 ]]; then
 14     echo "Files in the spool area:"
 15     printf "%s\n" "${MESSAGES[@]}"
 16 else
 17     echo "The spool area is empty"
 18     exit
 19 fi

The variable 'NG' holds the state of 'nullglob' before the script modifies it. Remember that 'shopt -p' returns a list of commands that will revert the named options to their current state.

Next (line 6) the 'nullglob' option is enabled.

The array 'MESSAGES' is created on line 7 to hold the list of mail files found in the spool area. This is done with a pattern which matches files that end with the string '.eml'. If we didn't have 'nullglob' enabled then when there were no files the array would contain the pattern - which would be misleading.

Having collected the file details 'nullglob' is turned off by executing the command in the variable 'NG' on line 8.

You might think that the script could just turn 'nullglob' on then turn it off again when it's no longer needed. However, I prefer to use the technique I have shown here because it needs to have no knowledge of the state of the option before it's set, and restores that state afterwards.

By line 13 the array 'MESSAGES' either contains a list of files or is empty. The script checks for these two cases by determining how many elements are in the array. Greater than zero means we have files to process and they are listed in lines 14 and 15. The script then goes on to do various things with the files.

If there were no files then the script reports this and exits.

That's it! This is not the only way to do this, but I like to write scripts that call as few sub-processes as I can, and this way appeals for that reason.


Comments

Subscribe to the comments RSS feed.

Comment #1 posted on 2017-05-31 01:20:49 by clacke

Thanks!

I have written a few scripts in my day that do something like first putting a glob in parenthesis, then double-checking whether the array is longer than one, and if it's just length one, check that that thing is a thing and not just the wildcard.

Should have used nullglob. Next time I will!

Comment #2 posted on 2017-05-31 07:38:40 by Dave Morriss

Glad you found it useful

Yes, I'm going to use nullglob in scripts now for sure.

There may be side-effects in other parts of a script - I'm not sure - so I'll turn it off once I've finished with it.

Comment #3 posted on 2017-10-06 05:38:34 by clacke

nullglob in the wild

Happy to note that I have now used `shopt -s nullglob` professionally!

Never do it in interactive shell though, and never `set -u` either. I did so by mistake, while trouble-shooting and making a careless copy and paste.

All kinds of prompt-rendering and tab-completion will fail loudly and hilariously.

Comment #4 posted on 2017-10-07 15:57:09 by Dave Morriss

A wild nullglob appears

@clacke

Delighted you've found a use for nullglob. I too have been surprised by its side-effects - presumably because stuff like Bash completion makes use of it AND doesn't enable/disable it in the way I waffled about in this episode.

There's always a slight air of "incompleteness" about Bash I feel, though it's a hell of a lot more polished than it was. I was forced to use csh and tcsh at one point in my Unix life, and boy does Bash make those shells look terrible!

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the letter P in HPR stand for?
Are you a spammer?
What is the HOST_ID for the host of this show?
What does HPR mean to you?