Actually, my point was not about anything that uses validation. That whole thing was a trip down a rabbit hole.
The OP insisted that there was no way that a variable could be treated as "polymorphic", I showed two shell examples to the contrary. i.e., 01 and 1 would register as 1 in
Code:
[ $myvar -eq 1 ]
The only conclusion was that OP was using an ancient shell, so I suggested typeset. Then we all went down a rabbit hole. We are still there.
To answer your question in the OP's context let's see what ksh88 on Solaris 10 "thinks":
Code:
for i in "-1" "+1" "01" "1" " 1"
do
printf "%%s=%s: %%d=:%d: " $i $i
[ $i -eq 1 ] && echo "Numeric and equals 1"
[ $i -eq -1 ] && echo "Numeric and equals -1"
[ $i -eq 0 ] && echo "probably not numeric"
done
output:
Code:
%s=-1: %d=:-1: Numeric and equals -1
%s=+1: %d=:1: Numeric and equals 1
%s=01: %d=:1: Numeric and equals 1
%s=1: %d=:1: Numeric and equals 1
%s=1: %d=:1: Numeric and equals 1
Now let's compare that to bash and bash plus RE - note change to RE
Code:
#!/bin/bash
for i in "-1" "+1" "01" "1" " 1" "-000001"
do
printf "%%s=:%s: %%d=:%d: " $i $i
[ $i -eq 1 ] && echo "Numeric and equals 1"
[ $i -eq -1 ] && echo "Numeric and equals -1"
[ $i -eq 0 ] && echo "probably not numeric"
done
echo 'using RE'
regex='^([+-]{0,1}[0-9]+$| [0-9]+$|[0-9]+$)'
for i in "-1" "+1" "01" "1" " 1" "-0000001"
do
printf "%%s=:%s: %%d=:%d: " $i $i
if [[ $i =~ $regex ]] ; then
echo 'tests numeric '
else
echo 'tests not numeric'
fi
done
output:
Code:
%s=:-1: %d=:-1: Numeric and equals -1
%s=:+1: %d=:1: Numeric and equals 1
%s=:01: %d=:1: Numeric and equals 1
%s=:1: %d=:1: Numeric and equals 1
%s=:1: %d=:1: Numeric and equals 1
%s=:-000001: %d=:-1: Numeric and equals -1
using RE
%s=:-1: %d=:-1: tests numeric
%s=:+1: %d=:1: tests numeric
%s=:01: %d=:1: tests numeric
%s=:1: %d=:1: tests numeric
%s=:1: %d=:1: tests numeric
%s=:-0000001: %d=:-1: tests numeric
Hmm. ksh88 is the oldest shell I have. So none of the other stuff is really needed to do what the OP appears to have wanted, IMO. This was my original point. Your point is that signed numbers fail the RE in my example, which is true. My bad. Fixed.
All of this might have been over the OP's level of understanding anyway.
This User Gave Thanks to jim mcnamara For This Post:
I want to make a script to read a list of ip addresses from a file then convert those ip addresses to ip number.
ip number is made by removing the dots then transfer to a number , so the ip number transfered to binary then to decimal which will represents the ip number 0 : 4294967295
Also I... (17 Replies)
hi ,
i need a script to convert number into hexadecimal base
for example: 237=>ED
it s very important for me thank you in advance for you help (5 Replies)
Hi,
I am using awk to get particular dates in seconds and the output am getting is like 1.28071e+09.
How can I convert it to number format.
Can anyone help me out?
Thanks in advance..! (7 Replies)
I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as
name1:name2:name3:1
when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios.
Cat test
Nov 10, 2012 5:17:53 AM
INFO: Request Type
Line 1.... (5 Replies)
Hi gurus,
I have a weird requirement. I need to convert the number to english lecture.
I have 1.2 ....19 numbers
I need to convert to first second third fourth, fifth, sixth...
Is there any way convert it using unix command?
thanks in advance. (8 Replies)
friends
as I can convert this value to number
in this example it is well but can vary the value does not help me cut from a nesecito espefifica opsiocn get zero
CantCabe = 00023
Cant = 23
CantPntCabe = 0000000000000034
CantPnt = 34
if && ; then
echo... (3 Replies)
I have data as below
"ROWS merge process complete. thousand rows changed"
I need to get a variable assigned the value of 1000. I mean convert the string thousand to 1000.
Any help or pointer.
Please use CODE tags as required by forum rules! (6 Replies)
Hello Guys,
I have a floating point number 1.14475E+15 I want to convert this number in to full number (Integer or Big integer). I tried couple of functions it did not work. When I use INT=${FLOAT/.*} I am getting value as 1. I don't want a truncated value
#!/bin/bash
#... (9 Replies)