Bash: Compare software version

by admin on

A little piece of code to compare software versions in bash. I used it once to check whether a software was updated or not, then throw an alert, but since it is a general function, you can use it about anywhere.

All you have to do is to extract version numbers and then write this command:

Then you check the variable exit_code, using the following values:

  • 10: the two versions are equal
  • 11: version_1 is greater than version_2
  • 9: version_2 is greater than version_1

Very basically, this scripts recursively extracts the first  number before a dot in both variables, then check if one of the two numbers is greater. I didn’t change not updated anything from the source, even if it’s fairly old, for it seems to me that it’s well coded and it works out of the box, handling almost every situation.


Sed Tips And Tricks

by Michele Brusoni on

This post is just a “Long Answer”. It’s the sequel of this simpler post on a sed problem I had: BigBrus’ Corner – Sed Disable Regex . Here are some tips and curiosity I found.

sed -i ‘s/’ /home/bigbrus_corner/test_sed

Let me explain field per field the above sed command:

  • sed : – well, obviously it’s sed
  •  -i   : means “in-place”, which means “replace in the source file”. Default sed behaviour is to output on stdout – which will probably be on the screen – leaving the file unchanged.
  • the next part is between two single quotes ( ‘  ‘ ) . sed does not need them to run, but it’s a good habit to use, for single quotes tells bash not to interpret what’s between them. In this way you will only have to handle sed syntax and special characters. If you want bash to use substitution, you should probably use double quotes ( ” ” ).
  • s : means substitution; it’s the function you want to execute and it’s absolutely the most common. For more functions, read this: Sed Manual – Other commands .
  • / : just after s there’s the delimiter character. By convention it’s / , for there are many other tools that use the same concept and the same character. sed anyway uses the first character after s as delimiter, so you can really use any character.
  • : the text you want to be replaced. It needs to be properly escaped
  • / : another delimiter
  • : the text to put in place. It needs to be properly escaped
  • / : another delimiter
  • g : it means global. sed will replace all occurrence in the text, not just the first. For more options of the function s, read this: Sed Manual – The “s” Command
  • /home/bigbrus_corner/test_sed : the file you want to edit.

Some other specifications on sed:

  • If you’d like to know, sed uses basic regular expression by default – sed Manual – Reporting Bugs – Regex syntax clashes (the first sentence of this paragraph is quite neat). Regarding which characters you should escape within a text to replace, you should know that it’s a POSIX standard and you can find them, for example, here: Wikipedia – Regular Expression basic
  • Well, I have to admit that I lied to you in the examples. It was to make more evident the difference of choosing the right separator. The fact is, “.” is a special character in POSIX regex. So, I should have escaped it. But, “.” in regex sintax means “match any single character”; incidentally, any dot in IP addresses IS “any single character”, so the string matches correctly anyway. But remember, you won’t be so lucky again! 😉
  • If you would like to use extended regular expression instead of basic ones, you must use -r | –regexp-extended flag.
  • sed with s and without options is “line-oriented”. This means that it will replace only the first occurrence per line
  • What amazed me the most, you can use sed as an interpreter to create scripts. It’s rather complicated, but just the fact you can use it it’s awesome. More informations here: Sed Manual – Example Scripts . If you want to know more on how to use interpreters in scripts, you can read this post of mine: BigBrus’ Corner – SHEBANG! (or #! )

Sed Disable Regex

by Michele Brusoni on

This started like a “small” post on this matter, but eventually it became a very long one, and finally decided to split the argument in two. This is about the problem I had regarding regex. You can find a “more in depth” post here:  BigBrus’ Corner – Sed Tips and Tricks . sed is a command line tool. Of course, I’m no expert of sed, I use it mostly to replace parts of configuration files in my installation scripts. It’s very useful for its ability to use regex and to replace in the same file part of lines, but in fact it is almost a programming language, with so many options to manipulate strings you won’t believe.

Anyway, the ability to use regex can become a problem when you have strings with lots of special characters. Like, for example, a password or a web address.
Well, the really simple answer is: sed cannot run without regex. There’s no parameter, no switch, nothing that can turn off regular expression matching. There may be other tools (I found something with perl, something with pure bash, a partial solution with grep and a “replace” tool, which should come installed with MySQL or something similar…), but not sed.LONG ANSWER

Anyway, most of the times, the problem isn’t with regular expression but with bash escape character – which is . Incidentally, it’s also the most used delimiter of sed (delimiter = a character chosen to separate the different fields of sed).
For example, if you have to correctly replace “” with “”, your sed command will be something like
sed -i ‘s/’ /home/bigbrus_corner/test_sed
which may be fascinating to someone but in my opinion is madness in a string. By the way, this pattern is called ‘Picket Fence’.
The most beautiful option of sed is that you can set its separator to almost any character. And in the most simple way: using it. sed will simply understand.
So, the command above can become
sed -i ‘s|||g’ /home/bigbrus_corner/test_sed
or even
sed -i ‘s#’ /home/bigbrus_corner/test_sedor even

sed -i ‘sshttp://’ /home/bigbrus_corner/test_sed
(it DOES work this last one! I couldn’t believe!! you can use s as a separator)

You can use really ANY character! You only have to put it in the right place and eventually escape any matching character in the string. This example, using i as separator, could make it clearer:sed -i ‘sihttp://’ /home/bigbrus_corner/test_sed

Note that, having used i as a separator, I had to escape every other i in the text to have it work. Anyway, that’s the reason you usually use “strange” characters like @#!/: or similar…

So, as you can see, probably most of the times (with small portions of not too complicated text) you actually don’t need to turn off regex.

Mutt error: “folder” is not a mailbox

by Michele Brusoni on

While developing a script I hit this error. It was a strange one, in my opinion. I didn’t know a lot about mutt and mail clients/servers in linux, so I was thinking “Why the hell a folder is not a valid mailbox????”

/home/bigbrus_corner/sent is not a mailbox. Why??

Probably the right question is instead “What is a Mailbox then?”

Mutt needs all mailbox to be files. So, you simply have to change in your muttrc configuration file (default is something like /home/user/.muttrc ) the guilty field from directory to file.

In my situation it was:

set record = “/home/bigbrus_corner/sent/”

which became simply (notice the missing / at the end):

set record = “/home/bigbrus_corner/sent”


but there are other possible fields, according to your mutt configuration.

Keep always in mind you luck. The error above (which requires a little more knwoledge on how mutt works to be helpful) appears only if you already created the directory. If the directory doesn’t exist, you receive the error below:

/home/bigbrus_corner/sent_nonexistent/: Is a directory (errno=21) . Apart from the error number, this is an error I can easily correct and understand!!!

which states directly that you are using a directory and that it is an error. From which you can deduce that you probably need a file, which in my case was the useful information.

Mutt, like many other mail clients (but anyway according to his configuration) writes all mails he manages in single files, possibly divided into mailbox (you know, Incoming, Sent, and similar).
The structure of those files is quite simple: the complete MIME message (Multipurpose Internet Mail Extension – which is the text message you actually send and has a lot more information than what you usually see), then a blank line, then another MIME message, then another blank line, and so on. Mails are simply concatenated keeping their original format.

This is, with some nice colors, what a MIME message looks like.

What’s peculiar about this, it’s that while Internet is a place of standards (and maybe there are too many of them), one thing that was never standardized with an “official” RFC is the format in which mails should be stored. It was left to developers of mail clients to define a format. I like to think that mutt (and other clients) developers were overdue with work and for once they could keep things VERY simple, so they basically used an append command 😉