korn shell: check the content of a string of a variable

hello,

i have a variable which should have following content :

var="value1"
or
var="value2"
or
var="value2:[digit][digit]*" # example: value2:22

how can i check :

  • if the content is ok (value1 / value2* )
  • the two options of "value2"

when content is example "value2:22" , i want to split it in "part1" and "part2" , like "value2", "22". for spliting i will use "awk"

is is possible to do this with "case" (with ERE) ?

case "${var}" in
  "value1" ) ...
  value2* )  case "${var}" in
                    value2 ) ...
                    value2:* ) ...
...
esac

split :

echo ${var}" | awk -F":" '{ print $1, $2 }' | read part1 part2

regards

Try this:

case "$var" in
    value1) echo value1 ;;
    value2) echo value2 ;;
    *:[0-9][0-9]*) echo part1=${var%:*} part2=${var#*:} ;;
    *) echo "Unknown format" >&2 ;;
esac
1 Like

Might need to flip value2 and value2:[0-9][0-9] around

case $var in
    value1)             echo value1 ;;
    value2:[0-9][0-9]*) echo "part1=${var%%:*} part2=${var#*:}" ;;
    value2)             echo value2 ;;
    *)                  echo "Unknown format" >&2 ;;
esac
1 Like

O by the way, the matching syntax use in a case statement is Unix Pattern matching, not ERE .

1 Like