syntax of if condition in ksh is wrong


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting syntax of if condition in ksh is wrong
# 1  
Old 08-24-2011
syntax of if condition in ksh is wrong

The syntax of 'if' conditionals in bash and ksh seems different. I am trying to check for a particular version using 'if' in ksh. This very simple syntax gives syntax error. I have tried many variants, but no go. Please correct the syntax. Later I will expand it to 'if' and 'else'.

Code:
#!/bin/ksh
version=$(head -1 /home/final_reports/version)
echo "version is $version" #prints 2.2
if [[ $version -ge 2.1 ]]; then #gives syntax error
echo "version is correct" 
fi

Thanks in advance.
# 2  
Old 08-24-2011
You can't do real number comparisons. -ge is for integers.
# 3  
Old 08-24-2011
try with >= (instead of ge)

-ge, -lt, -eq...etc... are for integer comparison
# 4  
Old 08-24-2011
AFAIK, you'll have to do this (in ksh 88):

Code:
#!/bin/ksh
version_major=2
version_minor=2
echo "version is $version_major.${version_minor}"
if [[ $version_major -ge 2 && $version_minor -ge 1  ]]·
then·
   echo "version is correct"·
fi

# 5  
Old 08-25-2011
Thank you purdym and itkamaraj for your quick help.

I used -ge and it is working. But why is that the simple syntax of if-else not working?

Code:
#!/bin/ksh
version=$(head -1 /home/final_reports/version)
echo "version is $version" #prints 2.2
if [[ $version -le 2.1 ]]; then 
echo "version is correct"
else
echo "version is wrong"
fi

I have changed "ge" to "le". So this should print "version is wrong". But it still prints "version is correct".

Please let me know what is wrong in the above syntax.

Where can I learn ksh scripting better?. I got many sites, but the examples given in them are very simple and easy ones. So it does not help when doing real world tasks.

Thanks in advance.
# 6  
Old 08-25-2011
If you are using a non legacy ksh release, this should work
Code:
#!/bin/ksh
version=$(head -1 /home/final_reports/version)
echo "version is $version" #prints 2.2
if [[ $(( version > 2.1 )) == 1 ]]
then 
     echo "version is correct"
else
     echo "version is wrong"
fi

# 7  
Old 08-26-2011
No need to do
Code:
if [[ $(( version > 2.1 )) == 1 ]]

in ksh93

This works just as well and is a cleaner syntax
Code:
if (( version > 2.1 ))
then
     echo "version is correct"
else
     echo "version is wrong"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If condition giving syntax error. Need help.

Hi, I have an if condition on executing it is giving syntax error as below: -------------------------------------------------------------------------------------- line 61: syntax error in conditional expression ./play_test.sh: line 61: syntax error near `]' ./play_test.sh: line 61: ` if... (2 Replies)
Discussion started by: ramki067
2 Replies

2. UNIX for Dummies Questions & Answers

[Solved] ksh script - can't figure out what's wrong

Hi! (I guess this could of gone into the scripting forum, but Unix for Dummies seemed more appropriate. Please note that I am not in school, so Homework doesn't seem appropriate either. You guys can let me know if you think otherwise.) I am following an exercise in a book on ksh scripting which... (2 Replies)
Discussion started by: sudon't
2 Replies

3. Shell Programming and Scripting

What's wrong with this condition

if && ] then + : test: ] missing Thanks in advance :) (8 Replies)
Discussion started by: ezee
8 Replies

4. Shell Programming and Scripting

What's wrong with this condition

if Please indicate the problem with this code Thanks in advance :) (4 Replies)
Discussion started by: ezee
4 Replies

5. Shell Programming and Scripting

Help with ksh using OR condition

I'm having an issue using OR condition, the script seems to work just fine but gives an error message while executing as can be seen in BOLD RED below: Code: #cat test.sh #!/bin/ksh set -x if || > /dev/null 2>&1;then echo "Fibre adapter found" else echo "No fibre... (10 Replies)
Discussion started by: mbak
10 Replies

6. Shell Programming and Scripting

Help with IF Condition Syntax

Hi. I expect the following unix script command to return 8: ls -ltr dropez* | grep -c dropez I can't seem to find the correct syntax (borne shell), can anyone help be to write an IF condition something like this: IF (ls -ltr dropez* | grep -c dropez) = 8 THEN ...do stuff ELSE ...do... (4 Replies)
Discussion started by: buechler66
4 Replies

7. Shell Programming and Scripting

Whats wrong with this IF condition?

I have four variables, dumpdata, defndata, compare1 and compare2 I want an IF statement condition which returns true when either dumpdata=defndata or (dumpdata<=compare1 and dumpdata>=compare2). But this is not working for me.. if (4 Replies)
Discussion started by: indianjassi
4 Replies

8. Shell Programming and Scripting

Whats wrong with the syntax

Whats wrong with below logic or syntax???? (4 Replies)
Discussion started by: dsravan
4 Replies

9. Shell Programming and Scripting

. $startApache wrong syntax?

Hi All, why doesn't this code start apache at boot? Cheers. coolboarderguy... startApache=/usr/local/apache2/bin/apachectl start if ; then . $startApache fi (5 Replies)
Discussion started by: coolboarderguy
5 Replies

10. Shell Programming and Scripting

What was wrong ??? ksh

I wrote this code and did not know what did I do wrong ?? ## get the first line of this file AFIRST=$(head -1 rss-cs-3423a-20051211-060001.dat) #CJKA|2005-12-10 08.01.30.000000|1111111111|ECI|1112221111|1113331111|1114441111 <--- this is a answer ## Third line of stat file BTHREE=$(cat... (11 Replies)
Discussion started by: sabercats
11 Replies
Login or Register to Ask a Question