mv command on remote server not working
Clash 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.
- Why is it failing? When I am trying same from command line, it
works. - 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.
bash shell-script ksh mv here-document
add a comment |Â
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.
- Why is it failing? When I am trying same from command line, it
works. - 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.
bash shell-script ksh mv here-document
What happens if you replacemv
withecho mv
? There is also-v
option tomv
.
â 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 inloadprofile.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
add a comment |Â
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.
- Why is it failing? When I am trying same from command line, it
works. - 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.
bash shell-script ksh mv here-document
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.
- Why is it failing? When I am trying same from command line, it
works. - 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.
bash shell-script ksh mv here-document
edited Feb 9 at 7:25
asked Feb 8 at 12:24
Koshur
3272515
3272515
What happens if you replacemv
withecho mv
? There is also-v
option tomv
.
â 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 inloadprofile.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
add a comment |Â
What happens if you replacemv
withecho mv
? There is also-v
option tomv
.
â 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 inloadprofile.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
add a comment |Â
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
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
 |Â
show 8 more comments
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
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
 |Â
show 8 more comments
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
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
 |Â
show 8 more comments
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
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
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
 |Â
show 8 more comments
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
 |Â
show 8 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
What happens if you replace
mv
withecho mv
? There is also-v
option tomv
.â 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 inloadprofile.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