How to switch users to execute the appropriate command or shell script in a shell script

Source: Internet
Author: User

In general, you may need to switch the root user to another user for execution during the automation process, such as: Oralce

However, the execution of the command will return to the root user, continue to execute other commands under the root user.

You need to know the parameters of the SU command-C or-s

Like what:

# su-oracle-c "PWD"
/home/oracle

Or

Su-oracle-s/bin/sh test.sh

The relevant commands in the test.sh executed at this point are to be bin/sh, in other words, bin/sh supported commands can be executed.

Of course we can redirect the results to other files, and then parse the file again after execution to find the results we need to automate the execution.

Of course we can edit a shell script to perform user Switching.

such as: VI test.sh after entering the following content

#!/bin/bash
Su-test <<eof
Pwd
Exit
Eof

Press [ESC] key, enter: Wq, return to save exit.

After chmod 777 test.sh

Execution: Sh test.sh or. test.sh or./test.sh

ok! problem solved.

How to switch users to execute the appropriate command or shell script in a shell script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.