mv command on remote server not working

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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.







share|improve this question






















  • What happens if you replace mv with echo mv? There is also -v option to mv.
    – ctrl-alt-delor
    Feb 8 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 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 at 10:20















up vote
0
down vote

favorite












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.







share|improve this question






















  • What happens if you replace mv with echo mv? There is also -v option to mv.
    – ctrl-alt-delor
    Feb 8 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 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 at 10:20













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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.







share|improve this question














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.









share|improve this question













share|improve this question




share|improve this question








edited Feb 9 at 7:25

























asked Feb 8 at 12:24









Koshur

3272515




3272515











  • What happens if you replace mv with echo mv? There is also -v option to mv.
    – ctrl-alt-delor
    Feb 8 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 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 at 10:20

















  • What happens if you replace mv with echo mv? There is also -v option to mv.
    – ctrl-alt-delor
    Feb 8 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 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 at 10:20
















What happens if you replace mv with echo mv? There is also -v option to mv.
– ctrl-alt-delor
Feb 8 at 12:30




What happens if you replace mv with echo mv? There is also -v option to mv.
– ctrl-alt-delor
Feb 8 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 at 9:06




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 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 at 10:20





@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 at 10:20











1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










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





share|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 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 at 12:39










  • @Koshur Where is $TXTDR defined?
    – Kusalananda
    Feb 8 at 13:17










  • @roaima Ah, I am assuming its a code fragment rather than a complete script.
    – Kusalananda
    Feb 8 at 13:23






  • 1




    @Kusalananda My bad - I was not escaping $?. All set now.
    – Koshur
    Feb 9 at 11:43










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422791%2fmv-command-on-remote-server-not-working%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










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





share|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 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 at 12:39










  • @Koshur Where is $TXTDR defined?
    – Kusalananda
    Feb 8 at 13:17










  • @roaima Ah, I am assuming its a code fragment rather than a complete script.
    – Kusalananda
    Feb 8 at 13:23






  • 1




    @Kusalananda My bad - I was not escaping $?. All set now.
    – Koshur
    Feb 9 at 11:43














up vote
4
down vote



accepted










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





share|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 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 at 12:39










  • @Koshur Where is $TXTDR defined?
    – Kusalananda
    Feb 8 at 13:17










  • @roaima Ah, I am assuming its a code fragment rather than a complete script.
    – Kusalananda
    Feb 8 at 13:23






  • 1




    @Kusalananda My bad - I was not escaping $?. All set now.
    – Koshur
    Feb 9 at 11:43












up vote
4
down vote



accepted







up vote
4
down vote



accepted






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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 8 at 14:53

























answered Feb 8 at 12:29









Kusalananda

103k13202318




103k13202318











  • 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 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 at 12:39










  • @Koshur Where is $TXTDR defined?
    – Kusalananda
    Feb 8 at 13:17










  • @roaima Ah, I am assuming its a code fragment rather than a complete script.
    – Kusalananda
    Feb 8 at 13:23






  • 1




    @Kusalananda My bad - I was not escaping $?. All set now.
    – Koshur
    Feb 9 at 11:43
















  • 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 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 at 12:39










  • @Koshur Where is $TXTDR defined?
    – Kusalananda
    Feb 8 at 13:17










  • @roaima Ah, I am assuming its a code fragment rather than a complete script.
    – Kusalananda
    Feb 8 at 13:23






  • 1




    @Kusalananda My bad - I was not escaping $?. All set now.
    – Koshur
    Feb 9 at 11:43















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 at 12:36





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 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 at 12:39




@Koshur I had to run for a bus. I will get back to this later. Others may supply fuller answers...
– Kusalananda
Feb 8 at 12:39












@Koshur Where is $TXTDR defined?
– Kusalananda
Feb 8 at 13:17




@Koshur Where is $TXTDR defined?
– Kusalananda
Feb 8 at 13:17












@roaima Ah, I am assuming its a code fragment rather than a complete script.
– Kusalananda
Feb 8 at 13:23




@roaima Ah, I am assuming its a code fragment rather than a complete script.
– Kusalananda
Feb 8 at 13:23




1




1




@Kusalananda My bad - I was not escaping $?. All set now.
– Koshur
Feb 9 at 11:43




@Kusalananda My bad - I was not escaping $?. All set now.
– Koshur
Feb 9 at 11:43












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422791%2fmv-command-on-remote-server-not-working%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay