1831991 Members
3061 Online
110034 Solutions
New Discussion

Session is closing

 
Chandramani
Occasional Contributor

Session is closing

Hi there,
I wrote a small scipt which will give a small menu.And the last option is exit.It is working fine when I am just excuting like this.
./bv_tools

but when I tried to source into my shell it is killing my telnet screen and closing all the windows
. ./bv_tools
---------------------------------
Here is the code which I wrote for exit:

echo " x) Exit"
echo
echo
echo " Your Selection : \c "
read CHOICE
if [ "${CHOICE}" = x ]
then
clear
exit 0
else
clear
(( k = "$CHOICE - 1" ))
${MENUCOMMAND[$k]}
fi
-----------------------------------


What could be the reason???

Please help me to solve the issue.

Thanks in advance.

-Chandramani
9 REPLIES 9
A. Clay Stephenson
Acclaimed Contributor

Re: Session is closing

Hi:

This is a case of the shell doing exactly what you ask it to do.

When you execute as ./bv_tools, bv_tools is run as a separate process so that the exit only applies to the shell executing bv_tools BUT when your source it as '. ./bv_tools' then this is run in the current shell and as soon as you exit you actually exit the current shell. NEVER use exits in sourced files (unless you truly want to exit the foreground process).


If it ain't broke, I can fix that.
ashish nanjiani
Frequent Advisor

Re: Session is closing

Hi your exit statement looks good. Can you send in the full code just to verify if everything looks good above and below the script.
Mladen Despic
Honored Contributor

Re: Session is closing

Hi,

If you run ./bv_tools,
the shell will create a child process, so your 'exit' command will simply tell the child process to exit, and you will be returned to the shell.

But if you run '. ./bv_tools', it's as if you type each line of your script at the command line. So, your 'exit' command will actually exit your shell.

If you need to test your script with the "dot" command, you can run 'sh' to open another shell first. Then '. ./bv_tools will terminate by exiting that shell, and you will be returned to your original shell.

Mlade
Chandramani
Occasional Contributor

Re: Session is closing

Hi Stephenson ,
Thanks for the clue. Can you give any alternative or other way to exit from the menu ,but not from the telnet session.

Thanks
Mladen Despic
Honored Contributor

Re: Session is closing

You can run:

sh -c ./bv_tools
Gerald Bush_2
Occasional Advisor

Re: Session is closing

All of the answers you received are valid. My personal preference would lead me to build the menu into a case statement.

print " 1) Say Hello "
print " 2) Say Goodbye "
print " 3) Exit "

print -n "Please select your choice: "
read choice
case $choice in
1) say_hello_function;;
2) say_goodbye_function;;
3) leave_function;;
*) print " Sorry, bad choice "
esac
# you cannot use exit
# as the name of a
# function as like a meta
# character it has special meaning

you can source these functions in a couple of different ways ( ie: functions in the same file above the case statement or functions in a "referenced" file like .filenamerc.

I personally like the .filenamerc method but it really doesn't matter.

jiin
David Burgess
Esteemed Contributor

Re: Session is closing

You're running it in the parent shell. Like exec does. When the script exits it's shell exits which is your only shell and therefore you get logged off.

HTH

Dave.
Chandramani
Occasional Contributor

Re: Session is closing

Hi,
I solved that problem by putting die() in place of exit 0.
I don't know whether it is correct or not,but it is working for me.If anyone knows more about die(),please explain little more.

Thanks
Deepak Extross
Honored Contributor

Re: Session is closing

Won't you achieve the same result by simply commenting out the exit statement?