4

I have a shell script of the following type:

#!/bin/bash

ssh mylogin@myremotemachine.com
echo "Hi"
exit

I run it locally to do something on a remote server (represented by 'echo "Hi"'). However, when I run it, I see the prompt on the remote server -- so the 'exit' command is not executed. When I then manually type 'exit' on the remote prompt, I then see "Connection to myremotemachine.com" closed and then "Hi". How can I set up the shell script such that it exits correctly and shows me the (local) prompt from which I executed it?

https://unix.stackexchange.com/questions/89747/ssh-exits-after-quit-case-in-bash-script and ssh and shell through ssh : how to exit? seem somewhat related, but I couldn't adapt the ideas presented there./

UPDATE

The following, not so minimal version leads to Unmatched '..

#!/bin/bash

date=`date "+%Y-%m-%d"`
rsync -acxzP --delete --link-dest=/u3/mylogin/backup/old_backup /home/mylogin mylogin@myremotemachine.com:/u3/mylogin/backup/$date\_backup
ssh mylogin@myremotemachine.com bash -c "'
rm -f /u3/mylogin/backup/old_backup
ln -s $date\_backup /u3/mylogin/backup/old_backup
exit
'"

If I remove the double quotes in this snippet, I get: bash: -c: option requires an argument and date: Undefined variable.

mathlete
  • 234

4 Answers4

9

This should solve your problem.

ssh mylogin@myremotemachine.com << HERE
rm -f /u3/mylogin/backup/old_backup
ln -s $date\_backup /u3/mylogin/backup/old_backup
HERE

Or you can do the following. Put all the commands you want to run on the remote host in a separate script. Give it a name like remote.sh

Now run the following

ssh mylogin@myremotemachine.com 'bash -s' < /path/to/remote.sh

Where remote.sh contains.

rm -f /u3/mylogin/backup/old_backup
ln -s $date\_backup /u3/mylogin/backup/old_backup
R J
  • 660
1

You can do

$ssh user@comp echo abc<ENTER>

then it will run that command (outputting abc), and exit

barlop
  • 25,198
1

Although barlops answer is correct, I think we can expand on it, to make it more clear. Since you seem to be using a script, I would suggest something like this:

#!/bin/bash

ssh mylogin@myremotemachine.com "echo Hi\!"

Extra reading: I also suggest you also look into special characters and when you need to escape them, as bash will automatically parse exclamation marks, when in double quotes. You can solve this by either using single quotes, or the backslash character to 'escape' it.

Edit:

#!/bin/bash
date=`date "+%Y-%m-%d"`
rsync -acxzP --delete --link-dest=/u3/mylogin/backup/old_backup /home/mylogin mylogin@myremotemachine.com:/u3/mylogin/backup/$date\_backup
ssh mylogin@myremotemachine.com '
date=`date "+%Y-%m-%d"`
rm -f /u3/mylogin/backup/old_backup
ln -s $date\_backup /u3/mylogin/backup/old_backup
'

When I tried a simplified version, the date variable works without issue, maybe this will correct the bash -c issue you were having. Also the exit is unneeded at the end, when the last command is executed the ssh session is automatically closed.

Ctark
  • 46
1

Maybe you have no newline at the end of script?

If there is no empty line (exactly LF - line feed character) after exit - your script will not "press enter" programatically.

Kamil
  • 2,686