Hi,
One thing first, I think you don't realize this:
If you su - (or su) another shell is started, but the one you started it from is still there and waiting (patiently). To get back to the 'beginning' you need to exit the shell(s) you started. I say this because of the remarks in posts #5 ( i give another su command ) and #7 ( su back to root ).
In short, if you do a su <user> or su - <user> you start another shell that should be closed again.
Some ways to do this, closing, are pressing CTRL-d or typing exit.
If I simplify things then the same is true when a script is started, it will start a new shell, do the commands and closes that shell again.
Code:
su - temper || { echo "Couldn't su -"; exit 1 ; }
This should work. From a bash shell commandline:
[stasis] druuna ~ $ su - jade || { echo "Couldn't su -"; exit 1 ; }
Password:
[stasis] jade ~ $
How do you run this piece of code?
BTW: The above code snippet migth not behave the way you expect it to due to the point I made in the first part of this post.
The exit 1 will exit the 'parent' shell (the shell from where you executed the code). This could be correct when done from a shell script (script will end), but if done from the command line your shell (session) will be closed.
Quote:
What i want is to run the script as root, then change to temper (in the script with su) and then execute a number of tasks in the script as user temper and later perhaps to su back to root.
But i was thinking perhaps this isn't the done thing and maybe better to run as root and then change to temper in the script and then call a different script to run as temper. however i'd rather not do this if i didn't have to. What do you think ?
|
The part in italic should be: return to root.
No perhaps, no su
It all depends on how much automation you want to have (my opinion: automate all that is possible
)
If you want to automate this, then I still think this is the way to go:
As root start a script. From within that script do some things as user root, start a script as user temper to do some things as user temper. Once this script is done, control is given back to the script started by root and it will continue (do some things as user root.
You need 2 scripts for that. The one that is started by root and the one called from that script
Here's the script that root starts:
Code:
#!/bin/bash
echo ""
echo "Running as user `id -un`"
echo ""
# need to do some things as user temper
# if something goes wrong exit this(!) script.
su - temper -c "/tmp/tets01" || { echo "Couldn't su -"; exit 1 ; }
echo ""
echo "Running as user `id -un`"
echo "Doing more things as root"
echo ""
Save it, make it executable and change owner/group to root.
This is the script that is called from the above script:
Code:
#!/bin/bash
echo ""
echo " Running as user `id -un`"
echo " Doing things"
echo ""
Save this last one as /tmp/tets01, make it executable an change its owner/group to that of temper.
It doesn't do much but print the user it runs under and echo some stuff. But, as you probably know, shellscripts can contain all sorts of commands.
Enough typing for one nigth
I'll find out soon enough if there will be a 'to be continued'......