LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   execute command as different user (https://www.linuxquestions.org/questions/linux-general-1/execute-command-as-different-user-63197/)

kilobravo 06-02-2003 09:19 AM

execute command as different user
 
I am running a script as root, and need to execute a few commands as another user from within the script. I have tried enclosing the commands in both parenthesis and curly brackets such as the following:

{ su <username>; source .bash_profile; <command1>; <command2> }

The problem is that commands 1 & 2 are execute as root, so the user was not changed by the su command. Actually, it appears as if it was changed, but that the commands following the su command were not executed in the correct shell. Is there any way to accomplish what I need?

Thanks.

nikhiljosh 06-02-2003 10:02 AM

$su
Password:
#su foo -c whoami
foo

As you can see i ran a command as foo.
I guess u'll have to run individual commands instead of group
HTH

unSpawn 06-02-2003 10:03 AM

Enclose in quotes: su <username> -c 'a; b; c' ?


All times are GMT -5. The time now is 05:36 PM.