0

I have this script:

#!/bin/bash
#!/usr/bin/expect
dialog --menu "Please choose the server to connect to:" 10 30 15 1 RAS01 2>temp

#OK is pressed
if [ "$?" = "0" ]
then
        _return=$(cat temp)

        # /home is selected
        if [ "$_return" = "1" ]
        then
                dialog --infobox "Connecting to server ..." 5 30  ; sleep 2
                telnet XXX
        fi

# Cancel is pressed
else
        exit
fi

# remove the temp file
rm -f temp

In the part of the code that says: # Cancel is pressed I want to insert some type of command that will disconnect the session and close the terminal automatically. Ive tried different variations of exit, exit 1, exit 5, close, etc. but none seem to do the trick

1 Answer 1

1

Here is what you can do:

kill -9 "$(ps --pid $$ -oppid=)"

But I definitely suggest you not to use this way. A better solution is to get the exit code of your script and exit if needed. For example

yourscript:

#... ...
else
    exit 1
fi

And in your ssh connection you do:

./myscript || exit

This is the correct way. Try to use it

Sign up to request clarification or add additional context in comments.

5 Comments

Thank You! I do have another question. This solves my whole issue when dealing with the Cancel Button. However, when the user selects OK it initiates a telnet connection to another server... would it be possible that once the telnet session with the external server is closed, that the connection where the script is running will automatically logoff. Basically trying to use the same command: 'kill -9 "$(ps --pid $$ -oppid=)"' but when the telnet session to the external server is closed.
@WiloMaldonado Sorry, I am not following you. Could you provide an example?
In a nutshell we want this script to load automatically for users and not allow them to do anything else. It loads automatically and if they hit cancel on the dialog screen it disconnects the session. So if I am at Server1 and I choose to initiate the telnet connection to Server2 (as showed in the code above) once I am done doing whatever I am at Server2 and close my session at Server2, the connection bounces back to Server1 but the script is no longer running. We want to terminate the active connection to Server1 once the user returns from Server2. Is this clearer?
I solved it. I just added the instruction: 'exit 5' after the telnet connection and it exits the script once it returns.
@WiloMaldonado great! Thanks for informing us about the solution. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.