Use other users to execute scripts in shell scripts

Source: Internet
Author: User

2. In some software installation processes, you need to create an independent user and then run some installation operations such as gitolite installation, create a git user and execute the installation.

Method 1:sudo -u user ...

Sudo-u Git-H <-codels ~ CAT test # other cmdscode
Subsequent commands will be executed by users after switching before code.

Method 2:su

Su command

    #!/bin/sh    echo ~/    su - test <<HERE    echo ~/    ls -al    HERE    echo ~/sudo su - user1echo #HOMEecho `ls -ltr`

You can use the sudo command. Its function is "excute a command as another user". By default, it is the root user. You can use-u to specify the user name (assuming that you want to execute the prog program as a user ):sudo -u user progYou can.

Another command, Su, is to run the shell with another identity, and then run the specified program with the new shell (Su can only run in the shell terminal, although the command can be written as [Su-C user prog] Like sudo, its implementation principle is actually to first run a shell and then run other programs through the shell ).

Http://www.shareup.me/metadocs/11197

In Shell, EOF and <are used in combination to indicate that subsequent input is used as the input of sub-commands or sub-shells until EOF is encountered, and then return to the main shell. As follows:

Export nls_lang = 'simplified chinese_china.zhs16gbk'
Sqlplus $ userid @ $ out_file <EOF
Exit
EOF

When you place this code in shell, the SQL plus command prompt status is automatically exited after the shell script is executed.

<EOF
(Content)
EOF

You can replace EOF with other things, which means to pass the content to the program as a standard input. Here, we will briefly review the usage of <. When s h e l sees <, it will know that the next word is a separator. The content after the Delimiter is treated as input until s h e l sees that the Delimiter is in a separate line ). This delimiter can be any string you define. Tip: it is best to leave no space at the beginning of the line.

Method 1. Automatically log on to MySQL (root: Root, passwd: 123456) and query the user = AA records in the test database and test1 table.

#! /Bin/sh
Mysql-uroot-p123456 <EOF
Use test;
Select * From testaa while a = 10000; ### 1000 not USR single quote Mark, because a is int
# Type, only char type need single quote Mark.

Exit

EOF

 

In shell: $0: get the name of the current script $ #: number of parameters passed to the script $: Process Number of the shell script $1, $2, $3...: script program parameters

Method 2: Use CT scripts

Secondary CT usage: Reference http://www.cnblogs.com/iloveyoucc/archive/2012/05/11/2496433.html

 You can put the script in the RC. Local directory in the/etc/rc. d directory.

 

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.