Run a command as user "x" in a shell script

Ever need to run a command as a specific user? Many Oracle commands require you to become the “oracle” user to make use of them.

To do this in a script, simply define your Oracle variables and use the “su” command. Here’s some example code:

ORA_HOME=/d01/app/oracle/product/9.2.0
ORA_OWNER=oracle
su – $ORA_OWNER -c “$ORA_HOME/bin/lsnrctl start listener_rmdyd1”
su – $ORA_OWNER -c “$ORA_HOME/bin/lsnrctl start listener_hbstgu1”