简体   繁体   English

关于STDIN,STDOUT,STDERR和返回值

[英]About STDIN,STDOUT,STDERR and return value

I can redirect the data stream by using < > and 2> for STDIN , STDOUT and STDERR . 我可以通过对STDINSTDOUTSTDERR使用< >2>来重定向数据流。

Somehow I'm confused them with return value. 不知何故,我把它们与返回值混淆了。

If return value is 1 or 2 how to understand? 如果返回值为12如何理解?

If return value is -1 how to understand? 如果返回值为-1怎么理解?

break can exit a loop but keep execute the following code, how about return ? break可以退出循环,但继续执行以下代码, return怎么样? Once see return , it will terminate the main() with the return code, correct? 一旦看到return ,它将用返回码终止main() ,对吗?

To answer this, let's refer to the C99 standard. 为了回答这个问题,让我们参考C99标准。 If you read on 7.20.4.3 The exit function , you'll find the following statement: 如果您阅读7.20.4.3退出函数 ,则会发现以下语句:

5 Finally, control is returned to the host environment. 5最后,控制权返回到主机环境。 If the value of status is zero or EXIT_SUCCESS , an implementation-defined form of the status successful termination is returned. 如果status的值为零或EXIT_SUCCESS ,则返回实现定义的状态成功终止的形式。 If the value of status is EXIT_FAILURE , an implementation-defined form of the status unsuccessful termination is returned. 如果status值为EXIT_FAILURE ,则返回实现定义的状态,表示状态失败终止。 Otherwise the status returned is implementation-defined. 否则,返回的状态是实现定义的。

What should be emphasized here is that the C standard doesn't specify any specific codes for exit status. 这里应该强调的是,C标准没有为退出状态指定任何特定的代码。 While in the snippet above you can see zero mentioned, it's purely part of the exit() API. 在上面的代码段中,您可以看到提到的 ,但这纯粹是exit() API的一部分。 Rephrasing the remainder of the sentence, that zero can be mapped onto any other implementation-defined successful termination. 改写句子的其余部分,可以将映射到任何其他由实现定义的成功终止上。 Similarly, there's no guarantee that the definitions of EXIT_SUCCESS and EXIT_FAILURE won't be mapped onto other codes. 同样,不能保证EXIT_SUCCESSEXIT_FAILURE的定义不会映射到其他代码上。 In particular, it may not even end up as an integer. 特别是,它甚至可能不会以整数结尾。

Furthermore, if you look at 7.20.4.6 The system function , you'll notice: 此外,如果您查看7.20.4.6系统功能 ,则会注意到:

Returns 3 If the argument is a null pointer, the system function returns nonzero only if a command processor is available. 返回3如果参数为空指针,则仅当命令处理器可用时,系统函数才返回非零。 If the argument is not a null pointer, and the system function does return, it returns an implementation-defined value. 如果参数不是空指针,并且系统函数确实返回,则它返回实现定义的值。

Which confirms that exit codes are purely implementation-defined, and you can't rely on any particular behavior. 这确认了退出代码纯粹是实现定义的,并且您不能依赖任何特定的行为。


Now, POSIX extends this a bit. 现在, POSIX对此进行了扩展。 If you read on the stdlib.h header, you'd find: 如果您阅读stdlib.h标头,则会发现:

EXIT_FAILURE EXIT_FAILURE

Unsuccessful termination for exit(); exit()的终止失败; evaluates to a non-zero value. 计算为非零值。

EXIT_SUCCESS EXIT_SUCCESS

Successful termination for exit(); 成功终止exit(); evaluates to 0. 评估为0。

This already restricts the implementations to use 0 for a successful exit. 这已经将实现限制为成功退出使用0 However, it doesn't really define which of the non-zero values is used for failure. 但是,它并没有真正定义哪个非零值用于失败。

If you read on exit() function, you'd find: 如果您阅读exit()函数,则会发现:

The value of status may be 0, EXIT_SUCCESS, EXIT_FAILURE, or any other value, though only the least significant 8 bits (that is, status & 0377) shall be available to a waiting parent process. 状态值可以是0,EXIT_SUCCESS,EXIT_FAILURE或任何其他值,尽管只有最低有效的8位(即状态&0377)可供等待的父进程使用。

Which also extends the C definition by guaranteeing that the exit status in range 0…255 will be made available to other processes. 通过确保将0…255范围内的退出状态提供给其他进程,这也扩展了C的定义。

So, POSIX defines zero as successful exit, EXIT_FAILURE (some non-zero value) as unsuccessful exit and leaves the remaining values for program use. 因此,POSIX将零定义为成功退出,将EXIT_FAILURE (一些非零值)定义为不成功退出,并将其余值留给程序使用。

However, if you are parsing termination codes from other processes, you should take a look at wait() function as well. 但是,如果要从其他进程解析终止代码,则还应该查看wait()函数。 It defines additional macros used to process the return status for non-normal termination statuses, such as termination by signal. 它定义了用于处理非正常终止状态(如信号终止)的返回状态的其他宏。


If we go even further, we can find that FreeBSD introduces sysexits.h header to define exit codes even further. 如果我们走得更远,我们会发现FreeBSD引入了sysexits.h标头来进一步定义退出代码。

The successful exit is always indicated by a status of 0, or EX_OK. 成功退出始终由状态0或EX_OK指示。 Error numbers begin at EX__BASE to reduce the possibility of clashing with oth er exit statuses that random programs may already return. 错误号从EX__BASE开始,以减少与随机程序可能已经返回的其他退出状态发生冲突的可能性。 The meaning of the codes is approximately as follows: 这些代码的含义大致如下:

