There are two main things wrong in your script:
First, it's $OPTARG
, not $OPTARGS
.
Second,
IP= $OPTARGS | grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}';
You cannot do that. Change the whole paragraph to something along the lines of
if ! echo "$OPTARG" | grep -qE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'; then
echo_text "Please enter a valid IP"
exit 1
fi
IP="$OPTARG"
It's also worth noting that, unless you are using an ancient bash
, your shell should have a =~
operator (see here) that you can replace the external grep
with:
When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex3)). The return value is 0 if the string matches the pattern, and 1 otherwise.