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.
if ! [[ "$mynum" =~ ^[+-]?[0-9]+[.,]*([0-9])+$ ]] ; then
echo "not a decimal"
else
echo "is a decimal"
fi
Code:
a="${mynum//[0123456789]/}"
You can check for the existence of a period or comma depending your locale number format but in addition it depends on how you want to treat the number "X.", I believe it should be considered an integer versus a decimal number.
This is the case when I was really tired I think. sorry for that.
But actually I can only repeat myself: bash is not really suitable for this.
Quote:
Originally Posted by Skaperen
but if i can trap errors correctly, maybe trying a conversion could be a way to test for validity without all that slow/inefficient.
Not really usable, since you cannot do decimal operations. So better to use another tool, which can do the calculation and also easily validate decimals, floats and also scientific notation is usable.
So why do you want to validate it in shell?
so the shell script can decide where to send or use the number. if some tool just behaves badly or unexpectedly with a non-number the script can choose an alternate action to take. i ran into such a thing a while back and decided to make such a tool for shell scripts to use. but, first, i wanted to get a perspective on how this kind of thing was commonly dealt with so i could make this tool better. for example, if people mention also doing hexadecimal tests, perhaps some other way, i could include that in my tool.
i agree that bash is a poor choice to implement this. my initial tool was intended to be in Python. this is such short running tool, i see no need to do it in C. if you need to do it so often that C speeds you up, you should do your development in C and avoid those extra syscalls for each test. one use for this, recently, was to validate parameters for a cloud submission. in the end, it might be better to have a Python script perform the entire submission and use bash to decide when, or if, to carry out such a submission.
i have already made a number of tools in Python for use in a bash script. i wanted to make this one more complete.
It strikes me that this would be a wonderful opportunity to write a simple utility for the detection. Python or PERL would be natural. Rust, Pascal, Modula (2 or 3), V-lang, GoLang, might serve. It could be done in C, C++, Smalltalk, or Java with a bit of work I think.
Just for clarification and the fact that I might of lead you astray.
From a math point of view XX versus XX.XX would be an integer versus decimal number. From purely a computer point of view XX would be a decimal number if it only contained numbers i.e 0-9. XX.XX is not a decimal because it contains a period(.). However, in the -ne test, the function fails if the number is 0 so that still does not work. So I might of been confusing...
the way i would implement "isdec" is that it will give a 0 exit status if the number can be converted by typical programming tools to a floating point number. this would allow one "." but not two of them. if the -i or --int option is used, only integers would be allowed, so no "." at all. a base of any value from 2 to 36 would be allowed by using -b= or --base= followed by the base. shortcut options would be -o and -x and --octal and --hex.
if just one number is given the exit status is 0 for a valid number and 1 for invalid. if -o is given or there are two or more numbers. it will output to stdout a '0' or '1',space separated on one line for the status or each number. if -r is given, the numbers are read from stdin until EOF, separated by any white space or any punctuation other that ".". it will use "," instead of "." where the locality changes the behavior of underlying tools (such as int() and float() in Python or strtol() and strtod() in C).
when you work with numbers you need to know what format is accepted by the tool you use.
Any tool will give a non-zero exit code for invalid numbers (which cannot be handled by that tool). Additionally they may print an error message about that situation. But implementing and maintaining another tool just to try to check if it will work is not that easy. (from my side it is completely pointless, if you want to check if it will be accepted by strod you need to call strod).
just to be sure my question is understood, it is about what tool would you use in bash to make decisions about what to do, based on whether a provided "number" actually is a valid number, or not. it is not about number conversion. it is not about implementing number processing in bash.
my plan is to implement many tools for bash scripting to make use of. right now,i'm looking at number testing tools. i want to discover what others might be using or expecting.
i am planning to implement most tools in Python3. but, C is not ruled out.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.