Continuous checking of a file

I have a requirement like this...

I want to go to a particular server for which i have acess .I want to do a ssh to that server from one server and check if a file is theer or not..and i need the script to chcek continuosly till it finds the file.When it finds the file i want it to come out of the loop ..please let me know..

#!/bin/ksh

while :
do
        if [[ $( ssh -n ${user}@${host} "ls filename 2> /dev/null " | wc -l ) -ne 0 ]]
        then
                echo "File found."
                break;
        else
                echo "File not found."
                sleep 10
        fi
done

hi Bipin,

thanks forthat.

I initially try to do it in my dev server without doing an ssh.

here is what I get..can you help?

./test_new.sh: line 4: ls /home/sendhabh/trial.done  2> /dev/null : No such file or directory
File not found.
-sh-3.2$ ls /home/sendhabh/trial.done
/home/sendhabh/trial.done
-sh-3.2$ wc -l ls /home/sendhabh/trial.done
wc: ls: No such file or directory
1 /home/sendhabh/trial.done
1 total

and this is my script:

-sh-3.2$ more test_new.sh
 #!/bin/ksh
while :
do
        if [[ $( "ls /home/sendhabh/trial.done  2> /dev/null " | wc -l ) -ne 0 ]]
        then
                echo "File found."
                break;
        else
                echo "File not found."
                sleep 10
        fi
done

You should not use double quotes here. Alternatively try

if [ -f /home/sendhabh/trial.done ]; then

That worked.Thanks bipin.

However for different server shall i remove quotes as well?

now my code looks like this:

-sh-3.2$ ./test_new.sh
File found.
-sh-3.2$ more test_new.sh
 #!/bin/bash
while :
do
        if [[ -f /home/sendhabh/trial.done ]]
        then
                echo "File found."
                break;
        else
                echo "File not found."
                sleep 10
        fi
done

---------- Post updated at 05:27 PM ---------- Previous update was at 04:43 PM ----------

I use this now..howeever i get rror as below:

-sh-3.2$ ./test_GMI_final.sh
ssh: : Name or service not known
File not found.
-sh-3.2$ more test_GMI_final.sh
#!/bin/ksh
DATEPREV=`date --date yesterday "+%d%m%Y"`
while :
do
        if [[ $( ssh -n etdmis@$xldn2218pap "ls /sbclocal/InternalSecureFileTransfer/users/ETDIT/gmi_coma/gmi_to_coma/US2COMA_
$DATEPREV.done
 2> /dev/null " | wc -l ) -ne 0 ]]
        then
                echo "File found."
                break;
        else
                echo "File not found."
                sleep 10
        fi
done

It may not be the wisest thing to do to continuously log in and out the remote system. Why don't you run the loop on the remote server and have it kick off the local action from there?

I use this now and it works..

#!/bin/bash
DATEPREV=`date --date yesterday "+%d%m%Y"`
ssh xldn2218pap << MSH
while :
do
if [ -f /sbclocal/InternalSecureFileTransfer/users/ETDIT/gmi_coma/gmi_to_coma/US2COMA_$DATEPREV.done ] ;
then
 echo "File found."
 break;
else
               echo "File not found."
                sleep 300
        fi
done
MSH

does it look good rudiC and bipin?

Pls use code tags as advised!
I don't see an obvious flaw in your script, but it might keep the ssh connection open for hours, which might work on LAN but might be risky for remote conenction across WAN.

Thanks,

I will take note of that.

Thanks everybody for your help.Its working fine now...

You can do that a lot more simply:

ssh ssh xldn2218pap "while [ ! -f /sbclocal/InternalSecureFileTransfer/users/ETDIT/gmi_coma/gmi_to_coma/US2COMA_$DATEPREV.done ] ; do sleep 300 ; done ; echo 'File found'"