views:

104

answers:

2

Hi All, I am writing a shell script where in a condition the script will change the user like su - xxx and then it has to execute some commands/scripts and should stay login to that user/session for user interaction. Once user enter exit it should exit from that session and should return to script.

I have tried using su - xxx -c "commands" but it terminates the session after executing the commands. I want to stay on that session after execution of the commands, user should exit manually (type exit) to close that session and return to script.

Any hint will do..

Thanks in advance....for you suggestion..

A: 

What does "should stay login to that user/session for user interaction" mean? Do you want to give the user a regular shell prompt so they can type commands?

To do that, you need to start a shell separately. Try:

#!/bin/bash
echo Doing stuff...
echo Starting shell. Type "exit" when done.
sudo su - -c bash
echo Thanks for playing!

This will start a sub-shell for the user. If the sub-shell terminates (usually when the user types exit/Ctrl-D), the script continues.

sleske
Thanks you so much for ur answer.
srikant
I want to export some ailas to new session, is there any way to do that, I have tried alias -x but not working.
srikant
A: 

Probably the easiest way is to start bash as the final command in your list of commands.

Douglas Leeder
Thanks you so much for ur answer.
srikant
I want to export some ailas to new session, is there any way to do that, I have tried alias -x but not working.
srikant
I don't think you can pass aliases across - su gets in the way. `su -` will start a login session anyway.
Douglas Leeder
You could write out a script for the bash session to load perhaps?
Douglas Leeder
Is there any way we can export the alias to subshell?with ksh
srikant
I'm afraid I don't know `ksh` at all, so you'd be better off asking that as a separate question - perhaps on superuser?
Douglas Leeder