0
B=$1
CHECK="$(ssh $USER@$LOGINHOST <<EOD
        . /path/loadprofile.sh
        if [[ ! -e \$TXTDR/$B.txt ]]; then; echo "TXT file for this job does not exist."; exit; fi
        mv \$TXTDR/$B.txt /tmp/$B
EOD)"

It is failing with exit code 1 and does not display any error message but the file that i am trying to move just vanishes. So i have two questions here.

  1. Why is it failing? When I am trying same from command line, it
    works.
  2. If it fails, why does the file that i am trying to move
    vanish? Is that how mv generally works?

NOTE: The script is running in bash shell and the default remote server shell is ksh.

  • What happens if you replace mv with echo mv? There is also -v option to mv. – ctrl-alt-delor Feb 8 '18 at 12:30
  • Which variables are defined before this snippet of script is run? You reference $USER, $LOGINHOST, $TXTDR, and $1. The standard variable $USER is unnecessary - ssh will default to the current user account. You escape $TXTDR for evaluation on the remote host so I assume you set it in loadprofile.sh (but you don't say so). There's no check for $1 or $B being empty, nor is there a check for it containing whitespace. – roaima Feb 9 '18 at 9:06
  • @roaima I have updated my post. Yes, i am loading the profile after connecting to remote server. Apologies, I posted a snippet of the script, not the complete script. There is a check for $1 and $B being empty. – Koshur Feb 9 '18 at 10:20
4

The here-document delimiter, EOD, that is ending the document, must be on a line by itself. The EOD) string is not equal to EOD.

mv does not remove files on failures. Assuming $B does not contain whitespace characters and if the TXTDR variable is undefined by the remote shell, the command executed would be

mv /$B.txt /tmp/$B

(with $B expanded by the local shell before ssh is called), but that will definitely not delete /$B.txt if the target is not writable.

If $B contains whitespace characters, it needs to be double quoted (just like $TXTDR should be):

mv "\$TXTDR/$B.txt" "/tmp/$B"

For doing the particular thing that you are using this code snippet for, I would probably have written it as

ssh "$user@$server" sh -s -- "$somepath/file.txt" <<'END_SCRIPT'
[ -e "$1" ] && mv "$1" "/new/location/${1##*/}"'
END_SCRIPT

or even just

ssh "$user@$server" sh -s -- "$somepath/file.txt" "/new/location/file.txt" <<'END_SCRIPT'
mv "$1" "$2"
END_SCRIPT

(which arguably does not need a here-document at all and could be shortened to ssh "$user@$server" mv "..." "...")

... without outputting unnecessary text. If you want to test for success, look at $? after this (would be zero if everything went ok).

This would also work:

if ! ssh "$user@$server" mv "..." "..."; then
    echo 'Something went wrong with ssh or remote mv' >&2
fi
| improve this answer | |
  • Oh yes! That did the trick. I remember it used to work for me previously even with ')'. Can you please answer the second part of the question. Why does the file vanish if mv fails? – Koshur Feb 8 '18 at 12:36
  • @Koshur I had to run for a bus. I will get back to this later. Others may supply fuller answers... – Kusalananda Feb 8 '18 at 12:39
  • @Koshur Where is $TXTDR defined? – Kusalananda Feb 8 '18 at 13:17
  • @roaima Ah, I am assuming its a code fragment rather than a complete script. – Kusalananda Feb 8 '18 at 13:23
  • 1
    @Kusalananda My bad - I was not escaping $?. All set now. – Koshur Feb 9 '18 at 11:43

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.