0

I have a bash-script that moves backup-files to the remote location. On few occasions the temporary HDDs on the remote server had no space left, so I added a md5 check to compare local and remote files. The remote ssh breaks however the while-loop (i.e. it runs only for first item listed in dir_list file).

# populate /tmp/dir_list
(while read dirName
  do
  # create archive files for sub-directories
  # populate listA variable with archive-file names
...
     for fileName in $listA; do
        scp /PoolZ/__Prepared/${dirName}/$fileName me@server:/archiv/${dirName}/
        md5_local=`md5sum /PoolZ/__Prepared/${dirName}/${fileName} | awk '{ print $1 }'`
        tmpRemoteName=`printf "%q\n" "$fileName"` # some file-names have strange characters
        md5_remote=`ssh me@server 'md5sum /archiv/'${dirName}'/'$tmpRemoteName | awk '{ print $1 }'`
        if [[ $md5_local == $md5_remote ]]; then
          echo "Checksum of ${fileName}: on local ${md5_local}, on remote ${md5_remote}." 
          mv -f /PoolZ/__Prepared/${dirName}/$fileName /PoolZ/__Backuped/${dirName}/
        else
          echo "Checksum of ${fileName}: on local ${md5_local}, on remote ${md5_remote}."  
          # write eMail
        fi
     done
  done) < /tmp/dir_list

When started the script gives the same md5-sums for the first directory listed in dir_list. The files are also copied both local and remote to expected directories and then script quits.

If I remove the line:

md5_remote=`ssh me@server 'md5sum /archiv/'${dirName}'/'$tmpRemoteName | awk '{ print $1 }'`

then apparently the md5-comaprison is not working but the whole script goes through whole list from dir_list.

I also tried to use double-quotes:

md5_remote=`ssh me@server "md5sum /archiv/${dirName}/${tmpRemoteName}" | awk '{ print $1 }'`

but there was no difference (broken dirName-loop).

I went so far, that I replaced the md5_remote... line with a remote ls-command without any shell-variables, and eventually I even tried a line without setting value to the md5_remote variable, i.e.:

ssh me@server "ls /dir/dir/dir/ | head -n 1"

Every solution that has a ssh-command breaks the while-loop. I have no idea why ssh should break bash-loop. Any suggestion are welcomed.

2
  • You might want to look at using rsync instead. Commented Nov 28, 2019 at 14:50
  • Also, doesn't the exit status of scp tell you if there was an error (such as the remote device running out of space)? Commented Nov 28, 2019 at 14:51

1 Answer 1

2

I'm plainly stupid. I found just answer on — what a surprise — stackoverflow.com.

ssh breaks out of while-loop in bash

As suggested I added a pipe to /dev/null and it works now:

md5_remote=`ssh me@server 'md5sum /archiv/'${dirName}'/'$tmpRemoteName < /dev/null | awk '{ print $1 }'`
Sign up to request clarification or add additional context in comments.

1 Comment

also ssh -n would have worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.