LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-21-2008, 01:55 PM   #1
basildon
LQ Newbie
 
Registered: Sep 2008
Posts: 12

Rep: Reputation: 0
Checking whether a variable is alphabetic


Hi

I used this snippet of code to test whether a variable contains solely alphabetic characters.

case $var in
[A-Za-z]) echo "alpha"
;;
*) echo "not alpha "
;;
esac

Testing the $var with all alphabetic characters resulted in the "not alpha" outcome

Could anyone please advise what is wrong?

Thanks
 
Old 09-21-2008, 02:26 PM   #2
animeshsaxena
LQ Newbie
 
Registered: May 2007
Posts: 20

Rep: Reputation: 0
Quote:
Originally Posted by basildon View Post
Hi

I used this snippet of code to test whether a variable contains solely alphabetic characters.

case $var in
[A-Za-z]) echo "alpha"
;;
*) echo "not alpha "
;;
esac

Testing the $var with all alphabetic characters resulted in the "not alpha" outcome

Could anyone please advise what is wrong?

Thanks
Everything is correct, but you are writing the regex for one character.

[A-Za-z] will match a single character, so when you type a string it won't match. Change the regex to [A-Za-z]*

* ---> More than one character or combinations of this.....for more info...check out regular expressions.
 
Old 09-21-2008, 03:53 PM   #3
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Code:
if [ "${var//[a-zA-Z]/}" == "" ]; then
  echo only alpha
else
  echo not only alpha
fi
Yves.
 
Old 09-21-2008, 04:00 PM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Also, I would use '[:alpha:]' instead of '[A-Za-z]', it's more portable across character sets / encodings.
 
Old 09-21-2008, 04:03 PM   #5
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Good to know. I knew this for regex, but I didn't know this could apply to shell patterns.

Yves.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
function showing a list of variable and value: (dynamic variable name) budhax Linux - Newbie 1 09-19-2008 07:05 PM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 05:21 PM
Replace variable with user defined variable ce124 Programming 10 04-13-2007 09:29 AM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 11:23 AM
Scripting: accessing a variable stored in a variable? tomolesonjr Linux - Newbie 5 05-05-2006 08:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration