If statement is not working in KSH

#! /bin/ksh
 
rm -f ./xyz file --- this line is working 
 
// Below any if stmt is not working.
if[-f /var/log/messages ]
then
echo " blah blah "
fi

or I replaced above if with

if[-d ${dirName}/ dirname ]
then
      echo "dir exists"
fi

This is also not working.

I am new to KSH. So can someone help why if stmt is not working in KSH.
It's giving me error as

if[-f /var/log/messages ]: not found
syntax error: `then' unexpected

Use space around the square brackets: if [ -f ...

It is also better practice to use:

instead of:

Still it's not working after using space.
Its giving error as

if[ -f /var/log/messages ]: not found    #==> I did put one space at start and end of [ ]
syntax error: `then' unexpected

You are still not using enough spaces

if [[ -f /var/log/messages ]]
then
  ....
fi

Yes I tried to use the space

if[[ -f /var/log/messages ]]
then
        echo "This dir not exists."
fi

Error : 
if[[ -f /var/log/messages ]]: not found
syntax error: `then' unexpected

---------- Post updated at 09:57 AM ---------- Previous update was at 09:54 AM ----------

After using the 2 spaces still the same error. I dont know why its giving error for IF

if[[  -f /var/log/messages  ]]: not found
./rdaMakeWL92.sh[8]: syntax error: `then' unexpected

You need a space either side of [ - between the if and the first [.

ifSPACE[[SPACE ... SPACE]]

OMG ...It really funny now it's working just putting space after IF ... welcome to shell scripting .. :slight_smile:

if  [-d /var/log/messages]
then
        echo "This dir not exists."
fi

Thanks much for helping me in this.

Surely one space after the "if" and one space after the "[" and one space before the "]".

if [ -f /var/log/messages ]
then
        echo "This file exists"
fi

if [ -d /var/log ]
then
        echo "This directory exists
fi

if [ ! -d /var/log ]
then
        echo "This directory does not exist"
fi

For me - only for ! or negative condition I should have to put space after IF,"[" and "]" but for normal or posivitive condtion only one space after if working. Anyway it good practice to put space as mentioned by you. :slight_smile:

It's not a question of "good practice". It's a question of correct syntax.

And to the shell, IF is not the same as if :slight_smile:

On the contrary: [ ... ] is standard and portable; [[ ... ]] is not.

[] is necessary for glob-style matching comparison, and he specifically mentioned having ksh...

I find myself at a loss when it comes to testing code. Nobody ever made shells this terrible and old for Linux.

How about dash which is the standard shell (/bin/sh) on Ubuntu ? It is not old but it does not support []

It's nonstandard in many ways, the 'read' command doesn't work right in some situations. Besides, it's not a ksh variant. pdksh seems to be a ksh88 variant, but this guy's shell, whatever it is, might be even older yet..!

It is not necesssary for glob-style matching. A case statement works in all shells.

---------- Post updated at 01:13 PM ---------- Previous update was at 01:09 PM ----------

The dash shell is probably the most POSIX-compliant (i.e., the most standard) shell there is, with the fewest extensions.

In what situations does the read command not work correctly?

The [[ builtin is a kornism and bashism with better-defined semantics than [ (a.k.a. test). If you want your script to be portable as in work with all POSIX-conformant shell script interpreters, you should use the more limited [.

As regards dash and POSIX conformance, according to the current manpage for dash:

Unlike a number of other shells, I do not believe that dash has passed the VSC-PCTS2003 POSIX Certification Test Suite.
It is easy to claim POSIX conformance but a testsuite is the only valid indicator of conformance.

In ksh the bracket type is significant and important. It distinguishes between a "Test" and a "Conditional Expression". There is some common syntax but as soon as you get into boolean expressions they are quite different.

[[ conditional expression ]]
[ test ]

Please tell me what can be done with [[ ... ]] that cannot be done with [ ... ] or case.

ERE matching in modern bash/ksh93:

if [[ "a.3.578.yyy" =~ .\.[0-9]\..*y+ ]]; then
  echo hello
fi