LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 04-30-2012, 04:37 PM   #1
dartec
LQ Newbie
 
Registered: Apr 2012
Location: Cape Town, South Africa
Distribution: Red Hat, CentOS
Posts: 2

Rep: Reputation: Disabled
Checking user input to a script


Hi all.
I am reasonably new to Linux and need some help with a script that I am writing.
What I am struggling with is:
The user is prompted to enter a 5 digit number.
How would I go about evaluating that the input is valid?
In another part of the script, the user is prompted for a string that consists of 4 alphas and 4 numerics, eg. abcd4321
Again, I am trying to do the same, as well as checking the case (either upper or lower) of the alphas.
If someone could point me in the right direction, I would be very grateful.
TIA

If I have posted this in the wrong forum, then I do apologise.
I am new here. :-)
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-30-2012, 04:59 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 414Reputation: 414Reputation: 414Reputation: 414Reputation: 414
This can be done with simple if statements.

So you set your users input with the read statement. So...

Code:
echo "Would you like to proceed? [Y/n]"
read CONTINUE

if [[ "$CONTINUE" != *[[:lower:]]* ]]; then
echo "Responses should be all lower case! Exiting Script. No changes have been made!"
exit 1

`alnum'
Letters and digits.

`alpha'
Letters.

`blank'
Horizontal whitespace.

`cntrl'
Control characters.

`digit'
Digits.

`graph'
Printable characters, not including space.

`lower'
Lowercase letters.

`print'
Printable characters, including space.

`punct'
Punctuation characters.

`space'
Horizontal or vertical whitespace.

`upper'
Uppercase letters.

`xdigit'
Hexadecimal digits.
 
1 members found this post helpful.
Old 04-30-2012, 05:00 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,933

Rep: Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692
Quote:
Originally Posted by dartec View Post
Hi all.
I am reasonably new to Linux and need some help with a script that I am writing.
What I am struggling with is:
The user is prompted to enter a 5 digit number.
How would I go about evaluating that the input is valid?
The best way to program is to evaluate and think about each step. You've actually provided the answer to your own question above....you know the user has to enter FIVE digits, and they all have to be numbers. So, you check the length of the string entered first..if it's not equal 5, that's an error. If the string entered has anything but numbers, error.
Quote:
In another part of the script, the user is prompted for a string that consists of 4 alphas and 4 numerics, eg. abcd4321
Again, I am trying to do the same, as well as checking the case (either upper or lower) of the alphas.
Same as above...you know it's got to be 8 characters, so anything else is invalid. You don't say what KIND of script (bash? Perl? python?) you're trying to write, but assuming its bash, you've got THOUSANDS of bash scripting guides you can find, if you looked:
http://tldp.org/LDP/abs/html/abs-guide.html.

EDIT: Kustom42 posted a great response while I was typing...

Last edited by TB0ne; 04-30-2012 at 05:02 PM.
 
2 members found this post helpful.
Old 04-30-2012, 05:02 PM   #4
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 414Reputation: 414Reputation: 414Reputation: 414Reputation: 414
http://tldp.org/LDP/Bash-Beginners-G...l/chap_07.html

Is also a great resource for this sort of stuff. Keep in mind the script I posted was just an excerpt from one of mine didnt want to write a new one so it doesn't specifically meet your needs but gives you an idea of what you need to do.
 
1 members found this post helpful.
Old 04-30-2012, 05:03 PM   #5
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 414Reputation: 414Reputation: 414Reputation: 414Reputation: 414
Lol me and TB0ne both send you to the same link. Gotta love it.
 
Old 04-30-2012, 06:58 PM   #6
dartec
LQ Newbie
 
Registered: Apr 2012
Location: Cape Town, South Africa
Distribution: Red Hat, CentOS
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you for all the input.
I have got the first (numeric) checks working 100%.
No just to tackle the second check, which is the one that I think I am going to struggle with, but that is for tomorrow.
I am using bash, by the way.
 
  


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
script that manipulates input from user restin84 Linux - Newbie 4 09-27-2011 10:36 PM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 07:40 PM
Shell script user input tuckermaddox Linux - Newbie 5 08-12-2004 04:14 AM
Masking user input in sh script? RMSe17 Linux - Software 3 07-20-2004 11:17 PM
User input using a BASH script... causticmtl Programming 5 07-13-2003 10:59 PM


All times are GMT -5. The time now is 06:20 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration