Why I am getting a syntax error?

Posted on 2007-08-10
Last Modified: 2013-12-26
I have the following Korn Shell script code:


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
print 1

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.

Question by:soccerplayer
    LVL 58

    Accepted Solution

    Try changing

    if $a > 0 then


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

    Expert Comment

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

    if  [  $a  -gt  0  ]  then

    (spaces doubled-up for clarity)
    LVL 48

    Expert Comment

    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:

    if [ $# -ne 2 ]
         echo "Usage: $0 [dir] [file]" >&2
         exit 1


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
    Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    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.

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now