How to run a command as specific user without switching(su)











up vote
0
down vote

favorite












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""









share|improve this question









New contributor




user918753 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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
    2 days ago










  • Do you have a command already installed named oraenv that can set those environment variables for you?
    – Mark Plotnick
    2 days ago

















up vote
0
down vote

favorite












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""









share|improve this question









New contributor




user918753 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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
    2 days ago










  • Do you have a command already installed named oraenv that can set those environment variables for you?
    – Mark Plotnick
    2 days ago















up vote
0
down vote

favorite









up vote
0
down vote

favorite











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""









share|improve this question









New contributor




user918753 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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""






shell






share|improve this question









New contributor




user918753 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user918753 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









mature

1292




1292






New contributor




user918753 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









user918753

61




61




New contributor




user918753 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user918753 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user918753 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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
    2 days ago










  • Do you have a command already installed named oraenv that can set those environment variables for you?
    – Mark Plotnick
    2 days ago




















  • 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
    2 days ago










  • Do you have a command already installed named oraenv that can set those environment variables for you?
    – Mark Plotnick
    2 days ago


















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
2 days ago




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
2 days ago












Do you have a command already installed named oraenv that can set those environment variables for you?
– Mark Plotnick
2 days ago






Do you have a command already installed named oraenv that can set those environment variables for you?
– Mark Plotnick
2 days ago












1 Answer
1






active

oldest

votes

















up vote
4
down vote













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?






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    user918753 is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487668%2fhow-to-run-a-command-as-specific-user-without-switchingsu%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote













    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?






    share|improve this answer



























      up vote
      4
      down vote













      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?






      share|improve this answer

























        up vote
        4
        down vote










        up vote
        4
        down vote









        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?






        share|improve this answer














        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?







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 days ago

























        answered 2 days ago









        glenn jackman

        50k569106




        50k569106






















            user918753 is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            user918753 is a new contributor. Be nice, and check out our Code of Conduct.













            user918753 is a new contributor. Be nice, and check out our Code of Conduct.












            user918753 is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487668%2fhow-to-run-a-command-as-specific-user-without-switchingsu%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Entries order in /etc/network/interfaces

            新発田市

            Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)