Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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...
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.