Using the EXEC function group call to execute the Shell script __ function

Source: Internet
Author: User
Tags call shell strcmp

The EXEC function under Linux is not a single function, but a function group, respectively:

int execl (const char *path, const char *arg, ...);
int EXECLP (const char *file, const char *arg, ...);
int execle (const char *path, const char *arg, ..., char * const envp[]);
int execv (const char *path, char *const argv[]);
int EXECVP (const char *file, char *const argv[]);
int Execve (const char *path, char *const argv[], char *const envp[]);

You can see that the function name of this function group is a combination of exec and add L, V, p, E, four letters.

The letter L represents list, which means that the function takes a parameter table, which requires that each command-line parameter of the new program be described as a separate parameter, and must end with (char*) 0;

The letter v represents the vector, which means that the function takes a argv[] vector, which requires the first to construct an array pointer to each parameter, and then the array address as an argument to the EXEC function;

The letter P represents path, which means that the function takes filename as an argument and uses the PATH environment variable to find the executable file;

The letter e represents Environ, which means that the function takes a envp[] array instead of the current environment.

Here, the letters L and V are mutually exclusive, that is, they cannot appear in the EXEC function at the same time.

The EXEC function is most commonly called a replacement execution executable file, such as:

echo in Execl ("/bin/echo", "echo", "executed by Execl", NULL);

EXECL (ls in "/bin/ls", "ls", "/azuo", "-la", (char *) 0);

echo in EXECLP ("echo", "echo", "executed by EXECLP", NULL);

But another way to do this is to invoke the Execute shell script.

There are two ways to invoke the shell script using the EXEC function Group:

Method One: Using the EXECL function, this method is similar to the echo in Execl ("/bin/echo", "echo", "executed Byexecl", NULL). It uses the executable file directly to execute the script.

Change the/bin/echo to/bin/sh or/bin/bash.

This allows us to invoke the T.sh script using the following function

Execle ("/bin/sh", "sh", "t.sh", null,null);

You can pass parameters to the t.sh script, omitted here.

Method Two: Using the Execle function

char* env[]={"envargv=\" i m inenvironment! You "", NULL};

Execle ("t.sh", "Nouse", "I ' min argv!", null,env);

Using the Execle function, you can also pass arguments to the shell script, use $0,$1$2 in the script, and so on to refer to the first argument, which is similar to the argv of the main function. Here the Execle function discovers that the pathname parameter is not a machine executable produced by the connection editor, then considers the file to be a shell script, and then attempts to invoke/bin/sh and use filename as the input to the shell.

Unlike the previous method, the EXECL function runs a shell script that uses an executable file and does not require t.sh to have execute permissions. The Execle function requires the user to have EXECUTE permission on the invoked shell script, otherwise the call fails, and the permission denied error message is not usually prompted. So when calling a shell script using execle, make sure that the script itself has execute permissions.

Such differences and terminals are similar:

The use of SH t.sh under the terminal does not require permission to execute and./t.sh is needed.

The following is a specific set of examples:

t.sh script:

#!/bin/bash

If [-N "$ENVARGV"]; then
   echo "can use the Var of env."
   echo "The VAT in env: $ENVARGV"
   echo "the Var in argv:$1"
fi

echo "Er,you I again.

To test the main program:

Tryexec. Procedure

#include <unistd.h>
#include <string.h>
#include <stdio.h>

int main (int argc,char** argv)
{
    char par[10]={0};
    strcpy (par,argv[1]);
    if (!strcmp (par, "execl"))
    {
       printf ("******************execl*****************\n");
       Execl ("/bin/cat", "Cat", "/home/gs/exec.txt", NULL);
    if (!strcmp (par, "execle1"))
    {
       printf ("**************call shell script (1) *****************\n");
       Execle ("/bin/sh", "sh", "t.sh", null,null);
    }
    if (!strcmp (par, "execle2"))
    {
       printf ("**************call shell script (2) *****************\n");
       char* env[]={"envargv=\" I ' m in environment! for you to check me.\ "", NULL};
       Execle ("t.sh", "Nouse", "I ' M in argv!", null,env);
    }
    return 0;
}

The results of the operation are as follows:

root@ubuntu:/home/gs/linuxapiusage# gcc tryexec.c
root@ubuntu:/home/gs/linuxapiusage#./a.out execl
execl*****************
hello,you to me.
root@ubuntu:/home/gs/linuxapiusage#./a.out execle1
**************call Shell script (1) *****************
er , you are again me.
root@ubuntu:/home/gs/linuxapiusage#./a.out execle2   There is no t.sh Execute permission
**************call shell script (2) * * * 
root@ubuntu:/home/gs/linuxapiusage#./a.out execle2   Increased enforcement permissions chmod +x t.sh
************** Call Shell script (2) ***************** your can use the
var of env.
The VAT in env: "I ' m in environment! You are OK
. The Var in argv:i ' m in argv!
Er,you I again.

The above shell scripts called for Execl and Execle, of course, can be used with EXECV and EXECVE, but only as a matter of parameter incoming format.

For more information, please refer to:

EXEC related

Http://man7.org/linux/man-pages/man3/exec.3.html

Or Linux under man exec

Introduction to the EXEC function group

http://blog.csdn.net/fisher_jiang/article/details/5608399

and Apue 8.10

I enjoy the copyright of the blog article, reproduced please indicate the source http://blog.csdn.net/baidu20008


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.