2

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
  • 1,335
  • 2
  • 11
  • 15
  • I did not received any error when I have copied you code as it is and run a check with "ksh -n" . which ksh you are using ksh88 or ksh93 – Ajay Feb 26 '16 at 10:45
  • If a use the ksh --version command i get "version sh (AT&T Research) 93t+ 2010-06-21", so I guess that i have ksh93 – ibaneight Feb 26 '16 at 10:49
  • There's no need to check if the user is running as root. Put `\$` in PS1, and it will be rendered as $ or # depending on whether or not the user is root. – William Pursell May 07 '19 at 22:00

2 Answers2

6

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)

KiriSakow
  • 957
  • 1
  • 12
  • 22
1

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

Ajay
  • 775
  • 5
  • 19
  • 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 – ibaneight Feb 26 '16 at 15:51
  • if you had run dos2unix filename ... that would have solved your problem .... it replaces the ctrl M character with unix line end. – Ajay Feb 26 '16 at 15:53