LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Checking user input to a script (https://www.linuxquestions.org/questions/linux-newbie-8/checking-user-input-to-a-script-942616/)

dartec 04-30-2012 03:37 PM

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. :-)

Kustom42 04-30-2012 03:59 PM

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.

TB0ne 04-30-2012 04:00 PM

Quote:

Originally Posted by dartec (Post 4667000)
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...

Kustom42 04-30-2012 04:02 PM

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.

Kustom42 04-30-2012 04:03 PM

Lol me and TB0ne both send you to the same link. Gotta love it.

dartec 04-30-2012 05:58 PM

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.


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