How do I perform commands in a bash script as a different user?
Hi. This is sort of a follow-up (but entirely new) question to one I posted yesterday.
Code:
#!/bin/bash Code:
sudo su - $1 Code:
su -c "command" $1 |
Code:
sudo -u otheruser /path/to/command parm1 parm2 |
While I appreciate the gesture; that didn't work.
|
Quote:
jlinkels |
You do realize that you also have to make sure that sudo is configured to allow that user to run that command, right?
|
Quote:
Code:
#!/bin/bash If you need to do a more complex edit, I recommend putting that part in a separate scriptlet. You can do Code:
sudo -u "$User" -- bash -c ' Code:
sudo -u "$User" -- bash -c "export USER='$User'; export HOME='$Home'; . /path/to/helper" || exit $? Note the getent passwd "$User" | cut -d : -f 6 expression: I believe it is the most portable easy way to find the user home directory. It should work even when using a remote user database. It does work fine on all machines I tested, even on SunOS 5.10. You could always rely on Bash tilde expansion, but applying the expansion from a string is a bit difficult to do reliably; all the ways I could think of just now would involve eval. |
All times are GMT -5. The time now is 12:10 AM. |