Cprogramming 简明教程

Command Execution in C

Command Execution in C

Command execution in C 用于使用 C 语言程序执行系统命令。系统命令是通过使用 system() function 执行的,它是 stdlib.h header file 的库函数。

Command execution in C is used to execute the system command using the C program. The system commands are executed by using the system() function which is a library function of stdlib.h header file.

通过使用 system() 函数,您可以在 C 程序中执行 Windows/Linux 终端命令。

By using the system() function, you can execute the Windows/Linux terminal commands inside a C program.

Syntax

以下是执行系统命令的语法 −

The following is the syntax to execute system commands −

system(char *command);

Example of Command Execution

以下代码显示了在 C 语言中使用 system() 函数执行 ls 命令的情况。

The following code shows the execution of ls command using system() function in C language.

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

int main() {

   char cmd[10];
   strcpy(cmd,"dir C:\\users\\user\\*.c");
   system(cmd);

   return 0;
}

Output

运行代码并检查其输出:

Run the code and check its output −

C:\Users\user>dir *.c
 Volume in drive C has no label.
 Volume Serial Number is 7EE4-E492

 Directory of C:\Users\user

04/01/2024  01:30 PM               104 add.c
04/02/2024  01:37 PM               159 add1.c
04/02/2024  01:37 PM               259 array.c
04/02/2024  01:37 PM               149 main.c
04/02/2024  01:37 PM               180 recursion.c
04/02/2024  01:37 PM               241 struct.c
04/02/2024  01:37 PM               172 voidptr.c
               7 File(s)           1,264 bytes
               0 Dir(s)  139,073,761,280 bytes

exec Family of Functions in C

exec 函数簇已在 " unistd.h " 头文件中引入。这些函数用于执行某个文件,并且一旦调用它们,便会用新进程映像替换当前进程映像。

The exec family of functions have been introduced in the "unistd.h" header file. These functions are used to execute a file, and they replace the current process image with a new process image once they are called.

以下是在 C 中的 exec 族函数 −

The following are the functions of exec family in C −

1. execl() Function

execl() 函数的第一个参数是可执行文件作为其第一个参数。当执行文件时,后续参数将可用。最后一个参数必须为 NULL。

The execl() function’s first argument is the executable file as its first argument. The next arguments will be available to the file when it’s executed. The last argument has to be NULL.

int execl(const char *pathname, const char *arg, ..., NULL)

请看以下示例:

Take a look at the following example −

#include <unistd.h>

int main(void) {

   char *file = "/usr/bin/echo";
   char *arg1 = "Hello world!";

   execl(file, file, arg1, NULL);

   return 0;
}

正在通过 C 代码调用 Linux 中的 echo 命令。

The echo command in Linux is being invoked through the C code.

Output

保存、编译并执行上述程序 −

Save, compile, and execute the above program −

$ gcc hello.c -o hello
$ ./hello
Hello world!

2. execlp() Function

execlp() 函数与 execl() 函数类似。它使用 PATH 环境变量找到文件。因此,不必给定可执行文件的路径。

The execlp() function is similar to the execl() function. It uses the PATH environment variable to locate the file. Hence, the path to the executable file needn’t be given.

int execlp(const char *file, const char *arg, ..., NULL)

请看以下示例:

Take a look at the following example −

#include <unistd.h>

int main(void) {

   char *file = "echo";
   char *arg1 = "Hello world!";

   execlp(file, file, arg1, NULL);

   return 0;
}

Output

此处, echo 已位于 PATH 环境变量中。保存、编译并从终端运行。

Here, echo is already located in the PATH environment variable. Save, compile and run from the terminal.

$ gcc hello.c -o hello
$ ./hello
Hello world!

3. execle() Function

在 execle() 函数中,我们可以将环境变量传递给函数,它会使用它们。它的原型如下 −

In the execle() function, we can pass environment variables to the function, and it’ll use them. Its prototype is like this −

int execle(const char *pathname, const char *arg, ..., NULL, char *const envp[])

请看以下示例:

Take a look at the following example −

#include <unistd.h>

int main(void) {

   char *file = "/usr/bin/bash";
   char *arg1 = "-c";
   char *arg2 = "echo $ENV1 $ENV2!";
   char *const env[] = {"ENV1 = Hello", "ENV2 = World", NULL};

   execle(file, file, arg1, arg2, NULL, env);

   return 0;
}

Output

保存、编译并从终端运行 −

Save, compile, and run from the terminal −

$ gcc hello.c -o hello
$ ./hello
Hello world!

4. execv() Function

execv() 函数接收一个参数向量,该向量可供可执行文件使用。此外,该向量的最后一个元素必须为 NULL:

The execv() function receives a vector of arguments that will be available to the executable file. In addition, the last element of the vector has to be NULL:

int execv(const char *pathname, char *const argv[])

请看以下示例:

Take a look at the following example −

#include <unistd.h>

int main(void) {

   char *file = "/usr/bin/echo";
   char *const args[] = {"/usr/bin/echo", "Hello world!", NULL};

   execv(file, args);

   return 0;
}

Output

保存、编译并执行上述程序 −

Save, compile, and execute the above program −

$ gcc hello.c -o hello
$ ./hello
Hello world!

5. execvp() Function

execvp() 具有以下语法 -

The execvp() has the following syntax −

int execvp(const char *file, char *const argv[])

请看以下示例:

Take a look at the following example −

#include <unistd.h>

int main(void) {

   char *file = "echo";
   char *const args[] = {"/usr/bin/echo", "Hello world!", NULL};

   execvp(file, args);

   return 0;
}

Output

保存、编译并执行上述程序 −

Save, compile, and execute the above program −

$ gcc hello.c -o hello
$ ./hello
Hello world!

6. execve() Function

除环境变量外,我们可以将其他参数作为 NULL 终止的向量传给 execve() 函数 -

In addition to environment variables, we can pass other arguments to execve() function as a NULL-terminated vector −

int execve(const char *pathname, char *const argv[], char *const envp[])

请看以下示例:

Take a look at the following example −

#include <unistd.h>

int main(void) {

   char *file = "/usr/bin/bash";
   char *const args[] = {"/usr/bin/bash", "-c", "echo Hello $ENV!", NULL};
   char *const env[] = {"ENV=World", NULL};

   execve(file, args, env);

   return 0;
}

Output

保存、编译并执行上述程序 −

Save, compile, and execute the above program −

$ gcc hello.c -o hello
$ ./hello
Hello world!