?
Solved

Why I am getting a syntax error?

Posted on 2007-08-10
3
Medium Priority
?
1,568 Views
Last Modified: 2013-12-26
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.

0
Comment
Question by:soccerplayer
3 Comments
 
LVL 58

Accepted Solution

by:
amit_g earned 460 total points
ID: 19674089
Try changing

if $a > 0 then

to

if [[ $a -gt 0 ]] then
0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 19674522
Agree except the square brackets shouldn't be doubled:

if  [  $a  -gt  0  ]  then

(spaces doubled-up for clarity)
0
 
LVL 48

Expert Comment

by:Tintin
ID: 19680368
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question