0

I am reading a list of file names from a config file. using a for loop im selecting each file in the list and then want to search for the file on a remote server.

for file in config
do

**search for file at {remoteserver}**
    returnStatus=$?
    if [ returnStatus = 0 ]; then
         email that file was found.
    fi
done  

the search for file at the remote server is the part troubling me. Any help is appreciated,

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Blue42
  • 139
  • 4
  • 13
  • Have you checked this question? [How to check if a directory exists in a shell script](http://stackoverflow.com/q/59838/1983854). Do that together with `ssh`. – fedorqui Mar 19 '14 at 11:01

2 Answers2

2

You can do something like:

for file in "${config[@]}"
do
    ssh user@remotehost test -e "$file"
    returnStatus=$?
    if [ $returnStatus -eq 0 ]; then
         email that file was found.
    fi
done  
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
1

Assuming that

  1. All the files are on separate servers
  2. You want to send the email from your local system

this could be the solution:

for file in "${config[@]}"
do
    exists=$(ssh user@host "if [ -e \"$file\" ]; then echo '1'; else echo '0'; fi")
    if [ $exists = "1" ]
    then
        echo "$file exists"
        #send email
    fi
done

If all the files were on the same remote server, it would make more sense to do the loop within the ssh connection, rather than reconnecting every time.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • Just wanted to thank you for helping in debug the script and getting it fixed. As you can tell, I am not that great with `bash`. Hopefully will get there soon. Appreciate it. +1 – jaypal singh Mar 19 '14 at 13:39