Run a script as a different user
I am writing a bash script. I run it as root, and inside the script I create a user x and
"su - x". I then want to continue running the script as the user x. This seemed impossible as the script stopped until I exited back to root, and then it continued. I've figured out a way around this by creating a .bash_profile for x before I "su - x" that tells it to run automatically a script that I create beforehand. If I haven't lost you yet, then you're nuts. In essence, I would like to do one of the following:
1) Run a script as user and then have it automatically exit back to root. Something like "Run scipt as x", and then when it's done, I'm root again.
OR (by solving the following problem, I have figured a way around the first one)
2) If I'm currently user x, is there a command I can type that will guarantee exiting back out to root? I've tried to put "exit" at the end of my script, but it doesn't seem to run it. Similarly with logout. I currently have a script running as root, so if I put "su - root" at the end of the script I'm running as x, will it continue the original root script, or will this new "root environment" be different from the original one?
If you can help me, that would be great. If you're wondering what the heck I'm trying to do, I'll tell you. I'm attempting to create an automatic LFS (Linux from Scratch) install script for Knoppix. I guess another, more simple question to ask is this : "Is it necessary to su - lfs to install chapter 4 in knoppix?
If you don't know what LFS is, I'd still appreciate an answer to my problems above. Thank-you in advance.