-1

I found this but i don't undertand it:

if [[ "${ARG//[IVXLCDM]/}" != "" ]]
then
        echo "Wrong input"
fi

1 Answers1

4

This is a fairly crude test. It uses pattern expansion to assert whether a string is composed only of characters acceptable in Roman numerals w/o checking the semantics.

The // ... / would replace any occurrence of any of the characters IVXLCDM in the variable. Only if all of them are OK the result would be an empty string.

Why crude? Because it would happily accept MCCM which is NOT a valid Roman numeral.

Edit: As @LéaGris pointed out it will also accept an empty string as "Roman" and the test with inequality != is inefficient.

tink
  • 14,342
  • 4
  • 46
  • 50
  • 1
    it also accept an empty string which cannot be considered a Roman number, and inefficiently compare with `!= ""` instead of using `-n`. – Léa Gris Jan 18 '21 at 00:57
  • 1
    Fair comments, @LéaGris ... mind if I incorporate that in my answer? – tink Jan 18 '21 at 00:59
  • 1
    go for it, this is why I did not put an answer, yours is plenty good and even better if you mention the extra details. – Léa Gris Jan 18 '21 at 01:02
  • 2
    The extended pattern `[[ $ARG != +([IVXLCDM]) ]]` would at least take care of the emptiness problem, but not the MCCM problem – glenn jackman Jan 18 '21 at 04:56