C 语言中的命令执行
C 语言中的命令执行
C 语言中的命令执行 用于使用 C 程序执行系统命令。系统命令是通过使用 system() 函数 执行的,该函数是 stdlib.h 头文件 的库函数。
通过使用 system() 函数,您可以在 C 语言程序中执行 Windows/Linux 终端命令。
语法
以下是执行系统命令的语法 -
system(char *command);
命令执行示例
以下代码展示了如何使用 C 语言中的 system() 函数执行 ls 命令。
#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; }
输出
运行代码并检查其输出 −
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
C 语言中的 exec 函数族
exec 函数族已在"unistd.h"头文件中引入。这些函数用于执行文件,并在调用后将当前进程映像替换为新的进程映像。
以下是 C 语言中 exec 函数族的函数 -
- execl() 函数
- execlp() 函数
- execle() 函数
- execv() 函数
- execvp() 函数
- execve() 函数
1. execl() 函数
execl() 函数的第一个参数是可执行文件。接下来的参数将在文件执行时生效。最后一个参数必须为 NULL。
int execl(const char *pathname, const char *arg, ..., NULL)
示例
请看以下示例 -
#include <unistd.h> int main(void) { char *file = "/usr/bin/echo"; char *arg1 = "Hello world!"; execl(file, file, arg1, NULL); return 0; }
Linux 中的 echo 命令正在通过 C 代码调用。
输出
保存、编译并执行上述程序 -
$ gcc hello.c -o hello $ ./hello Hello world!
2. execlp() 函数
execl() 函数类似于 execl() 函数。它使用 PATH 环境变量来定位文件。因此,无需提供可执行文件的路径。
int execlp(const char *file, const char *arg, ..., NULL)
示例
查看以下示例 -
#include <unistd.h> int main(void) { char *file = "echo"; char *arg1 = "Hello world!"; execlp(file, file, arg1, NULL); return 0; }
输出
此处,echo 已位于 PATH 环境变量中。保存、编译并从终端运行。
$ gcc hello.c -o hello $ ./hello Hello world!
3. execle() 函数
在 execle() 函数中,我们可以将环境变量传递给该函数,它会使用它们。它的原型如下 -
int execle(const char *pathname, const char *arg, ..., NULL, char *const envp[])
示例
查看以下示例 -
#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; }
输出
保存、编译并从终端运行 -
$ gcc hello.c -o hello $ ./hello Hello world!
4. execv() 函数
execv() 函数接收一个可执行文件可用的参数向量。此外,向量的最后一个元素必须为 NULL:
int execv(const char *pathname, char *const argv[])
示例
查看以下示例 -
#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; }
输出
保存、编译并执行上述程序 -
$ gcc hello.c -o hello $ ./hello Hello world!
5. execvp() 函数
execvp() 的语法如下 -
int execvp(const char *file, char *const argv[])
示例
查看以下示例 -
#include <unistd.h> int main(void) { char *file = "echo"; char *const args[] = {"/usr/bin/echo", "Hello world!", NULL}; execvp(file, args); return 0; }
输出
保存、编译并执行上述程序 -
$ gcc hello.c -o hello $ ./hello Hello world!
6. execve() 函数
除了环境变量之外,我们还可以将其他参数作为以 NULL 结尾的向量传递给 execve() 函数 -
int execve(const char *pathname, char *const argv[], char *const envp[])
示例
查看以下示例 -
#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; }
输出
保存、编译并执行上述程序 -
$ gcc hello.c -o hello $ ./hello Hello world!