Quote:
Originally Posted by scottf456
I have a script that I need to run as my user. Inside I will be reading a text file that is a list of users. I need to change to each of these users and execute some commands as that user. It is necessary to be that user because the commands set up files that must be owned by the owner. Typically I would use the command:
sudo su - ${USERNAME}
but this requires a manual entering of the password. Is there a way that I can execute the sudo command and have the password be passed in as part of the command?
I have tried the following:
echo ${PASSWORD} | sudo -S su - ${USERNAME}
which looked good but did not actually stay as the new user.
Please note that I do have permissions needed to execute the sudo command from the command line.
|
Two things come to mind here: if you know the user's password, you could just omit the sudo, then your echo may work. Or, you could modify sudoers to not prompt those users for a password, either at all or just for that script.
EDIT:
After further thought, this jumped out at me:
"because the commands set up files that must be owned by the owner"
Since the script is ostensibly running as sudo/root, why not just set things up, and run a chmod/chown on the files that gets created?? Safer than having a script that can perform operations as another user....