dimanche 4 août 2019

Check for duplicate word in comma-separated string in bash

I need to check that a variable does not contain a duplicate entry in a comma-separated string.

For example, inside of $animals, if I have:

,dog,cat,bird,goat,fish,

That would be considered valid since every word is unique.

The string:

,dog,cat,dog,bird,fish,

would be invalid since dog is entered twice.

,dog,cat,dogs,bird,fish,

Would be valid since there is only one instance of dog (dogs is there but allowed since it's not the same exact word)

The string:

,dog,cat,DOG,bird,fish

Would also be invalid since dog is not the same as DOG.

Is there any way I can do this? I would put some code I've tried but I don't know what to use to even experiment.

Using bash 3.2.57(1)-release on 10.11.6 El Capitan

Aucun commentaire:

Enregistrer un commentaire