Files

100 lines
7.9 KiB
Plaintext
Raw Permalink Normal View History

Episode: 3722
Title: HPR3722: Bash snippet - plurals in messages
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr3722/hpr3722.mp3
Transcribed: 2025-10-25 04:38:31
---
This is Hacker Public Radio Episode 3,722 for Tuesday the 8th of November 2022.
Today's show is entitled, Bash snippet plurals and messages.
It is part of the series, Bash scripting.
It is hosted by Dave Morris, and is about nine minutes long.
It carries an explicit flag.
The summary is, how to use English singular and plural words and messages.
Hello everybody. Welcome to Hacker Public Radio. This is Dave Morris.
Today I'm talking about Bash thing. It's fairly brief, I think.
I hesitate to say trivial. Anyway, I'll be funny useful.
So what I'm talking about is plurals when you're sending messages from a Bash script.
It was prompted by the fact that when you generate a message, it's got a number in it.
So I've got an example here found 42 files or something like that.
Then the day comes when it reports found one files.
And that irritates me. I don't know why, but it does.
So I have been irritated this way to the point that I've gone to find out ways of working around it.
I've say not the biggest problem in the world, but one that irritates me.
And I thought it would be a number of people listening who might feel the same way.
So he's me giving you some hints and tips.
So the simplest method in your Bash script would be method one in the notes using his statement.
So if a variable F count is equal to one, then echo the message found one file.
Otherwise, echo found and then the variable with the number in F count files.
Simple. It's one of the things English does.
Okay. Plurals and Singulars are logical. A lot of the time.
But it would be a pain to have to type this if it's a five line if statement.
Which you could put on one line, but then it would be ugly and hard to read.
So if it had to do that for every message, it's got a number in it. It would really be a pain.
So the next option I thought of was to write a Bash function.
I tinkered around with this before, but recently came up with something which seems to be a bit more robust.
This I've included a listing of it. It's quite short in the notes.
And I have included it as a downloadable file to go with the show.
So I call the function pluralize, which I know discovers hard to say.
It declares three variables as local.
That means they're scoped to the function and get deleted one.
When it exits, they're called singular plural and count.
And they are the three arguments that this particular function needs.
So singular is the text you want to be output when you've got a count is equal to one.
Plural is when it's not equal to one.
And the count obviously is the number that you use to make that choice.
So it's still got the same if statement in it, but you can you don't have to repeat it all the way through your script.
So I've got an example where I set variable I to one and then echo the string found dollar one.
Then in a dollar and parentheses, which is a command substitution called pluralize with the words file files and the contents of variable I.
Or I in other words.
So that comes back to found one file change I to 42 and it comes back and says found 42 files.
So great will be you can make it a bit more complex if you want to.
So I've got here an echo which uses pluralize and it starts with the word there.
Then pluralize is called to produce one of two strings.
The first one is is one light.
The second string is R and then dollar I lights.
So that will get substituted into the string and then dollar I is used as the count for the function.
So when it's set to one, it says there is one light.
You set it to four, you get there are four lights.
And as I said, the pluralize function is available where you to download if you find it useful and interesting.
What actually prompted me to do this show was the fact that I stumbled upon a GNU tool for doing this.
And there's a piece of GNU development which is called the GNU translation project.
And it's got a lot of utilities in there which are really for use in CNC++ and other languages rather than in bash.
But there's a whole bunch of my point at you at the documentation which is quite large and comprehensive.
And I must admit I've completely got my head around all the things that it provides.
And I'm not going to spend time on it other than to talk about this one tool today.
The one tool that I have been using is called ngettext.
There's n followed by a GETXT because there's another gettext tool for other purposes.
What it does is choose among plural forms or singular and plural forms.
And it also implements translations which not think I'm going to be doing here.
But the whole business of translation is quite sophisticated but complex and consists of a set of messages being formulated.
And then the set of translations being produced by the person writing the package or by translators who specialize in that type of thing.
And making a sort of a message catalog thingy that can be looked up to find the appropriate way of expressing particular message in a different language.
So it looks really interesting but I don't have any personal application for this but maybe I will learn more about it and tell you it another time.
So in brief ngettext takes three mandatory parameters and it calls it funny things which presumably relate to the guts of the project more than I've had the inclination to find out about.
The first one's called message ID which is just the text that you want displayed when the count is one.
Then the second one is message ID, hyphen plural which you can guess is the plural form of the same text.
And then there's a count. There are other optional parameters and options and stuff but they're not relevant here, they're relevant to the translation issue.
This tool can be used in exactly the same way as the pluralized example.
I say exactly, I don't know if there's any pitfalls to using it where you happen to try and trigger the translation and it doesn't work or something.
I haven't found them yet but I've not been digging that deeply.
I'll give an example here of exactly the same there is one light, there are four lights thing but replacing my pluralized function with ngettext.
So whether you use this which is probably quicker than using a bash function but I mean in these days it's probably not a big deal.
You're unlikely to be doing billions of these in a run so the efficiency is probably not a major issue.
I think, well let me go into my conclusion, I have been using ngettext in scripts since I discovered it because it's just a nice convenient way of getting the more readable messages.
If you also need to use the translation stuff then maybe this is a good thing to use.
I don't know how you would use it in bash, I've not seen any examples.
My understanding of the GNU gettext project is pretty superficial so rather than use this just because it's pretty useful.
I think I might use a bash function in the future because I don't need all the gettext project functions.
So in my link section there's downloadable copy of the function if you want to play with it.
It's a pointer to the GNU gettext utilities and the page in the HTML version of the documentation which tells you about the ins and outs of ngettext is also a man page.
Don't think you have to install it, I think you get text in most Linux's by default but I'm probably wrong because I only use one Linux version.
And if you're on BSD what I have no idea where you go from there.
Anyway that's all I have to say really and I hope you find it useful.
Okay, bye!
You have been listening to Hecker Public Radio at Hecker Public Radio does work.
Today's show was contributed by a HBLer listener like yourself.
If you ever thought of a coin podcast, you click on our contribute link to find out how easy it leads.
Hosting for HBR has been kindly provided by an honesthost.com, the internet archive and our syncs.net.
On the Sadois status, today's show is released under Creative Commons Attribution 4.0 International License.