std::system - C/C++ 执行命令行

std::system - C/C++ 执行命令行

Defined in header <cstdlib>

int system(const char *command);

1. Execute system command - 执行系统命令

Invokes the command processor to execute a command.
调用命令行处理器执行 command

Calls the host environment’s command processor (e.g. /bin/sh, cmd.exe, command.com) with the parameter command. Returns an implementation-defined value (usually the value that the invoked program returns).
以参数 command 调用运行环境的命令行处理器 (例如 /bin/sh, cmd.exe, command.com)。返回相应实现的定义值 (通常是被调用程序所返回的值)。

If command is a null pointer, checks if the host environment has a command processor and returns a nonzero value if and only if the command processor exists.

If command is a null pointer, the function only checks whether a command processor is available through this function, without invoking any command.
如果 command 是空指针,则检查运行环境是否有命令行处理器,而不调用任何命令。当且仅当命令行处理器存在才返回非零。

The effects of invoking a command depend on the system and library implementation, and may cause a program to behave in a non-standard manner or to terminate.
调用命令的效果取决于系统和库的实现,并且可能导致程序以非标准方式运行或终止。

2. Parameters

command
character string identifying the command to be run in the command processor. If a null pointer is given, command processor is checked for existence.
标识要运行于命令处理器的命令字符串。若给出空指针,则检查命令行处理器的是否存在。

C-string containing the system command to be executed.
包含要执行的系统命令的 C-string

Or, alternatively, a null pointer, to check for a command processor.
或者,可以使用 null pointer 来检查命令行处理器。

3. Return value

Implementation-defined value. If command is a null pointer, returns a nonzero value if and only if the command processor exists.
实现定义值。若 command 为空指针,则当且仅当命令行处理器存在才返回非零值。

If command is a null pointer, the function returns a non-zero value in case a command processor is available and a zero value if it is not.
如果 command 是空指针,则在命令处理器可用的情况下该函数将返回非零值,否则将返回零。

If command is not a null pointer, the value returned depends on the system and library implementations, but it is generally expected to be the status code returned by the called command, if supported.
如果 command 不是空指针,则返回的值取决于系统和库的实现,但通常应将其视为调用的命令返回的状态码 (如果支持)。

Notes
On POSIX systems, the return value can be decomposed using WEXITSTATUS and WSTOPSIG
POSIX 系统上,可用 WEXITSTATUSWSTOPSIG 分解返回值。

The related POSIX function popen makes the output generated by command available to the caller.
相关的 POSIX 函数 popen 使调用方可获取到 command 生成的输出。

An explicit flush of std::cout is also necessary before a call to std::system, if the spawned process performs any screen I/O.

spawn [spɔːn]:v. 产卵,导致,引发,引起 n. 卵

4. Example

4.1 Example

//============================================================================
// Name        : Yongqiang Cheng
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <cstdlib>
#include <fstream>
#include <iostream>

const int len = 3 * 4;

int main() {

    int ret = std::system("ls -l > test.txt");  // "ls -l > test.txt"
    std::cout << "ret = " << ret << std::endl;

    std::cout << "Hello, World!\n" << std::endl;
    std::cout << std::ifstream("test.txt").rdbuf();

    return 0;
}

/home/yongqiang/CLionProjects/hash_table/cmake-build-debug/hash_table
ret = 0
Hello, World!

total 124
-rw-r--r-- 1 yongqiang yongqiang 45597 Mar  4 23:12 CMakeCache.txt
drwxr-xr-x 1 yongqiang yongqiang   512 Mar  4 23:16 CMakeFiles
-rw-r--r-- 1 yongqiang yongqiang  5124 Mar  4 23:12 Makefile
-rw-r--r-- 1 yongqiang yongqiang  1549 Dec  1 00:43 cmake_install.cmake
-rwxr-xr-x 1 yongqiang yongqiang 33760 Mar  4 23:16 hash_table
-rw-r--r-- 1 yongqiang yongqiang  5799 Mar  4 23:12 hash_table.cbp
-rw-r--r-- 1 yongqiang yongqiang     0 Mar  4 23:16 test.txt

Process finished with exit code 0

4.2 Example

//============================================================================
// Name        : Yongqiang Cheng
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>      /* printf */
#include <stdlib.h>     /* system, NULL, EXIT_FAILURE */

int main() {
    printf("Checking if processor is available...");

    if (system(NULL)) {
        puts("Ok");
    } else {
        exit(EXIT_FAILURE);
    }

    printf("\nExecuting command DIR...\n");
    int i = system("dir");

    printf("\nThe value returned was: %d.\n", i);

    return 0;
}

/home/yongqiang/CLionProjects/hash_table/cmake-build-debug/hash_table
Checking if processor is available...Ok

Executing command DIR...
CMakeCache.txt	Makefile	     hash_table      test.txt
CMakeFiles	cmake_install.cmake  hash_table.cbp

The value returned was: 0.

Process finished with exit code 0

4.3 mkdir - rm - mv

//============================================================================
// Name        : Yongqiang Cheng
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <cstdlib>
#include <fstream>
#include <iostream>

const int len = 3 * 4;

void system_command_mkdir(const std::string dir) {
    int ret = std::system(("mkdir -p " + dir).c_str());

    if (ret != 0) {
        std::cout << "The value returned was: " << ret << ".\n" << std::endl;
    }
}

