nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Check if remote files exist (periodically)

This is a discussion on Check if remote files exist (periodically) within the Shell scripting forums, part of the Development/Scripting category; Hi all, I am trying with next code to check file existence from time to time Code: while [ 1 ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 30-01-2009, 03:27 AM
Junior Member
User
 
Join Date: Jan 2009
OS: Debian
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
pixelboss is on a distinguished road
Default Check if remote files exist (periodically)

Hi all,

I am trying with next code to check file existence from time to time

Code:
while [ 1 ] 
   do
      if [ ssh ${REMOTE_USER}@${REMOTE_HOST} 'ls "'$RESULT_FILE'" >/dev/null' ]; then
         echo "file $RESULT_FILE exists";
         exit;
         break;
      else
         echo "file $RESULT_FILE does not exist";
         sleep 5
      fi
   done
CASE I
When I tried to create $RESULT_FILE in the remote machine and the code still running, it always give me a false indication about the file even the actual file exist!

CASE II
When I tried to delete the remote file, the code above gives me again a false indication about the file by saying the remote file exists which no exist in reality

I hope if someone can help me with this problem

Thanks in advance.
Reply With Quote
  #2 (permalink)  
Old 30-01-2009, 06:29 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

SSH is not designed for this kind of work. It will never work out. You need to use ftp or something else using Perl / Python. FTP would be easy to script and can perform various operations such as rename, delete, upload, download and so on.
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 28-02-2009, 11:08 AM
Junior Member
User
 
Join Date: Feb 2009
OS: Redhat
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
r0tt13 is on a distinguished road
Default checking remote files via ssh

not to knock the person that responded, but with linux you can do almost anything you want to if you put some pieces together.

for your instance, you want to use grep. grep is your friend.

since grep uses a 0 or 1 on exit, 0 if it finds something or 1 if it doesnt, you could do something like this....

Code:
CHECKFILE=`ssh <user>@<host> 'ls /path/to/file | grep file > /dev/null; echo $?'`
if [ $CHECKFILE -eq 0 ]
then
echo "file exists"
else
echo "file doesnt exist"
fi
the value of the checkfile variable should echo a 1 or a 0 if you test it manually. you can see how grep can used for alot of different checks. i use it alot in scripts just because the fact that it exits a 1 or a 0 depending.... alot of tools do this but some dont.. its a matter of trial and error.

i hope this helps

Last edited by nixcraft; 28-02-2009 at 11:59 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Unix Shell: Check if Word exist in file or not LinuxCommandos Shell scripting 1 07-11-2008 02:50 AM
Check file exists on Remote Server sanjus Shell scripting 3 12-06-2008 06:05 AM
Checking files exist seeker082 Shell scripting 6 22-02-2008 12:39 AM
Shell script to check the disk space on remote systems vijayscripts Shell scripting 5 21-10-2007 06:29 PM
shell script to open log files and check for faults trueman82 Shell scripting 1 23-11-2006 02:35 AM


All times are GMT +5.5. The time now is 08:51 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38