I have a shell script at /www/cgi-bin/test that I can access on my network at http://192.168.1.1/cgi-bin/test
.
I am attempting to parse the query string, which should look like d=domain.com
, and validate it against a regular expression:
#!/bin/sh
echo "Content-type: text/html"
echo ""
domain=${QUERY_STRING#d=}
if [[ ! $domain =~ [A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,}) ]]; then
exit
fi
echo "Validation success!"
When this didn't work, I tried using another regex which I stole from here:
if [[ ! $domain =~ \
^(([a-zA-Z](-?[a-zA-Z0-9])*)\.)*[a-zA-Z](-?[a-zA-Z0-9])+\.[a-zA-Z]{2,}$ \
]]; then
exit
fi
I can't get this regex to match either. In both cases, I tried escaping the curly braces (\{2,\}
) according to the Advanced Bash-Scripting Guide, but that didn't make any difference.
In case it's relevant, the platform I'm on is OpenWrt 12.09.
Edit: I just realized my shell script might not support bash's [[ ... =~ ... ]]
syntax. Unfortunately OpenWrt doesn't ship with bash.