$SHELL usage in script

June 5, 2014 at 05:48:13
Specs: Linux, x86-64
I was trying to find a method to launch a new shell from a script, which is in fact a menu type of script. I just call $SHELL because that is what holds the shell that is default for the user that runs the script in the first place.

It starts another session ... but I cannot exit from it. If I enter "exit" explicitally, it says "exit", so it actually closes a session, but then it seems to return into another new session. If I repeatingly enter "exit", he keeps on exiting, but I never actually exit my initial telnet session.
I did the same in another script, and it showed the same behaviour.

I do the same from the command prompt, that means just launching "$SHELL" (without quotes), and there it actually works, if I repeat the exit command, it'll evntually destroys my telnet session.

Why doesn't that work from script ?
How should I do that ?

Hi there.


See More: $SHELL usage in script

Report •

#1
June 5, 2014 at 08:14:51
Unix/Linux is designed so the child cannot kill te parent shell. The menu program is the child so typing exit only kills the menu program but can never kill the parent. Of course, from the child, you can find the process ID of the parent and kill the parent, but I don't recommend that. If you want to make sure a user can only use the menu program, place this in their .profile on the last line:

/PATH/TO/menuprogram; exit;

This ensures the user never gets the command line.

If you want to go hard core, ask the unix/linux administrator to change the default shell in /etc/passwd to the menu program:

nails:x:1407:comment,,,://home/nails:/path/to/menu_program

The above line ensures user nails only can execute the menu_program.


BTW, you cannot always be certain that the SHELL contents always contain the shell you think it does. On my Solaris system, it always set to /bin/sh no matter what your parent shell is. Of course, you can always change it if you have command line access.

From the command line, you can see the name of your current shell by executing:

echo $0

(This works for every shell, but csh and every variation of the "C" shell.)

Instead of SHELL, this is what I do; I place this command in my .profile:

export my_shell=$(echo $0)

This ensures that shell variable my_shell always contains the value of my parent shell.

message edited by nails


Report •

#2
June 5, 2014 at 10:58:46
"Unix/Linux is designed so the child cannot kill te parent shell. The menu program is the child so typing exit only kills the menu program but can never kill the parent."

Maybe I explained wrong, but killing the telnet session is not the purpose. I only told to explain that the chain is not followed to the end (which is typically what happens when you repeat using "exit").

It's like this :

shell (telnet session) -> script (menu) -> shell

The last shell never wants to return to the script (menu). I do not want to kill the shell which is the telnet session!

Hi there.


Report •
Related Solutions


Ask Question