#include <stdio.h>
#include <stdlib.h>
int main() {
// 使用 system 函数执行 shell 命令 "ls"
int ret = system("ls");
// 检查 system 函数的返回值
if (ret == -1) {
printf("system() function failed\n");
} else if (WIFEXITED(ret)) {
printf("Command exited with status %d\n", WEXITSTATUS(ret));
}
return 0;
}
#include <stdio.h>
和 #include <stdlib.h>
:包含标准输入输出库和标准库,system
函数在 stdlib.h
中声明。system("ls")
:调用 system
函数执行 shell 命令 "ls"
,该命令会列出当前目录下的文件和文件夹。if (ret == -1)
:检查 system
函数是否失败。如果返回值为 -1
,表示函数调用失败。WIFEXITED(ret)
和 WEXITSTATUS(ret)
:用于检查子进程是否正常退出,并获取其退出状态码。上一篇:查看linuxcpu核数
下一篇:linux 查找nginx目录
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站