LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-25-2006, 04:37 PM   #1
TheEngineer
LQ Newbie
 
Registered: Oct 2006
Location: UK
Distribution: RHEL4
Posts: 12

Rep: Reputation: 0
Shell script assistance required..


Hi.

I have been using Linux for a few years now and want to
learn how to program/write shell scripts.

So I decided to try and write a simple shell script that will process the arguments on a command line. It must:

(1) display the number of arguments.
(2) show if an argument is only numeric
(3) show if an argument is only alphabetic
(4) show if an argument is only alphanumeric
(5) exit with an error message if an argument is invalid

This is what I have done so far:



Code:
#!/bin/sh

echo -e "\nNumber of parameters: $#\n"

while test $# -gt 0; do
  case "$1" in
    [0-9]*)  echo -e "Numeric: $1\n"
	     shift;;

 [A-Za-z]*)  echo -e "Alphabetic: $1\n"
	     shift;;

  	 *)  echo -e "Error: $1 is an invalid option.\n"
	     exit 1;;

  esac	
done

exit 0
I am having trouble with (4), how to detect an alphanumeric string.

Code:
[A-Za-z0-9]*
would work to some extent, but it seems to pattern match the obvious.

I would appreciate any help.

Thanks,


J.
 
Old 10-25-2006, 04:55 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I don't have my Regex dictionary in front of me, so I don't know the exact syntax, but I think you want:
((A-Z)OR(a-z))AND(0-9)

ie match a pattern that has either LC or UC letters AND at least one number
 
Old 10-25-2006, 08:56 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
If you want it to match only if there is an alpha AND a numeric, you'll need the '1 or more' regex modifer '+' .
I think it's

[A-Za-z]+[0-9]+

this code is untested ... derived from this page: http://www.tldp.org/LDP/abs/html/x14890.html
 
Old 10-26-2006, 12:16 AM   #4
johndoe0028
Member
 
Registered: Jun 2005
Location: CA, USA
Distribution: Gentoo
Posts: 165

Rep: Reputation: 30
Here, this works.(A little messy, but...)

Code:
#!/bin/sh

echo -e "\nNumber of parameters: $#\n"

while test $# -gt 0; do
	case "$1" in
		[A-Za-z]*[0-9]* | [0-9]*[A-za-z]*) echo -e "Alphanumeric: $1\n"
		shift;;
		[0-9]*)  echo -e "Numeric: $1\n"
		shift;;
		[A-Za-z]*)  echo -e "Alphabetic: $1\n"
		shift;;
		*)  echo -e "Error: $1 is an invalid option.\n"
	     exit 1;;
	esac
done
exit 0
For alphanumeric, I used "OR" characters, and the range of digits and letters to test it. Alphanumeric comes first, because it is the most complex. (It may pass the numeric test, and switch before it goes through the alphanumeric test. [0-9] doesn't test for letters, so it should go after the one the does)

It doesn't care about non-digit characters or non-letter characters if they are padded by digits or letters, though. 7.5 returns as numeric, 5.a returns as alphanumeric, and a.b returns as alphabetic.

Hope this helps
 
Old 10-26-2006, 06:42 AM   #5
TheEngineer
LQ Newbie
 
Registered: Oct 2006
Location: UK
Distribution: RHEL4
Posts: 12

Original Poster
Rep: Reputation: 0
Thumbs up

Thanks to all of you who replied.

I shall look at the A.B.S. Guide - it looks very good indeed.

Best regards,

J.
 
  


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
assistance required v.s.sankar Linux - Certification 4 05-02-2006 08:01 AM
Assistance with 'find -exec cp' shell script dick.swift Linux - Software 6 01-23-2006 10:00 AM
shell script help required syntax error sridhar11 Programming 1 10-31-2005 08:38 AM
small shell script required demigor Programming 6 08-08-2005 04:38 PM
shell script provides an error that a binary operator is required max_rsr Linux - General 1 03-12-2005 08:26 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration