0

I want to run a specific set of commands as 'oracle' user from ldap user 'exp'

for example:

exp@linuxtest -]$ sudo -u oracle \"export ORACLE_HOME=/u01/app/oracle;echo $ORACLE_HOME;export PATH=$PATH:$ORACLE_HOME/bin;$ORACLE_HOME/bin/exp full=y

I tried above command, getting following error

usage: sudo [-D level] -h | -K | -k | -V
usage: sudo -v [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-u user name|#uid]
usage: sudo -l[l] [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-U user name] [-u user name|#uid] [-g groupname|#gid] [command]
usage: sudo [-AbEHknPS] [-r role] [-t type] [-C fd] [-D level] [-g groupname|#gid] [-p prompt] [-u user name|#uid] [-g groupname|#gid] [VAR=value] [-i|-s] [<command>]

In the end, i want to run the following command as 'oracle' user without switching to oracle user. Because switching to oracle user requires password which gives full access as 'oracle' user to 'exp' user.

sudo su - oracle -c \"export ORAENV_ASK=NO;export ORACLE_SID=`ps -ef |grep pmon |grep -v grep | grep pmon | grep -v asm| grep -v APX | awk -F_ '{print $3}'`;export ORACLE_HOME="`cat /etc/oratab |grep dbhome | awk -F: '{print $2}' |sort  -u |sed -e /^$/d`";export ORACLE_SID=`ps -ef |grep pmon |grep -v grep | grep pmon | grep -v asm| grep -v APX | awk -F_ '{print $3}'`;export PATH=$PATH:"`cat /etc/oratab |grep dbhome | awk -F: '{print $2}' |sort  -u |sed -e /^$/d`"/bin:"`cat /etc/oratab |grep dbhome | awk -F: '{print $2}' |sort  -u |sed -e /^$/d`"/OPatch;"/u01/app/oracle/product/12.1.0/dbhome_1/bin/expdp  \"sys/oracle as sysdba\" DIRECTORY=DATAPUMP_DIR dumpfile=FULL_DB.dmp FULL=Y LOGFILE=full_exp.log"\"
2
  • are you sure that it is the full error message? by the way: you should mask the inner quotes of commands, not the outer most ones – Jaleks Dec 12 '18 at 23:00
  • Do you have a command already installed named oraenv that can set those environment variables for you? – Mark Plotnick Dec 13 '18 at 0:49
4

You're in quoting hell. Try this: using sudo, lauch a shell, and feed that shell commands from a here-doc. This version you can actually read:

sudo -u oracle -c sh <<'END_SH'
    export ORAENV_ASK=NO
    export ORACLE_SID=$(pgrep -fl pmon | grep -Eiv 'asm$|apx$' | cut -d_ -f3)
    export ORACLE_HOME=$(awk -F: '/dbhome/ {print $2}' /etc/oratab |sort  -u)
    export PATH="$PATH:$ORACLE_HOME/bin:$ORACLE_HOME/OPatch"

    "$ORACLE_HOME"/bin/expdp  "sys/oracle as sysdba" DIRECTORY=DATAPUMP_DIR dumpfile=FULL_DB.dmp FULL=Y LOGFILE=full_exp.log
END_SH

I simplified your grep|grep|awk pipelines quite a bit. I don't understand why you're using sort when setting ORACLE_HOME: are you expecting dbhome to appear multiple times in /etc/oratab?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.