I have a bash script that will take one argument: a product ID. The product ID can be in one of two formats: all numbers, or a mix of letters, numbers, and underscores. Depending on which type of ID is entered, the script will handle it in a slightly different way.
Right now, I'm using getopts
with one flag for each subtype to distinguish between which type of product ID I'm going to be using in the script. For example:
./myscript -n 1034596
or
./myscript -v AB_ABCD_12345
With a simplified version of the script looking like this:
#!/bin/bash
while getopts ":n:v:" opt; do
case $opt in
n)
echo "This is a numbers only ID." >&2
;;
v)
echo "This is a letters, numbers, underscore ID" >&2
;;
esac
done
Since the formats are static, that is, the first type of ID will never be anything but numbers, is there any way to automatically distinguish between the two types of IDs and handle them appropriately without the need for the -n
or -v
flag? So, I could just enter ./myscript 1034596
and the script will know that since the argument contains nothing but numbers it should process it a specific way.