I am uploading a file via sftp and just performing the safety test if the file is completely uploaded to the remote server. I am taking md5 hash of both(file in local and file in remote server) and matching them. If they match I conclude that the upload was successful. Here is the part of code from shell script.

ssh $REMOTE_MC 'digest -a md5 $TARGET_DIR/$filename' > $HOME_DIR/remote_hash_$datetag.txt
local_hash=$(md5sum $HOME_DIR/$dump | cut -d' ' -f1)
echo "local = $local_hash"
cat $HOME_DIR/remote_hash_$datetag.txt
remote_hash=$(cat $HOME_DIR/remote_hash_$datetag.txt)
echo "remote = $remote_hash" 

local = cd8d77f0467754bc0c1c7ac3fb7f6184
remote = dee4a8484f99c577fd70cb8fa01e5995

The problem I am facing is that,when i run the script, hashes differ but if I run the command
ssh $REMOTE_MC 'digest -a md5 $TARGET_DIR/$filename' > $HOME_DIR/remote_hash_$datetag.txt
out of the shell script, i get the same hash. What am i doing wrong in the script?

Things I tried:
ssh $REMOTE_MC 'digest -a md5 $TARGET_DIR/$filename >> $TARGET_DIR/remote.txt'
I redirected the output to a remote file instead of local file, the remote file contains the correct hash. But if i redirect it to local file, the hash mismatches.

Thanks in advance.


You are using single quotes surrounding the ssh command. This way the variables $TARGET_DIR and $filename are taken literally instead of being evaluated. Change the single quotes to double quotes to have the variables evaluated:

ssh $REMOTE_MC "digest -a md5 $TARGET_DIR/$filename" > $HOME_DIR/remote_hash_$datetag.txt

Another suggestion is to write the variables as ${variable} like ${datetag} to make the variable name boundaries more clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.