bash uuid generator

Onliner bash scripts are handy but bash and common utilities don’t always work the same on the two systems I most use: Centos vs. OS X.

centos $ cat /etc/redhat-release 
CentOS release 5.4 (Final)
osx $ sw_vers | head -n2
ProductName:	Mac OS X
ProductVersion:	10.6.2

For example, I recently wrote a simple script to generate a set of UUID using the uuidgen utility. OS X and Centos versions of uuidgen take very different parameters.

Of course they do.

Centos uuid manpage

UUIDGEN(1)                                                 UUIDGEN(1)

NAME
       uuidgen - command-line utility to create a new UUID value

SYNOPSIS
       uuidgen [ -r | -t ]
  ...

I like to use the uuidgen -r option to explicitly generate a random-based UUID. It’s not strictly necessary as this is the default behavior. Still, I like to put it in. That’s just me. OS X doesn’t have this option. Oh, well.

OS X uuidgen manpage

UUIDGEN(1)           BSD General Commands Manual           UUIDGEN(1)

NAME
     uuidgen -- generates new UUID strings

SYNOPSIS
     uuidgen [-hdr]
  ...

Next up, OS X generates UUID in upper case whereas Centos generates UUID in lower case.

centos $ uuidgen
18722f8e-14cd-41fb-a63e-af9ff1c287ce
osx $ uuidgen
81AE9EAC-0B8B-4DB9-B262-76AA8C285DD6

Again, not really a big deal but I like consistency. Easy to fix with a pipe and tr.

osx $ uuidgen | tr [:upper:] [:lower:]
62a4d6b9-e0a9-4996-9e71-e7291158b700

But I needed a set of UUID. A simple loop would suffice.

centos $ for i in `seq 1 4`; do uuidgen | tr [:upper:] [:lower:]; done
408bf1d7-80a6-41ee-8a75-f7bbb5b65dd7
ae5e0aa4-f0b2-48ff-9cfe-ab99fb37b5c7
7e0a7e69-364d-4259-9b3f-83d448e9b591
e1d1b257-974e-4754-a6d3-fe4566b55c93
osx $ for i in `seq 1 4`; do uuidgen | tr [:upper:] [:lower:]; done
-bash: seq: command not found

Drat! No `seq 1 4` in OS X.

Okay. Use the alternate form to declare a sequence.

osx $ for i in {1..4}; do uuidgen | tr [:upper:] [:lower:]; done
c861326b-bde8-4198-b45a-6bfb7016addb
ef813568-5d3d-4587-a170-8aab798fd83b
21fe8562-1511-4fd4-bd37-71b43c32e013
acb10051-9af8-42b8-9ac9-54010ad71d07

and verifiy that it also works on Centos.

centos $ for i in {1..4}; do uuidgen | tr [:upper:] [:lower:]; done
93c68aba-cbe5-4b79-a1cc-e00eaae0527a
c564a4f4-9d39-4d2d-8762-4ba506c97de8
f694000b-d2cc-4b31-aabd-c3facd13b081
86466e00-3948-45f7-9090-09ab816b8fb6

Would ruby be easier? Probably not for this simple hack.

If I knew ruby better, dropping into irb would be just as easy as bash oneliners. But there would be other problems. For example, “Which ruby?”

centos $ ruby -v
ruby 1.9.1p376 (2009-12-07 revision 26041) [x86_64-linux]
osx $ ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]

2 Comments:

  1. It’s been said many times before and I’ll say it here:

    OS X ain’t linux

    True. Then again, linux ain’t linux. (hint: debian vs. centos)

    I’ll happily live with the bsd and linux differences over the windows alternative.

    kelly

    2010.03.24
    20:28

  2. FYI, Apple’s ‘uuidgen’ utility is out of compliance with the UUID specification. The spec says:
    • UUID hex strings should be generated (output) in lowercase.
    • Uppercase hex strings should be tolerated as input.

    The spec’s actual language is quite clear:

    6.5.4 Software generating the hexadecimal representation of a UUID shall not use upper case letters. NOTE – It is recommended that the hexadecimal representation used in all human-readable formats be restricted to lower-case letters. Software processing this representation is, however, required to accept both upper and lower case letters as specified in 6.5.2.

    UUID spec: http://www.itu.int/rec/T-REC-X.667/en

    Note that some UUID utilities from Microsoft generated UUID (GUID) hex strings in mixed case, apparently a programming error by the MS staff.

    Thanks for sharing your bash code to fix this Apple (BSD?) problem.

    Basil

    2013.10.08
    14:25

Your email will never published nor shared. Required fields are marked *...

*

*

Type your comment out: