为什么shell使用0作为成功? [英] why shell uses 0 as success?

查看:30
本文介绍了为什么shell使用0作为成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C 和许多其他语言中,0 表示 false,1(或非零)意味着真实.在 shell 中 0 表示进程的状态表示成功非零表示错误.Shell if 语句本质上使用 0真的.为什么第一个 shell 的作者决定使用 0 表示真的吗?

解决方案

OP询问

<块引用>

为什么第一个 shell 的作者决定用 0 表示真?

简短的回答:无法以这种形式回答这个问题,因为第一个 shell没有任何退出状态.

编辑

因为@John Kugelman 要求进行总结,所以这个答案有点长.

第二个、第三个和第 N 个 shell 也没有任何可以称为退出状态"的东西.对于近似正确的答案,需要深入历史.

shell 被发明出来了由路易斯·普赞"作为RUNCOM"工具,在麻省理工学院的兼容分时系统阅读 CTSS,写在哪里:

<块引用>

Louis Pouzin 还为 CTSS 发明了 RUNCOM.这个设施,Unix shell 脚本的直接祖先,允许用户创建要执行的命令的文件系统文件,带有参数替代.Louis 还制作了 Multics 外壳的设计,Unix shell 的祖先.

另请阅读此页面.

下一级"在 Multics 中,称为Multics 命令语言:

<块引用>

命令语言解释器 Shell 通常由监听器.

<块引用>

它的作用是以命令行的形式监听请求在用户控制台输入.在上述命令语言中描述,侦听器从控制台读取一行,评估该行作为命令,并重新调用自身以重复该功能.

它没有任何可以称为退出状态"的东西.程序调用 terminate{file/process} 过程,什么会停止程序执行.

这是最重要的一点.需要区分

  • 作为系统调用退出当你在你调用的现实中终止一个正在运行的(编译的)程序时,(例如,在C"中使用 exit(N))库函数,是连接底层操作系统和正确终止程序的桥梁.这意味着exit(N)"(从退出函数的角度来看)依赖于操作系统实现.查看维基百科
  • shell 的退出状态 -执行命令的退出状态是 waitpid 系统调用或等效函数返回的值.维基百科.waitpid 之类的函数通常会返回前一点的退出状态,但它取决于操作系统的实现.(ofc,现在由 POSIX 标准化).

回到历史:

在 Multics 之后,UNIX 得到了发展.从这个页面我们今天所知道的 shell 有两个前身:

  • Thompson shell(版本 1 到版本 6)- 如果有人感兴趣,这里 维护了 v6 的可运行版本.
  • Programmers Work Bench [PWB] 外壳
  • 我们今天知道的第一个 shell 是 Bourne shell V7

如果有兴趣,请阅读手册 - 链接在 这里.

第一次提到退出代码";在里面PWB(又名 Mashey)Shell 手册.所有贝壳之前"只谈论:

<块引用>

终止报告

<块引用>

如果一个命令(后面没有&")异常终止,打印一条消息.(除出口外的所有终止和中断被认为是异常的.)

因此,问题的答案在上述几行中, - 符合评论中已经说过的内容:

  • 因为将退出代码读取为错误代码.0 表示没有错误.>0 一些错误.
  • 可能是因为只有一种成功模式和多种失败模式.

来自 PWB 外壳手册

的精彩引文<块引用>

$r 是前面命令的退出状态码.``0'' 是大多数命令的正常返回.

注意:most这个词.:),例如它不是一瞬间"发明的;由某人,但它是随着时间的推移而进化的.它非常依赖于底层操作系统中 exitwait-like 调用的实现.

例如,第一版 UNIX 对 exit(N) 级别一无所知 - 它是一个简单的 exit()wait 没有返回特定值 - 来自 Unix 程序员手册第一版,1971 年 11 月 3 日

<块引用>

exit 是终止进程的正常方式.所有文件都是关闭并通知父进程是否正在执行等待.

<块引用>

wait 导致其调用者延迟,直到其子进程之一终止.如果任何孩子已经死亡,立即返回;如果没有孩子,立即返回并设置错误位.

另外,阅读这里 - 真正值得 - 进化Unix分时系统 - 关于forkexecexitwait...

V7 将退出状态标准化为:

<块引用>

一个简单命令的价值在于它的如果它正常终止则退出状态或 200+状态如果它异常终止(有关状态列表,请参见信号(2)值).

还有:

<块引用>

诊断shell 检测到的错误,例如语法错误导致shell 返回非零退出状态.如果外壳是以非交互方式使用,然后执行 shell文件被放弃.否则,shell 返回出口最后执行的命令的状态(另见退出).

Howg.. OMG... 有人可能会很好地编辑这篇文章并纠正我蹩脚的英语和/或扩展/更正上述内容...

最后 - 对不起各位 - 完全是主题 - 但忍不住添加以下 图像 来自历史 - 没有多少当前用户知道 Microsoft-XENIX.;)

