In the following code the line stating
if ! validAlphaNum $input ; then is never seen to do the evaluation. Can someone tell me what I have missed.
#!/bin/sh
# Ensures that input is only alphanumeric
validAlphaNum()
{
# Validate arg: returns 0 for success & 1 for failure
# Strip all unacceptable characters
compressed="$(echo $1 | sed -e 's/[^[:alnum:]]//g')"
if [ "$compressed" != "$input" ] ; then
return 1
else
return 0
fi
}
echo "Enter input: "
read input
if ! validAlphaNum $input ; then
echo "Your input must consist of only letters & numbers." >&2
exit 1
else
echo "Input is valid."
fi
exit 0
~
if ! validAlphaNum $input ; then is never seen to do the evaluation. Can someone tell me what I have missed.
#!/bin/sh
# Ensures that input is only alphanumeric
validAlphaNum()
{
# Validate arg: returns 0 for success & 1 for failure
# Strip all unacceptable characters
compressed="$(echo $1 | sed -e 's/[^[:alnum:]]//g')"
if [ "$compressed" != "$input" ] ; then
return 1
else
return 0
fi
}
echo "Enter input: "
read input
if ! validAlphaNum $input ; then
echo "Your input must consist of only letters & numbers." >&2
exit 1
else
echo "Input is valid."
fi
exit 0
~