LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Checking for Alphabets (http://www.linuxquestions.org/questions/programming-9/checking-for-alphabets-784667/)

gregarion 01-25-2010 02:01 PM

Checking for Alphabets
 
Code:

echo -n "read this also:"
read NewAuthor


if [ -z $( echo "$NewAuthor" | tr -d '[[:digit:]]' ) ] ; then
echo "its a digit"
else
echo "something else"
fi

Hey guys , i am trying to do a search to check if the input is using alphabets and nothing else. I tried using [[:digit:]] and [[:alpha:]] but none seems to work


When i use digit, it read 22.k as alphabet and not as a wrong syntax.
For alpha , it does not allow me to input data which has spaces such as " hello world". how can i solve this issue.

David the H. 01-25-2010 02:27 PM

Assuming bash:

Code:

if [[ $testvar =~ [[:digit:]] ]]; then
    echo "String contains numbers."
else
    echo "No numbers found."
fi

Of course, just using [[:digit:]] only won't match non-alphabetical characters either, like punctuation marks or foreign scripts. You'll have to modify it for that.


All times are GMT -5. The time now is 02:11 PM.