|
|
Compare strings as integers
Hello,
I have 2 strings in a script (sh) that I want to compare. The two
strings are parsed from a version number.
num1=1
num2=2
I want to do the following:
if [ $num1 > $num2 ] ; then
But fails, how can I do this using the sh shell. Is there some
conversion routine.
Thanks,
Enda
|
|
0
|
|
|
|
Reply
|
keepyourstupidspam
|
4/22/2005 9:41:33 AM |
|
Enda Mannioooo wrote:
> I want to do the following:
> if [ $num1 > $num2 ] ; then
man test
You need to replace > by -gt.
By the way, it feels strange reeding a shell script with more $ than ".
|
|
0
|
|
|
|
Reply
|
Marc
|
4/22/2005 9:54:36 AM
|
|
Hi,
Thanks for replying but the operator -dt does not solve this for me
because I am working with strings not integers, integers are stored in
the strings.
doing:
imag=2
kmaj=3
if [ $imaj -gt $kmaj ] ; then
exit 1
elif [ $imaj -lt $kmaj ] ; then
echo "works"
fi
gives me:
../test.sh: [: -gt: unary operator expected
../test.sh: [: -lt: unary operator expected
|
|
0
|
|
|
|
Reply
|
keepyourstupidspam
|
4/22/2005 12:57:46 PM
|
|
keepyourstupidspam@yahoo.co.uk wrote:
> imag=2
^
> kmaj=3
>
> if [ $imaj -gt $kmaj ] ; then
^
I believe this is the reason.
|
|
0
|
|
|
|
Reply
|
Marc
|
4/22/2005 1:12:01 PM
|
|
In article <1114174665.930810.296940@l41g2000cwc.googlegroups.com>,
keepyourstupidspam@yahoo.co.uk wrote:
> Hi,
>
> Thanks for replying but the operator -dt does not solve this for me
You misspelled -gt
> because I am working with strings not integers, integers are stored in
> the strings.
>
> doing:
>
> imag=2
You misspelled "imaj".
I think you need to get some more sleep, your fingers are fumbling like
crazy.
> kmaj=3
>
> if [ $imaj -gt $kmaj ] ; then
> exit 1
> elif [ $imaj -lt $kmaj ] ; then
> echo "works"
> fi
>
>
> gives me:
>
> ./test.sh: [: -gt: unary operator expected
> ./test.sh: [: -lt: unary operator expected
If you'd run your script with the -x option you would have seen that the
problem was that the variable wasn't being expanded as you expected.
Can't people do the least bit of debugging before posting? You're a
programmer, act like it!
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
|
|
0
|
|
|
|
Reply
|
Barry
|
4/22/2005 1:29:14 PM
|
|
|
4 Replies
516 Views
(page loaded in 0.174 seconds)
Similiar Articles: Compare strings as integers - comp.unix.solarisHello, I have 2 strings in a script (sh) that I want to compare. The two strings are parsed from a version number. num1=1 num2=2 I want to do the f... compare alphanumeric number with integer problem - comp.lang ...Compare strings as integers - comp.unix.solaris compare alphanumeric number with integer problem - comp.lang ..... Basic :: Getting Only The Integer From An ... To combine string and integer in F90 - comp.lang.fortran ...compare alphanumeric number with integer problem - comp.lang ... Compare strings as integers - comp.unix.solaris compare ... whether a scalar is a number/whole/integer ... JDBC compare the content of two tables - comp.lang.java.help ...Compare strings as integers - comp.unix.solaris JDBC compare the content of two tables - comp.lang.java.help ... Compare strings as integers - comp.unix.solaris JDBC ... integer overflow in atoi - comp.lang.cI want to implement atoi function which converts string to an integer. So I did this ... So, yes, a correct version will need to compare strings only of they have exactly ... Sum of all even integers - comp.lang.java.helpCompare strings as integers - comp.unix.solaris Sum of all even integers - comp.lang.java.help Compare strings as integers - comp.unix.solaris Sum of all even integers ... display integer in MessageBox - comp.lang.c++.moderatedCompare strings as integers - comp.unix.solaris display integer in MessageBox - comp.lang.c++.moderated It means "Long Pointer to Const Wide-character STRing". Compare numbers - comp.soft-sys.matlabCompare strings as integers - comp.unix.solaris Hello, I have 2 strings in a script (sh) that I want to compare. The two strings are parsed from a version number. num1=1 ... ps "pauses" when displaying zoneconsole - comp.unix.solaris ...Hello. Since "lately", I'm having a problem with "ps" on a S10 U5 ... Compare strings as integers 4 195 keepyourstupidspam counting the digits in a number, exponential format problem - comp ...Compare: <?php var_dump( pow(101, 40) ); var_dump( bcpow('101', '40 ... convert integer to string - comp.lang.perl.misc I thought, "No problem, I'll just create a ... Visual Basic :: Comparing Strings As IntegersComparing Strings As Integers I'm going to be alphabetizing a string array. It would really help if I could take a string and turn it into a number so that I could ... Compare strings as integers - comp.unix.solaris | Computer GroupHello, I have 2 strings in a script (sh) that I want to compare. The two strings are parsed from a version number. num1=1 num2=2 I want to do the f... 7/26/2012 3:07:44 AM
|
|
|
|
|
|
|
|
|