ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Hey guys, what i am trying to do is to allow the user to key in data , such as "23.23" or "24" , as it is the price of certain objects
How am i able to design a check which will allow me to prevent users from typing in input such as "ab.21" or "rfrr" as this field is purely a numerical field.
The problem i am facing is i tried using this search code.
Code:
echo "read this"
read this
if [ -z "`echo "$this" | tr -d [[:digit:]]`" ] ;
echo "True - only numeric"
else
echo "False - there are NON a numeric stuff here!"
fi
when i enter "1" or "a" for input, it will read the correct statements. but when i try inputting data such as "1.32" , it will give me a message saying "False - there are NON a numeric stuff here!". i can change the option to [[:alnum:]] , but then that would allow entries such as "aa.21" to be accepted.
echo "read this"
read this
if [ -z "`echo "$this" | tr -d [.[:digit:]]`" ]
then
echo "True - only numeric"
else
echo "False - there are NON a numeric stuff here!"
fi
shopt -s extglob
echo "read this"
read this
if [[ $this == +([[:digit:]]) ]]; then # +([[:digit:]])?(.+([[:digit:]]))
echo "True - only numeric"
else
echo "False - there are NON a numeric stuff here!"
fi
Last edited by konsolebox; 01-19-2010 at 08:03 AM.
Nopes, it does not need scientific notation. Its just used to enter decimals meant for price of an object.
Sorry Konsolebox , but can i check why does "+([[:digit:]])" need an extra () and + infront of it? and what does "shopt -s extglob" do?
i tried running it like this :
Code:
#!/bin/bash
echo "read this"
read this
if [[ $this == +([[:digit:]]) ]]; then # +([[:digit:]])?(.+([[:digit:]]))
echo "True - only numeric"
else
echo "False - there are NON a numeric stuff here!"
fi
and i got this error message.
Code:
./test: line 8: syntax error in conditional expression: unexpected token `('
./test: line 8: syntax error near `+(['
./test: line 8: `if [[ $this == +([[:digit:]]) ]]; then # +([[:digit:]])?(.+([[:digit:]]))'
is it due the fact i did not put in "shopt -s extglob"?
*this echo out the content inside $this and pipes it into the next set of command
Code:
Code:
tr -d '[.[:digit:]]')
*this command will check if the data from the $this is in digits and has a "." , and if it is, will delete them. if not, it will not delete them , thus the data will not be 0 and this the message that there is a non-numeric stuff is inside.
Lol, yeah, i am using the grep method, but i was asking about the other coding because i was curious about how it work for my general knowledge? is my explanation for it correct?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.