LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2022, 06:56 PM   #31
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,760

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931

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.
 
Old 05-28-2022, 11:18 PM   #32
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,714

Rep: Reputation: 2722Reputation: 2722Reputation: 2722Reputation: 2722Reputation: 2722Reputation: 2722Reputation: 2722Reputation: 2722Reputation: 2722Reputation: 2722Reputation: 2722
Quote:
Originally Posted by Skaperen View Post
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.

Last edited by wpeckham; 05-28-2022 at 11:21 PM.
 
1 members found this post helpful.
Old 05-30-2022, 12:58 AM   #33
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,976

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
Quote:
Originally Posted by Skaperen View Post
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
 
1 members found this post helpful.
Old 05-30-2022, 01:53 PM   #34
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by michaelk View Post
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.
 
Old 05-30-2022, 02:00 PM   #35
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by wpeckham View Post
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.
 
Old 05-30-2022, 02:22 PM   #36
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by pan64 View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Arithmetic operation in bash script, multiply by decimal number, how? postcd Linux - General 5 06-28-2021 04:21 PM
4gig file trying to replace decimal number A with decimal number B tasdca Linux - Software 5 03-26-2015 05:51 PM
Number of bits (field in general) required to store decimal number srinietrx Programming 7 11-04-2014 07:13 AM
[SOLVED] BC provides incorrect decimal results, but only on the last two decimal places... standard_output Linux - Newbie 4 06-27-2012 05:30 PM
convert number (not hex) into Decimal number drManhattan Programming 10 10-15-2011 08:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration