LinuxQuestions.org
Review your favorite Linux distribution.
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-17-2022, 06:12 PM   #16
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

how about this?

Code:
isdec(){ test "$1" -ne 0 2>/dev/null;}
then bash code can check if $foo is decimal or not like:
Code:
...
if isdec "$foo"; then
    echo "is OK decimal"
else
    echo "is NOT decimal, aborting!"
    exit 1
fi
...
 
Old 05-17-2022, 07:41 PM   #17
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,760

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
Your trying to use the fact that -ne is only valid for integers?

A decimal number is defined as having a decimal part i.e 0.541

Last edited by michaelk; 05-17-2022 at 08:00 PM.
 
Old 05-18-2022, 09:24 PM   #18
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
good point. do you have a suggestion for a fix?
 
Old 05-19-2022, 12:34 AM   #19
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
yes, there is a very good thread about it. Probably you have found it already:
https://www.linuxquestions.org/quest...er-4175711309/
 
Old 05-19-2022, 08:40 AM   #20
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,760

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
Isn't your posted link this very thread...

Code:
        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.
 
Old 05-19-2022, 09:13 AM   #21
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 michaelk View Post
Isn't your posted link this very thread...
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 View Post
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?
 
Old 05-20-2022, 01:40 AM   #22
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752
I think trying to do it in bash would certainly be very educational in teaching you a lot about numbers & tests.

Otoh, if this was for some serious use later, I'd definitely use a more sophisticated language (in my case Perl).
In fact I just checked & it's Chapter 2 https://www.geos.ed.ac.uk/~bmg/softw...l.Cookbook.pdf
 
Old 05-20-2022, 01:30 PM   #23
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
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.
 
Old 05-20-2022, 02:40 PM   #24
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
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.
 
Old 05-21-2022, 05:28 PM   #25
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
of those, i only know Python and C. so that limits me. in C, i would also need to code up a Makefile and maybe a README.
 
Old 05-21-2022, 06:20 PM   #26
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,760

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
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...
 
Old 05-21-2022, 10:32 PM   #27
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
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).
 
Old 05-22-2022, 03:06 AM   #28
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
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).
 
1 members found this post helpful.
Old 05-22-2022, 06:34 PM   #29
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,760

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
It's an interesting exercise of sorts regardless.

I cobbled together a c program.
Attached Files
File Type: txt ischk1.txt (2.8 KB, 10 views)

Last edited by michaelk; 05-22-2022 at 06:36 PM.
 
Old 05-28-2022, 06:13 PM   #30
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
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.
 
  


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:42 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