Check if a file exists with certain prefix

Hi guys,
How would i check a file exists with certainprefix? i have a directory with some files:
ABC1
ABC2
ABC3
etc..

and want to do:
please note i am using the korn shell environment.As when i gone through some stuff on then net i came to know some of the options will work differently based on the working shell environment.tht's wy i am specially mentioning my envrionment.

Code:
if [file exists with prefix ABC*] then
do something
else
do something else
fi

Any one could help me on this!!!!!

Advanced Thanks
Narasimharao.

use find command.. go through man page..try to do it your self

vidyadhar's solution is also a good way to go but if you want something in ksh:

#!/bin/ksh

if [ -f ABC* ]
then
	echo file exists.
else
	echo not
fi

Hi redoubtable,
the below code is working for only exact file names(i mean the -f option).could you please tell me the way for finding files with certain prefix.I know the find command.but i need another way by using simple if command and along with their options.

Hi can any one please help me on this..........

Advanced Thanks
Narasimharao.

The code I gave you works for filenames starting with "ABC".
ABC1, ABC2, ABC3.

redoubtable@Tsunami ~ $ ls -l ABC*
-rw-r--r-- 1 root root 0 2008-08-19 11:42 ABC1
-rw-r--r-- 1 root root 0 2008-08-19 11:42 ABC2
-rw-r--r-- 1 root root 0 2008-08-19 11:42 ABC3
redoubtable@Tsunami ~ $ 
redoubtable@Tsunami ~ $ ./s.ksh 
file exists.
redoubtable@Tsunami ~ $ cat s.ksh 
#!/bin/ksh

if [ -f ABC* ]
then
	echo file exists.
else
	echo not
fi
redoubtable@Tsunami ~ $ 

The '*' provides the capability of matching not only exact file names but also every file name starting with ABC. Remember that you must not use " in ABC.

Example:
This is ok:

if [ -f ABC* ]

This is wrong:

if [ -f "ABC*" ]

You can do something like this:

for file in *; do
  prefix=echo ${file:0:3}
  if [ "$prefix" = "ABC" ];  then
    # do something
  else
    # do something else
  fi
done

Regards

Hi Franklin52,
what i remember is the bash counts the number of the variable from 0, isn't it ?
so, I think that should be:

prefix=echo ${file:0:3}

Regards

You're right, thanks!

Regards

hi redoubtable,

                  Thanks for your information.now the code is working fine but  with if [ -a abc* ] not with  [-f abc* ] .i dont know wy -f option is not working.

Thanks
Narasimharao.

Hi Franklin52,
"prefix=echo ${file:0:3}" when i am placing this code in my korn shell environment it is throwing a warning by saying "The specified substitution is not valid for this command". i think it is specific to evironment.
Can you please tell me the how would i write the same syntax in a korn shell enviroment?

Advanced thanks!!!!
Narasimharao.

-f works only for ordinary files. -a works for any file. I'm glad you found a solution.

prefix=`echo $file|sed 's/\(...\).*/\1/'`

or:

prefix=`echo $file|awk '{print substr($0,1,3)}'`

Regards