Q:

Check input for alphanumeric and slashes in Bash?

I want to check if the input contains only alphanumeric characters and /slashes. 
My code:
if [ ! -z `echo $2 | tr -d "[\/:alnum:]"` ]; then
   echo "Error: input contains non-alphanumeric characters."
   exit 1 
fi

If I type .script.sh /example/test/ an error is thrown. Why?

A:

You can use tr as tr -d '[:alnum:]\/'
Example:
str="123.abc?/123#"; tr -d '[:alnum:]\/' <<<$str
.?#

You can see in the example above anything that's not alphanumeric nor / gets printed.
Using within if:
str="abc123/"; if [ -n "$(tr -d '[:alnum:]\/' <<<$str)" ];then echo invalid; else echo valid; fi
valid

str="abc123/."; if [ -n "$(tr -d '[:alnum:]\/' <<<$str)" ];then echo invalid; else echo valid; fi
invalid

So Your code should look like:
if [ -n "$(tr -d '[:alnum:]\/' <<<$2)" ]; then
...

