integer comparison in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting integer comparison in ksh
# 1  
Old 07-20-2011
integer comparison in ksh

Hi,

I am just trying to compare integer in ksh. can you please tell me what's wrong with this code... or give me suggestions on alternative.

sample code:

i=0;

if [ i -eq 0 ]; then
echo inside if
fi


Thanks in advance!
# 2  
Old 07-20-2011
Try with below
Code:
i=0;
if [ $i -eq 0 ]; then
echo inside if
fi

In case you want to debug your script put below code at the very beginning

Code:
set -x

Cheers
Harish
This User Gave Thanks to harish612 For This Post:
# 3  
Old 07-20-2011
Quote:
Originally Posted by nram_krishna@ya
Hi,

I am just trying to compare integer in ksh. can you please tell me what's wrong with this code... or give me suggestions on alternative.

sample code:

i=0;

if [ i -eq 0 ]; then #should be $i
echo inside if
fi


Thanks in advance!
# 4  
Old 07-20-2011
Suggestion

For ksh, use the built-ins to save some processes and thus efficiency ('[' is an external program (test) and echo is also an external program, while '((' is built-in for math and print is built-in). While this example will not save much time, in a larger script or more complex script your savings will be significant:
Code:
#!/bin/ksh  

integer i=0;

if (( $i == 0 )); then
  print "inside if"
fi

# 5  
Old 07-20-2011
(( )) is the best method - my opinion. You can use variable without $, about like in C.
Code:
y=100
(( a=y+2 ))
(( b=a+3 ))
if (( b < a )) ; then
   echo "b<a"
fi
(( b < a )) && echo "b<a" || echo "a<=b"
(( a= (b+100)*12 ))
# floating (LANG ... =>  , or . delimeter)
typeset -F2  l1 l2 l3 
l1=1.22 
l2=2.22 
(( l3=l1*l2)) 
echo $l3

Ofcourse you can use test command, but (( )) is nicer for numbers.
# 6  
Old 07-20-2011
Quote:
'[' is an external program (test) and echo is also an external program
Not in any modern version of ksh. These have been builtins for a long time. Use the builtins command to see the default list of builtins in ksh.
# 7  
Old 07-21-2011
Yes, old bourne shell not included [, echo, true, false, ... but long time in many shells has been builtin echo, test, true, false, ... = posix shells.
So [, test, [[, ((, echo, print, printf, true, false, :, .. are builtin.
=> sometimes works little different, options are not always same, and so on. Some has done
just like posix has written, some has addon options.

In history we used
:
in forever loops, but today you can use true, it's not anymore external and so on.

Awk, sed, cut, tr, grep, expr, ... are still externals.

Ex. in ksh you can use command builtin to look ksh builtin. Also ksh93 include some builtin manuals. No need to read ksh manual.
Code:
read --man
typeset --man
set --man


Last edited by kshji; 07-21-2011 at 03:21 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] String integer comparison

I am trying to execute something like this file=/tmp/test.txt firstline=$(head -n 1 $file) value=`echo $firstline | cut -d'=' -f2` if then echo true fi i read the first line of a file, cut to the numeric value in the first line and check if it greater than 2 but for some... (11 Replies)
Discussion started by: madhan_dc
11 Replies

2. Shell Programming and Scripting

Doubt in integer comparison

Hi What is the difference between following commands Command1 length=1 if ] ; then echo "Hellow world" fi Command 2 length=1 if ] ; then echo "Hellow world" fi which is correct usage from this (2 Replies)
Discussion started by: morbid_angel
2 Replies

3. UNIX for Dummies Questions & Answers

Counting vowels in string. "Comparison pointer-integer".

I'm trying to write a programme which scans strings to find how many vowels they contain. I get an error saying that I'm trying to compare a pointer and an integer inif(*v == scanme){. How can I overcome this ? Also, the programme seems to scan only the first word of a string e.g.: if I type "abc... (1 Reply)
Discussion started by: fakuse
1 Replies

4. Shell Programming and Scripting

Integer in conditional ksh statement

Hi, This is confusing me and I would appreciate some help. Consider this script #!/bin/ksh typset -i stat=`ls -l $WORK/stat |wc -l` if (( $stat < 20 )) then echo true else echo false fi If the value of $stat is say 29. When I run this above it echos true and not false.... (7 Replies)
Discussion started by: giles.cardew
7 Replies

5. Programming

warning: comparison between pointer and integer

Hi guys :D I am still playing with my C handbook and yes, as you can see I have small problem as always :cool: I wrote a C code #include <stdio.h> #define MESSAGE 100 int main(void) { char input_mes - Pastebin.com And when I try to compile it I get following errors from gcc ... (1 Reply)
Discussion started by: solaris_user
1 Replies

6. Shell Programming and Scripting

Date comparison using ksh

Hi All, i have a text sample below. rootdbs 1 0 01/03/2010.03:11 physdbs 2 0 01/03/2010.03:17 logdbs01 3 0 01/03/2010.03:17 logdbs02 4 0 01/03/2010.03:17 dbs01 5 0 01/03/2010.03:17 dbs02 6 0 01/03/2010.03:17 dbs03 7 0 01/03/2010.03:17 dbs04 ... (4 Replies)
Discussion started by: informix2009
4 Replies

7. Programming

comparison between pointer and integer

I received a warning when I tried to compile my program that said: warning: comparison between pointer and integer Could you please explain to me what this means, and in what ways I could possibly fix this? Thanks for your help! (2 Replies)
Discussion started by: sjung10
2 Replies

8. AIX

Problem in ksh script ( String comparison )

hi , i am trying to compre two strings if ] or if ] when the length of var1 is small (around 300-400 char ) it works fine but when it is large (around 900-1000 chars) it fails is there any limitations for this type of comparison ??? (1 Reply)
Discussion started by: amarnath
1 Replies

9. Shell Programming and Scripting

Problem in ksh script ( String comparison )

hi , i am trying to compre two strings if ] or if ] when the length of var1 is small (around 300-400 char ) it works fine but when it is large (around 900-1000 chars) it fails is there any limitations for this type of comparison ??? (3 Replies)
Discussion started by: amarnath
3 Replies

10. Shell Programming and Scripting

KSH arithmatic Integer overflow

Guys, I have two big numbers to multiply. In doing do I am getting integer overflow. I managed to multiply number but this number is useless as KSh does not recognise it as a valid number. Here is what I am doing $ expr 999999999999 \* 100 276447132 I got the right value by doing... (2 Replies)
Discussion started by: vikas_sri
2 Replies
Login or Register to Ask a Question