Eu modifiquei o seu script e este funciona agora. Eu adicionei alguns comentários dentro do script para torná-lo mais compreensível. Deixe-me saber se você precisar de mais ajuda.
#!/bin/bash
#The export path which we set here.
export PRIMARY=/home/ramesh
#The main for loop execution starts here.
for entry in "$PRIMARY"/*
do
#Get the base name of the file which we check in the remote servers.
#Get just the filenames without the path.
#I am going to use the filename in the remote server to check.
filename=$(basename "$entry")
echo "File Name: $filename"
#Calculate the MD5Sum locally.
local_md5sum=$(md5sum "$entry")
echo "Local MD5Sum: $local_md5sum"
#Check if the file exists in server1.
#Otherwise I can check in the other server.
if ssh ramesh@server1 stat /home/ramesh/'$filename' \> /dev/null 2\>\&1 then
#I have the file in server1 and so I get the md5sum from server1.
#I store the md5sum inside remote_md5sum variable.
remote_md5sum=$(ssh ramesh@server1 "cd /home/ramesh/; find -name '$filename' -exec md5sum {} \;")
else
#Now, I know the file is in server2 as it is not present in server1.
remote_file=$(ssh ramesh@server2 "cd /home/ramesh/; find -name '$filename' -exec md5sum {} \;")
fi
echo "Remote MD5Sum: $remote_file"
done
Teste
Eu queria testar o script acima para nomes de arquivos com espaços também. Funciona bem e esta é a saída que recebo quando executo o script.
File Name: file1
Local MD5Sum: 39eb72b3e8e174ed20fe66bffdc9944e /home/ramesh/file1
Remote MD5Sum: b5fc751f836c5430b617bf90a8c4725d ./file1
File Name: file with spaces
Local MD5Sum: 36707e275264f4ac25254e2bbe5ef041 /home/ramesh/file with spaces
Remote MD5Sum: 36707e275264f4ac25254e2bbe5ef041 ./file with spaces