Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

So I have the following standard script:

    #!/bin/bash
    PS3='Please enter your choice: '
    options=("option 1" "Option 2" "Option 3" "Quit")
    select opt in "${options[@]}"
    do
        case $opt in
            "Option 1")
                echo "you chose choice 1"
                ;;
            "Option 2")
                echo "you chose choice 2"
                ;;
            "Option 3")
                echo "you chose choice 3"
                ;;
            "Quit")
                break
                ;;
            *) echo invalid option;;
        esac
    done

But when I run the script through SSH and choose the Quit option, the SSH exits too. What might cause the problem? I only want to exit the script and to continue work in SSH.

Well the problem was in .bash_aliases file which I discussed below. I was put exec before the script. When I remove it and run the command, everything was OK.

share|improve this question
    
are you running local script on remote server or what ? –  Rahul Patil Sep 7 '13 at 7:31
2  
when you run script it's just like command ssh user@ip command it will execute and exit –  Rahul Patil Sep 7 '13 at 7:32
    
I am running a script situated on a remote Ubuntu server. First I log in with SSH and then I run the script - just enter the script path and name.. –  ispasov Sep 9 '13 at 5:33
add comment

4 Answers

Based on the behavior you're describing it sounds like you're running your script like so:

$ ssh remote picker.bash

Example

When I ssh into a remote system it works as expected, mainly that I stay the remote server once the script has completed.

Say I have 2 computers, remotey and lappy. We'll denote them using "L" for lappy, and "R" for remotey.

L$ ssh remotey

R$ ./picker.bash
1) option 1
2) Option 2
3) Option 3
4) Quit
Please enter your choice: 4

R$

However if I run the script like this, I get the behavior you're describing where the ssh terminal goes away:

L$ ssh remotey ./picker.bash
1) option 1
2) Option 2
3) Option 3
4) Quit
Please enter your choice: 4

L$
share|improve this answer
    
Well before I run the script I have some more operations to do. So I log in with ssh username@server, then I do some file downloading on the remote server and after that I run the script - I write the path and the name of the script. And after that the ssh exits. –  ispasov Sep 9 '13 at 8:39
    
@ispasov - OK that's what I figured but the behavior sounded like the above. Can you share the rest of the script on the Ubuntu server. There is something else in the script besides the above which is causing it to exit. –  slm Sep 9 '13 at 12:33
    
@sim This is the whole script. I just test it before add more commands to it and I get this weird behavior. –  ispasov Sep 9 '13 at 12:39
    
@ispasov - Does the script not include the #!/bin/bash at the top then? –  slm Sep 9 '13 at 13:03
    
Yes. But I found the solution (my mistake). Please see the end of my answer. Thank you for your help. –  ispasov Sep 9 '13 at 14:34
show 1 more comment

You could start a sub-shell from option 4, but when I try it with 'exec /bin/bash --login' the prompt of the sub-shell is lost, rendering the remote shell somewhat difficult to use.

The man page for ssh seems to suggest that you can run a shell Xor a command, but not both:

SYNOPSIS ssh [many options] [user@]hostname [command] ...

If command is specified, it is executed on the remote host instead of a login shell.

...

When the user's identity has been accepted by the server, the server either executes the given command, or logs into the machine and gives the user a normal shell on the remote machine.

...

The session terminates when the command or shell on the remote machine exits

share|improve this answer
add comment

If you run ssh remotehost somescript then the script runs and that's it. You only get an interactive shell if you don't specify a remote command: the interactive shell is the default remote command. When the script exits, the remote session is finished.

If you want to run an interactive shell after the script, then tell SSH that you want both. Add the -t option so that you get a terminal on the remote host: by default, SSH only creates a terminal when you tell it to run an interactive shell, not when you specify a command.

ssh -t remotehost '/path/to/script; exec bash'

Note that any environment variable defined in the script will not be available. If you need that, source the script in the remote shell instead. This also lets you close the session from the script: exit in the script will not just exit the script but the whole remote session since that's all running inside one shell.

ssh -t remotehost '. /path/to/script; exec bash'
share|improve this answer
    
Well if I run the command like this SSH doesn't exit. But I have to log into the server first, then run several operations before to run the script. I also have bash alias for the script. So could you tell me how to start the script in that way and to prevent SSH to exit. –  ispasov Sep 9 '13 at 9:06
    
@ispasov If you run SSH and get a shell prompt, then invoke the script from that shell prompt, then SSH does not exit. –  Gilles Sep 9 '13 at 9:35
    
The problem is that it does exit. How to properly invoke the script - with sh script, exec script, or just to write the name of the script (bash alias)? –  ispasov Sep 9 '13 at 11:59
    
@ispasov What exactly are you doing then? The problem is in some part that you don't describe in your question. Post the complete script and explain exactly how you invoke it. –  Gilles Sep 9 '13 at 12:01
    
In my question I have posted the whole script as is. It's a test script so that's why it doesn't have any options. My steps are: 1. ssh user@server; 2. Password; 3. I am logged in the shell of the server and write the path and the name of the script /home/user/bin/script.sh; 4. When I choose option 4 - Quit, SSH exits. –  ispasov Sep 9 '13 at 12:05
show 4 more comments
up vote 0 down vote accepted

The problem was in bash_aliases. I had to remove exec before the script. So now the bash_aliases file looks like:

command="/path/to/script.sh"

The exec causes SSH to exit session.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.