C system()的返回值

#include <stdlib.h>

int system(const char *command);

* If command is NULL, then a nonzero value if a shell is available, or 0 if no shell is available.

* If a child process could not be created, or its status could not be retrieved, the return value is -1.

* If a shell could not be executed in the child process, then the return value is as though the child shell terminated by calling _exit(2) with the status 127.

* If all system calls succeed, then the return value is the termination status of the child shell used to execute command. (The termination status of a shell is the

termination status of the last command it executes.)

In the last two cases, the return value is a "wait status" that can be examined using the macros described in waitpid(2). (i.e., WIFEXITED(), WEXITSTATUS(), and so on).

system() does not affect the wait status of any other children.

https://stackoverflow.com/questions/808541/any-benefit-in-using-wexitstatus-macro-in-c-over-division-by-256-on-exit-statu

tianyxu@TIANYXU-M-V83Z glibc % git grep __WEXITSTATUS
bits/waitstatus.h:#define       __WEXITSTATUS(status)   (((status) & 0xff00) >> 8)
bits/waitstatus.h:#define       __WSTOPSIG(status)      __WEXITSTATUS(status)
posix/sys/wait.h:# define WEXITSTATUS(status)   __WEXITSTATUS (status)
stdlib/stdlib.h:# define WEXITSTATUS(status)    __WEXITSTATUS (status)

The status stored by waitpid() encodes both the reason that the child process was terminated and the exit code. The reason is stored in the least-significant byte (obtained by status & 0xff), and the exit code is stored in the next byte (masked by status & 0xff00 and extracted by WEXITSTATUS()). When the process terminates normally, the reason is 0 and so WEXITSTATUS is just equivalent to shifting by 8 (or dividing by 256). However, if the process is killed by a signal (such as SIGSEGV), there is no exit code, and you have to use WTERMSIG to extract the signal number from the reason byte.

比如如果system执行grep但是没抓住字符,它的返回值就是256,WEXITSTATUS(rc)就是1。

### 回答1: 在 Python 中,os.system(command) 函数执行指定的 shell 命令,并返回命令执行的状态码。如果命令执行成功,则返回 0,否则返回非零值。如果命令无法执行,则返回 -1。同时,该函数会直接在终端中打印出命令的输出结果。如果需要获取命令的输出结果,可以使用 subprocess 模块中的函数。 ### 回答2: os.system()是Python中用于执行系统命令的函数,它会调用操作系统的shell来执行指定的命令,并返回一个表示执行结果的返回值。 当os.system()执行成功时,返回值为0,表示命令执行成功完成。这意味着系统命令已经顺利执行,并且不会返回任何错误信息。 如果os.system()执行失败,返回值为非零整数。返回值通常可以表示不同类型的错误或异常情况。具体的返回值与不同的操作系统和执行的命令有关。 需要注意的是,os.system()只返回命令的退出状态码,而不包括命令的输出结果。如果需要获取命令的输出结果,可以使用其他方法,比如subprocess模块中的函数。 总之,os.system()返回值可以让我们判断执行命令是否成功,以及获取执行结果的状态信息。 ### 回答3: os.system()函数是Python标准库中的一个函数,它用于执行操作系统的命令。它接受一个命令作为参数,并在操作系统中执行该命令。该函数的返回值是命令执行的状态码。 当命令成功执行时,os.system()函数的返回值为0。这表示命令执行成功并且没有出现错误。如果命令执行失败或者出现错误,os.system()函数的返回值将是一个非零的整数。这个返回值有助于我们判断命令执行的结果。 我们可以根据os.system()返回值来判断命令是否执行成功。例如,如果我们执行一个命令并希望根据结果进行不同的操作,我们可以使用条件语句来判断os.system()返回值。 需要注意的是,os.system()函数在某些操作系统上可能会以不同的方式工作。因此,在使用这个函数时,我们需要考虑到不同操作系统的差异带来的影响。 为了更好地控制执行命令的结果,我们还可以使用subprocess模块中的更强大的函数来代替os.system()函数。subprocess模块提供了更高级的接口,可以更灵活和安全地执行系统命令,并获取其输出及错误信息。 总而言之,os.system()函数返回的是命令执行的状态码,用于判断命令是否执行成功。我们可以根据这个返回值来进行后续的操作,并注意不同操作系统的差异。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值