ksh syntax error: `if' unmatched

12,081

Solution 1

I had the same error. Turned out it was due to DOS format newlines (CR-LF) in my *.sh file created in Windows and then transferred to a Linux server.

Commands to convert DOS format newlines (CR-LF) to UNIX format newlines (LF)

  • In Windows: using Notepad++, as explained here:

    • From the "Edit" menu, select "EOL Conversion" -> "UNIX/OSX Format".
    • You can also set the default EOL in notepad++ via "Settings" -> "Preferences" -> "New Document/Default Directory" then select "Unix/OSX" under the Format box.
  • In UNIX/Linux: using one of the techniques explained here:

    • Convert DOS to UNIX using sed command:

    sed 's/^M$//' input.txt > output.txt

    • Convert DOS to UNIX using tr command:

    tr -d '\r' < input.file > output.file

    • Convert DOS to UNIX using this Perl one-liner:

    perl -pi -e 's/\r\n/\n/g' input.file

    • Convert DOS to UNIX using dos2unix command:

    dos2unix myfile.txt or dos2unix -b myfile.txt (with a backup)


[ Bonus tip ]

Commands to convert UNIX format newlines (LF) to DOS format newlines (CR-LF)

  • Convert UNIX to DOS using unix2dos command:

unix2dos myfile.txt or unix2dos -b myfile.txt (with a backup)

  • Convert UNIX to DOS using sed command:

sed 's/$'"/`echo \\\n\\\r`/" input.txt > output.txt (you need those \\\, you do)

Solution 2

I am using following version

version         sh (AT&T Research) 93u+ 2012-08-01

I did not received any syntax error for your above code , though there a problem with your if statement condition instead of

if [[$0 = "-ksh"]]

it should be

 if [[ $0 == "-ksh" ]] 

or

if [[ $0 = "-ksh" ]] 

the latter is obsolete

The complete code is as below

#!/bin/ksh
# ksh example
if [[ $0 = "ksh" ]];
then
  bash
exit $?
fi

if [[ $0 == "-ksh" ]];
then
   bash --login
   exit $?
fi

export LOGIN=$LOGNAME

#prompt config
PS1="$LOGIN@"$(hostname)":$PWD"

if [[ "$(id -u)" == "0" ]];
then
export PS1="$PS1# "
else
   export PS1="$PS1> "
fi

#Alias utile
alias ll="ls -la"

#Set any export here
export PATH_EXAMPLE=/home/userTest
export JAVA_HOME=$PATH_EXAMPLE/games/java/current
export PATH=$JAVA_HOME/bin:$PATH

You script may be having some unwanted character , try to look out for then using cat -vte you can also try command dos2unix filename and then run ksh -n

Share:
12,081
ibaneight
Author by

ibaneight

Updated on June 04, 2022

Comments

  • ibaneight
    ibaneight almost 2 years

    I'm new in ksh world and I have a problem right now with a script. The script under this lines is into the .profile file of a user in a UNIX machine and when I try to connect whith him i get always the error

    home/userTest/.profile: syntax error: `if' unmatched

    I don't know how to solve this, because I suppose that this scripts defines the prompt for the connected user, and if I have this error the prompt only shows "$"

    I tried the command

    ksh -n /home/userTest/.profile

    and I get the error always in the last line of the file

    #!/bin/ksh
    # ksh example 
    if [[$0 = "ksh"]];
    then
      bash
      exit $?
    fi
    
    if [[$0 = "-ksh"]];
    then
      bash --login
      exit $?
    fi
    
    export LOGIN=$LOGNAME
    
    #prompt config
    PS1="$LOGIN@"$(hostname)":$PWD"
    
    if [["$(id -u)" = "0"]];
    then
      export PS1="$PS1# "
    else
      export PS1="$PS1> "
    fi
    
    #Alias utile
    alias ll="ls -la"
    
    #Set any export here
    export PATH_EXAMPLE=/home/userTest
    export JAVA_HOME=$PATH_EXAMPLE/games/java/current
    export PATH=$JAVA_HOME/bin:$PATH
    

    How can I solve this problem ?

    Thanks.

  • ibaneight
    ibaneight about 8 years
    I finally I get my problem solved. I was creating mi .ksh file from windows, and then uploading it to my unix machine. When I create the file from unix the problem is solved. Thanks
  • Ajay
    Ajay about 8 years
    if you had run dos2unix filename ... that would have solved your problem .... it replaces the ctrl M character with unix line end.