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.
I understand ( I think... ), as I have stated before bash variables are not typed and basically are treated as strings unless used in a particular test or function. I am not worried about speed and use regex or some kind of string matching if I need something specific.
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.
Perhaps, but one of the easiest ways to test is to use a conversion tool to convert the string between number formats and back, and compare the results to the original. If they match, it was a number format that was valid and properly understood, if they differ then it was either not a valid number or was not properly formatted.
There are multiple reasons for using a number conversion tool, a couple are: #1 bash does not understand or handle floating point numbers, number conversion utilities can and do. #2 number conversions tent to either be very unforgiving or terribly forgiving: doing the double conversion with compare bypasses most false positives and false negatives.
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.
I think I understand that. But the answer is still the same: bash has limitations, and cannot handle numbers very well. Validity is always defined by the tool you use and different tools may use different syntax. So yo need to implement an imperfect parser (see for example here: https://en.wikipedia.org/wiki/Decimal_separator).
Someone posted here, at LQ a full calculator written in pure bash (posix shell). So it is not impossible and also you may say it is an interesting exercise, but actually it will be extremely inefficient.
Here is the link to that thread: https://www.linuxquestions.org/quest...ts-4175703227/
Last edited by pan64; 05-30-2022 at 11:04 AM.
Reason: typo
I understand ( I think... ), as I have stated before bash variables are not typed and basically are treated as strings unless used in a particular test or function. I am not worried about speed and use regex or some kind of string matching if I need something specific.
regex is a good way to do that if you understand regex well. i don't. when i need something that is not doable in bash, now days i usually do it in Python. i used to do it in C. if i can't do it in Python, i fall back to C. i define "can't do it" to include things like "not practical", "unmaintainable", and so on.
i also like to encapsulate these things down to simple code to use it. that's why i like "isdec". but that does not restrict how to implement it. if you know good regex you can make use of in bash, i think you should do it and make a bash function to use so regex strings do not need to be coded in so many places.
a lot can be done in bash with its string-only variables and limited number processing. i have done many things in bash over the years.
Perhaps, but one of the easiest ways to test is to use a conversion tool to convert the string between number formats and back, and compare the results to the original. If they match, it was a number format that was valid and properly understood, if they differ then it was either not a valid number or was not properly formatted.
There are multiple reasons for using a number conversion tool, a couple are: #1 bash does not understand or handle floating point numbers, number conversion utilities can and do. #2 number conversions tent to either be very unforgiving or terribly forgiving: doing the double conversion with compare bypasses most false positives and false negatives.
my opposition to number conversion is only in the API design. doing the conversion and comparing the results to the original is a good way do to an implementation. i am only looking at how users could make use of it, discouraging them to re-implement it, or satisfying their desired to not re-implement it.
I think I understand that. But the answer is still the same: bash has limitations, and cannot handle numbers very well. Validity is always defined by the tool you use and different tools may use different syntax. So yo need to implement an imperfect parser (see for example here: https://en.wikipedia.org/wiki/Decimal_separator).
Someone posted here, at LQ a full calculator written in pure bash (posix shell). So it is not impossible and also you may say it is an interesting exercise, but actually it will be extremely inefficient.
Here is the link to that thread: https://www.linuxquestions.org/quest...ts-4175703227/
i would imagine such a calculator to be rather inefficient. i did make my own command line calculator based on Python. it just evaluates a Python expression. it works for me as a Python coder. if Python can't do it, this tool can't do it. while, in theory, i could do encryption with such a tool, i would not even try. it would be way too big. it would not be well tested, which encryption critically needs. but if the calculator is a device emulator style, the slowness would be working the slowness of use, and the user may be forgiving, to a point.
i have done a few big things in bash, often just to see if i could. it can do a lot of "impossible" things ... at the expense of efficiency. OTOH, if you have a case to implement where only bash is available, that might be the only way to get it done by the end of the week. but i'm asking about how one might use tools in bash, not how to implement it in bash.
IMHO, i am confident that i could design a "machine" that could be implemented in bash (inefficiently). then that could be the basis of other inefficient implementations. so nothing is impossible. it would just be inefficient.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.