Use the exec function group to call and execute shell scripts

Source: Internet
Author: User
Tags call shell strcmp

In Linux, the exec function is not a single function, but a function group, which is:

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[]);

We can see that the function names of this function group are all combined by exec and the four letters "L", "V", "P", and "E.

 

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

The letter V represents the vector, indicating that the function obtains an argv [] vector, that is, an array pointer pointing to each parameter must be constructed first, and then the array address is used as the parameter of the exec function;

The letter P represents path, indicating that the function takes filename as the parameter and uses the PATH environment variable to find the executable file;

The e-generation table "environ" indicates that this function takes an envp [] array without using 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.

Exec functions are most commonly called to replace executable files, such:

Echo in execl ("/bin/echo", "Echo", "executed by execl", null;

Execl ("/bin/ls", "ls", "/azuo", "-La", (char *) 0) in LS;

Echo in execlp ("Echo", "Echo", "executed by execlp", null;

 

However, another method is to call and execute shell scripts.

There are two ways to use the exec function group to call the shell script:

 

Method 1: Use the execl function. This method is similar to echo in execl ("/bin/echo", "Echo", "executed byexecl", null. It directly uses an executable file to execute the script.

Change/bin/ECHO to/bin/sh or/bin/bash.

In this way, we can use the following function to call the T. Sh script:

Execle ("/bin/sh", "sh", "T. Sh", null, null );

You can input parameters to the T. Sh script, which is omitted here.

 

Method 2: execle Function

 

Char * env [] = {"envargv = \" I'm inenvironment! You see me. \ "", null };

Execle ("T. Sh", "nouse", "I 'min argv! ", Null, ENV );

Using the execle function, you can also pass parameters to the shell script and use $0, $1 $2, and so on in the script for reference. $1 is the first parameter, this is similar to argv of main function.

Here, the execle function finds that the pathname parameter is not a machine executable file generated by the connection editor, and considers the file as a shell script, so it tries to call/bin/sh, and use filename as shell input.

Unlike the previous method, execl functions use executable files to run shell scripts, and T. Sh is not required to have execution permissions. The execle function requires the user to have the execution permission of the called shell script. Otherwise, the call fails and the permission denied error message is not displayed. Therefore, when using execle to call a shell script, make sure that the script has the execution permission.

 

The difference is similar to that in the terminal:

To use sh T. Sh in a terminal, you do not need to execute the permission./t. Sh.

 

A specific group of instances is as follows:

T. Sh script:

#!/bin/bashif [ -n "$envargv" ]; then   echo "you can use the var of env."   echo "the vat in env:$envargv"   echo "the var in argv:$1"fiecho "er,you see me again."

Test main program:

Tryexec. Program

#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! you see me.\"",NULL};       execle("t.sh","nouse","i'm in argv!",NULL,env);    }    return 0;}

The running result is as follows:

Root @ Ubuntu:/home/GS/linuxapiusage # GCC tryexec. croot @ Ubuntu:/home/GS/linuxapiusage #. /. out execl ********************************** hello, you see me. root @ Ubuntu:/home/GS/linuxapiusage #. /. out execle1 **************** call shell script (1) * **************** er, you see me again. root @ Ubuntu:/home/GS/linuxapiusage #. /. out execle2 no t here. sh execution permission *************** call shell script (2) * **************** root @ u Buntu:/home/GS/linuxapiusage #. /. out execle2 adds the execution permission chmod + x T. sh *************** call shell script (2) * *************** you can use the VaR of Env. the VAT in env: "I'm in environment! You see me. "The VaR in argv: I'm in argv! Er, you see me again.

The above is the shell script called by execl and execle. Of course, execv and execve can also be used, but it is just a problem of parameter input format.

For more information, see:

Exec Problems

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

Or in Linux, man Exec

Exec Function Group Introduction

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

And apue 8.10.

I enjoy the copyright of blog articles, reprint 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.