void system_command_rm(const std::string dir) {
    int ret = std::system(("rm -rf " + dir).c_str());

    if (ret != 0) {
        std::cout << "The value returned was: " << ret << ".\n" << std::endl;
    }
}

void system_command_mv(const std::string src, const std::string dst) {
    int ret = std::system(("mv " + src + " " + dst).c_str());

    if (ret != 0) {
        std::cout << "The value returned was: " << ret << ".\n" << std::endl;
    }
}

int main() {
    int ret = std::system("ls -l");  // "ls -l"
    std::cout << "\nret = " << ret << std::endl << std::endl;

    std::string dir = "yongqiang";
    system_command_mkdir(dir);

    ret = std::system("ls -l");  // "ls -l"
    std::cout << "\nret = " << ret << std::endl << std::endl;

    system_command_rm(dir);

    ret = std::system("ls -l");  // "ls -l"
    std::cout << "\nret = " << ret << std::endl << std::endl;

    system_command_mkdir(dir);

    std::string dst_dir = "yongqiangcheng";
    system_command_mv(dir, dst_dir);

    ret = std::system("ls -l");  // "ls -l"
    std::cout << "\nret = " << ret << std::endl << std::endl;

    std::cout << "Hello, World!\n" << std::endl;

    return 0;
}

/home/yongqiang/CLionProjects/hash_table/cmake-build-debug/hash_table
total 164
-rw-r--r-- 1 yongqiang yongqiang 45597 Mar  4 23:12 CMakeCache.txt
drwxr-xr-x 1 yongqiang yongqiang   512 Mar  5 22:45 CMakeFiles
-rw-r--r-- 1 yongqiang yongqiang  5124 Mar  4 23:12 Makefile
-rw-r--r-- 1 yongqiang yongqiang  1549 Dec  1 00:43 cmake_install.cmake
-rwxr-xr-x 1 yongqiang yongqiang 74608 Mar  5 22:45 hash_table
-rw-r--r-- 1 yongqiang yongqiang  5799 Mar  4 23:12 hash_table.cbp
-rw-r--r-- 1 yongqiang yongqiang   464 Mar  4 23:16 test.txt

ret = 0

total 164
-rw-r--r-- 1 yongqiang yongqiang 45597 Mar  4 23:12 CMakeCache.txt
drwxr-xr-x 1 yongqiang yongqiang   512 Mar  5 22:45 CMakeFiles
-rw-r--r-- 1 yongqiang yongqiang  5124 Mar  4 23:12 Makefile
-rw-r--r-- 1 yongqiang yongqiang  1549 Dec  1 00:43 cmake_install.cmake
-rwxr-xr-x 1 yongqiang yongqiang 74608 Mar  5 22:45 hash_table
-rw-r--r-- 1 yongqiang yongqiang  5799 Mar  4 23:12 hash_table.cbp
-rw-r--r-- 1 yongqiang yongqiang   464 Mar  4 23:16 test.txt
drwxr-xr-x 1 yongqiang yongqiang   512 Mar  5 22:45 yongqiang

ret = 0

total 164
-rw-r--r-- 1 yongqiang yongqiang 45597 Mar  4 23:12 CMakeCache.txt
drwxr-xr-x 1 yongqiang yongqiang   512 Mar  5 22:45 CMakeFiles
-rw-r--r-- 1 yongqiang yongqiang  5124 Mar  4 23:12 Makefile
-rw-r--r-- 1 yongqiang yongqiang  1549 Dec  1 00:43 cmake_install.cmake
-rwxr-xr-x 1 yongqiang yongqiang 74608 Mar  5 22:45 hash_table
-rw-r--r-- 1 yongqiang yongqiang  5799 Mar  4 23:12 hash_table.cbp
-rw-r--r-- 1 yongqiang yongqiang   464 Mar  4 23:16 test.txt

ret = 0

total 164
-rw-r--r-- 1 yongqiang yongqiang 45597 Mar  4 23:12 CMakeCache.txt
drwxr-xr-x 1 yongqiang yongqiang   512 Mar  5 22:45 CMakeFiles
-rw-r--r-- 1 yongqiang yongqiang  5124 Mar  4 23:12 Makefile
-rw-r--r-- 1 yongqiang yongqiang  1549 Dec  1 00:43 cmake_install.cmake
-rwxr-xr-x 1 yongqiang yongqiang 74608 Mar  5 22:45 hash_table
-rw-r--r-- 1 yongqiang yongqiang  5799 Mar  4 23:12 hash_table.cbp
-rw-r--r-- 1 yongqiang yongqiang   464 Mar  4 23:16 test.txt
drwxr-xr-x 1 yongqiang yongqiang   512 Mar  5 22:45 yongqiangcheng

ret = 0

Hello, World!


Process finished with exit code 0

5. Data races - 数据竞争

The function accesses the array pointed by command.
该函数访问命令指向的数组。

Concurrently calling this function with a null pointer as argument is safe. Otherwise, it depends on the system and library implementation.
使用空指针作为参数同时调用此函数是安全的。否则,它取决于系统和库的实现。

6. Exceptions (C++) - 异常

No-throw guarantee: this function does not throw exceptions.
不抛出异常保证:这个函数不会抛出异常。

If command is not a null pointer, it causes undefined behavior.
如果 command 不是 null pointer,则会导致未定义的行为。

References

https://en.cppreference.com/w/cpp/utility/program/system
http://www.cplusplus.com/reference/cstdlib/system/

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值