In C and many other languages, 0 means false and 1 ( or non-zero ) means true. In the shell 0 for the status of a process means success and non-zero means an error. Shell if statements essentially use 0 for true. Why did the writer of the first shell decide to use 0 for true?

解决方案

The OP asking about the

Why did the writer of the first shell decide to use 0 for true?

The short answer: Isn't possible to answer this question in this form, because the first shell has not any exit status.

EDIT

Because @John Kugelman asked for summarisation, this answer getting a bit long.

Nor the second, third and Nth shell hasn't anything that could be called "the exit status". For the approximative correct answer, need to go to deeper into the history.

The shell was invented by "Louis Pouzin" as the "RUNCOM" tool, in the MIT's Compatible Time-Sharing System read about CTSS, where is written:

Louis Pouzin also invented RUNCOM for CTSS. This facility, the direct ancestor of the Unix shell script, allowed users to create a file-system file of commands to be executed, with parameter substitution. Louis also produced a design for the Multics shell, ancestor of the Unix shell.

Read also this page.

The "next level" was in the Multics, called as the Multics Command Language:

The command language interpreter, the Shell, is normally driven by the Listener.

Its function is to listen for requests in the form of command lines typed in at the user console. In the above command language description, the listener reads in a line from the console, evaluates the line as a command, and re-calls itself to repeat the function.

It hasn't anything that could be called "exit status". The programs calls the terminate{file/process} procedure, what stops the program execution.

And this is one of the most important points. Need differentiate between

  • the exit as system call When you terminating a running (compiled) program, (for example, in "C" using the exit(N)) in the reality you call a library function, what makes the bridge to the underlying operating system and correctly terminates the program. This means the "exit(N)" (from the point of view of the exit function) is OS implementation-dependent. See Wikipedia
  • shell's exit status - The exit status of an executed command is the value returned by the waitpid system call or equivalent function. Wikipedia. The waitpid like functions usually returns the exit status from the previous point, but it is OS implementation-dependent. (ofc, now standardized by POSIX).

Back to the history:

After the Multics, the UNIX got developed. From this page the shell as we know today has two predecessors:

  • the Thompson shell (version 1 up to version 6) - if someone is interested, here is maintained a runnable version for the v6.
  • and the Programmers Work Bench [PWB] shell
  • the first shell that we known today was the Bourne shell V7

If interested, read through the manuals - links are here.

The first mentioning of the "exit code" is in the Manual of the PWB (aka Mashey) Shell. All shells "before" talks only about the:

Termination Reporting

If a command (not followed by "&") terminates abnormally, a message is printed. (All terminations other than exit and interrupt are considered abnormal.)

So, the answer to the question is in the above lines, - in line what is already said in the comments:

  • Because read the exit code as an error code. 0-mean no error. >0 some error.
  • Probably because there's only one mode of success and many modes of failure.

Nice citation from the PWB shell manual

$r is the exit status code of the preceding command. ``0'' is the normal return from most commands.

Notice the: most word. :), e.g. it was not invented in "one moment" by someone, but it is evolved in time. And it very tightly depends on the implementation of the exit and wait-like calls in the underlaying OS.

For example, the 1st version of UNIX know nothing about the exit(N) levels - it was a simple exit() and the wait didn't return a specific value - from the The first edition of the Unix Programmer's Manual, dated November 3, 1971

exit is the normal means of terminating a process. All files are closed and the parent process is notified if it is executing a wait.

wait causes its caller to delay until one of its child processes terminates. If any child has already died, return is immediate; if there are no children, return is immediate with the error bit set.

Also, read here - really worth - The Evolution of the Unix Time-sharing System - about the evolution of the fork, exec, exit and wait...

The V7 standardized the exit status as:

The value of a simple command is its exit status if it terminates normally or 200+status if it terminates abnormally (see signal(2) for a list of status values).

also:

DIAGNOSTICS Errors detected by the shell, such as syntax errors cause the shell to return a non-zero exit status. If the shell is being used non interactively then execution of the shell file is abandoned. Otherwise, the shell returns the exit status of the last command executed (see also exit).

Howg.. OMG... Someone could be so nice as to edit this post and correct my broken English and/or extend/correct the above...

And finally - sorry folks - totally of the topic - but can't resist adding the following image from the history - not many current users know Microsoft-XENIX. ;)

这篇关于为什么shell使用0作为成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