File Exists

Hi,

I have scenerio where i need to check 3 files whether they are of current day files and then count should not be equal to zero.

how to achieve the same please let me know.

Vishal

Do have specific file names or file name patterns or could the three files have any name?

there are 3 different files and for one of the file i need to check count also if count is less then 0 i have to rerun the process

You're not making yourself very clear, is there a count (number) inside one of the files or do you you mean how many lines or bytes are stored in one of the files?

Let me explain you the full scenerio. For e.g i have 3 files created by informatica process and stored in Unix server and files are

  1. a.txt
    2 b.txt
    3 c.txt

Now before i start my next process i need to make sure these files exists in folder and only b.txt has records in it i mean there should be some records inside the file or file should not be empty.

If the b.txt file is empty then i need to runmy informatica process again and make sure b.txt has data in it.

once b.txt has data then i go to next process.

overall i am checking two things here one is files are loaded today only today not yesterday and one of the file has data in it.

Okay, so you could do something like:

if [ -f a.txt -a c.txt -a ! -s a.txt -a -s b.txt -a ! -s c.txt ]; then
  # All three files exist, a.txt and c.txt are empty and b.txt is not empty
  echo ok
else
  # Rerun informatica process
  echo not ok
fi

Hi,

thanks for your reply, but i am confused could you please eleborate more.

Vishal

The manpage for "if" is called test(1), in the manpage you will see that -f switch tests that the file exists and that the -s switch tests for a file existing and being bigger than zero bytes in size, the ! before a switch inverts its meaning.

Hi,

I am getting an error 0403-021 charater Missing.

Vishal

If your shell is posix compatible, then syntax is correct.
posix-sh, ksh, bash, bourne-shell sh, zsh, ash, dash, ...

echo $0

Please post your script which gave the error.

My interpretation of the problem is as as follows. This incorporates a subtle syntax change in the if ... and ... and ... .

if [ -f a.txt -a -f b.txt -a -f c.txt ]
then
        if [ ! -s b.txt ]
        then
                # All 3 files exist but b.txt is empty
                echo "Rerun informatica process"
                exit
        else
                # All 3 files exist and b.txt is not empty
                # Continue process
                :
        fi
else
        # One or more files is missing presumably an error
        echo "One of more files is missing"
        exit
fi
# Normal process starts here

Hi,

thank you ver much script is working now, one more question i need to make sure all the files is of today's date and time stamp.

any workaround for that.

Vishal

here is another way

#!/bin/bash

# files we need to verify if they exist

ExistPath=("/path/to/file1.txt
                  /path/to/file2.txt
                  /path/to/file3.txt

")

for file in $ExistPath ; do

   if [[ -e $file ]]

   then /bin/echo "$file exists"

   else /bin/echo "$file does not exist"

    #now touch the file to update the time/date modified

   /usr/bin/touch $file
   
   fi

done

exit 0

I am not sure if the touch will still go through the loop as coded. I think closing out the if may kill the loop, but not sure.

Thanks for the code.But i wanted to check if the file which exists in the folder has the timestamp of todays date, and if so has data in it which is greater than 0 so that i could go forward and process the file through the informatica process.Could you help me out with this.

Thanks

Please post which Operating System and version you have. This is so we can work out which variant of the "find" command you have.

HI,

Just wanted to know can we return error codes out of shell scirpt.i mean if file found that return 0 or 1 and if doesnot found then some number.

Right now but is happening is if i print file exist then my informatica will not recognize that.

Please help me on this.

Using exit.
Script exit using some exit value, usually 0 = ok, <> 0 is not ok.
After exit you can look the value of variable ?

#somecmd script
exit 5
somecmd
echo "exit status $?"

@shady4u

Without the base information on your environment we cannot script the "today" bit. There is too much variation in the "find" command.

This should get you started.

# Create a file timestamped first thing today
touch -t `date +%Y%m%d0000` timestamp

Then you need to use "find" with the "-newer" option to find out whether the file exists and was created today.