To check if file exists

Hi,
I have the below code written. However I am not getting the desired output

I am checking if the particular path has file in it.

#!/bin/bash
      ls -l /IRS2/IRS2_ODI/INFILE/*LS* 1>/dev/null 2>/dev/null
   if [ $# ==  0 ]
    then
     echo $?
     echo "File Exists"
   fi

There is no file in this path. But still it gets inside the if clause and says "File Exists".

Could you help me on this?

Hi,

Her is a quick and dirty one, amend as required.

if [ -f "filename" ]; then
                 printf "\n File Present \n\n"
            else
                 printf "\n File Not Present $ERROR \n\n"
            fi

In the code above, you will probably have to change the printf statement for an echo -e statement.

Regards

Gull04

This doesn't check for the previous command's exit code, but for the count of positional parameters. When calling the script without arguments, you'll branch into the then construct. Try if [ $? == 0 ] instead, but be aware that $? is being overwritten by every single new command...

I guess the title should say "To check if file exists".
Please also try a file_exists function.