EX_USAGE (64) The command was used incorrectly, eg, with the wrong number of arguments, a bad flag, a bad syntax in a parameter, or whatever. EX_USAGE(64)命令使用不正确,例如,参数数量错误,标志错误,参数语法错误等。 […] […]

Which means that applications written for FreeBSD may use well-defined exit codes 64+. 这意味着为FreeBSD编写的应用程序可以使用定义良好的退出代码64+。


To summarize shortly: 总结一下:

  1. The C standard defines only the API for returning successful and unsuccessful exit. C标准仅定义用于返回成功不成功退出的API。 It doesn't define how to use, pass or obtain that result. 它没有定义如何使用,传递或获得该结果。
  2. POSIX strictens that to using zero for success, and guarantees that in case of normal process termination, the 8 least significant bits of exit code will be made available to other processes (eg via wait() ). POSIX严格要求成功使用 ,并保证在正常进程终止的情况下,退出代码的8个最低有效位将可用于其他进程(例如,通过wait() )。
  3. FreeBSD reserves exit codes 64+ to create well-defined error condition exit codes. FreeBSD保留退出代码64+来创建定义良好的错误条件退出代码。

That's how far the standards go. 这就是标准的发展范围。 Now, in the reality most programs use zero to indicate plain success, and non-zero codes to indicate various other statuses. 现在,实际上,大多数程序使用来表示简单成功,而使用非零代码来表示其他各种状态。 Programs using exit codes to pass specific information often describe them in manpages. 使用退出代码传递特定信息的程序通常在手册页中对其进行描述。

For example, if you look at GNU cmp : 例如,如果您查看GNU cmp

An exit status of 0 means no differences were found, 1 means some differences were found, and 2 means trouble. 退出状态0表示没有发现差异,1表示发现了一些差异,2表示有问题。

as mentioned in comments if any command execution returns 0 then it indicates successful execution, whereas any non-zero value means error while executing that particular command. 如注释中所述,如果任何命令执行返回0,则表示成功执行,而任何非零值表示执行该特定命令时出错。 after execution of command the return value can be seen by echo $? 执行命令后,返回值可以通过echo $?看到echo $? in unix. 在Unix中。 most of the time the non zero return value indicating error, ie the return value is predefined for known errors. 大多数情况下,非零返回值指示错误,即返回值是为已知错误预定义的。 these are also called as exit codes. 这些也称为退出代码。

There has been an attempt to systematize exit status numbers (see /usr/include/sysexits.h), but this is intended for C and C++ programmers. 曾尝试对退出状态号进行系统化(请参阅/usr/include/sysexits.h),但这是供C和C ++程序员使用的。 A similar standard for scripting might be appropriate. 类似的脚本标准可能是合适的。

also

For the shell's purposes, a command which exits with a zero exit status has succeeded. 出于shell的目的,成功执行了以零退出状态退出的命令。 A non-zero exit status indicates failure. 非零退出状态表示失败。 This seemingly counter-intuitive scheme is used so there is one well-defined way to indicate success and a variety of ways to indicate various failure modes. 使用了这种看似违反直觉的方案,因此存在一种明确定义的方式来指示成功,并且存在多种方式来指示各种故障模式。 When a command terminates on a fatal signal whose number is N, Bash uses the value 128+N as the exit status. 当命令终止于编号为N的致命信号时,Bash使用值128 + N作为退出状态。

If a command is not found, the child process created to execute it returns a status of 127. If a command is found but is not executable, the return status is 126. 如果找不到命令,则为执行该命令而创建的子进程将返回状态127。如果找到但不能执行该命令,则返回状态为126。

If a command fails because of an error during expansion or redirection, the exit status is greater than zero. 如果命令由于扩展或重定向期间的错误而失败,则退出状态大于零。

All of the Bash builtins return an exit status of zero if they succeed and a non-zero status on failure, so they may be used by the conditional and list constructs. 如果所有Bash内置程序成功,则返回的退出状态为零,失败时返回的状态为非零,因此条件构造和列表构造可以使用它们。 All builtins return an exit status of 2 to indicate incorrect usage. 所有内置插件均返回退出状态2,以指示使用不正确。

MSDOS exit code is only 1 byte, and there may a prior precedent for this. MSDOS退出代码只有1个字节,因此可能有一个先例。 Windows continued this standard, limiting exit codes to the range 0 to 255. The MSDOS / Windows command interpreter stores the exit code in a command interpreter variable named ERRORLEVEL which can be used in batch files. Windows继续执行此标准,将退出代码限制在0到255之间。MSDOS/ Windows命令解释器将退出代码存储在名为ERRORLEVEL的命令解释器变量中,该变量可在批处理文件中使用。 There's also an environment variable named %ERRORLEVEL% that defaults to ERRORLEVEL, but the environment variable can be set to something other than ERRORLEVEL. 还有一个名为%ERRORLEVEL%的环境变量,默认为ERRORLEVEL,但是可以将环境变量设置为ERRORLEVEL以外的其他值。 There may be cases where the exit code is used for something like switch case statement in C, as opposed to being an error indicator. 在某些情况下,退出代码用于C中的switch case语句之类的东西,而不是作为错误指示符。 I'm not sure how exit codes can be used *nix operating systems scripts. 我不确定如何使用退出代码* nix操作系统脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM