Why I am getting a syntax error?

I have the following Korn Shell script code:

#!/bin/ksh

cd "$1"
if [[ -f $1$2 ]] then   #if file exists then get a count
     a=`wc -l < "$1$2"`
     if $a > 0 then
        print 0
        exit
     fi
fi
print 1
exit

This gives me the following syntax error:
FindFile.ksh[9]: syntax error: `fi' unexpected

I tried pretty much everything. I tried moving the inner IF LOOP after the first IF LOOP. I get an "else expected" error. I
would appreciate any help.

soccerplayerAsked:
Who is Participating?
 
amit_gCommented:
Try changing

if $a > 0 then

to

if [[ $a -gt 0 ]] then
0
 
Duncan RoeSoftware DeveloperCommented:
Agree except the square brackets shouldn't be doubled:

if  [  $a  -gt  0  ]  then

(spaces doubled-up for clarity)
0
 
TintinCommented:
Square brackets can be doubled if using the ksh (as in this case) built in test operator.

soccerplayer - you should always have a check in your script to ensure the correct number of args has been supplied, eg:

#!/bin/ksh
if [ $# -ne 2 ]
then
     echo "Usage: $0 [dir] [file]" >&2
     exit 1
fi

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.