膨胀的echo命令 [英] Bloated echo command

查看:123
本文介绍了膨胀的echo命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看"echo"命令的以下实现:

Look at the following implementations of the "echo" command:

http://bxr.su/d/bin/echo/echo.c (DragonFly)

http://bxr.su/d/bin/echo/echo.c (DragonFly)

http://bxr.su/n/bin/echo/echo.c (NetBSD)

http://bxr.su/f/bin/echo/echo.c (FreeBSD)

http://git. savannah.gnu.org/cgit/coreutils.git/tree/src/echo.c (GNU)

当您进入列表的底部时,我相信您会注意到每个实现中的膨胀都在增加. 272行回显程序的作用是什么?

As you go down the list, I'm sure you'll notice the increasing bloat in each implementation. What is the point of a 272 line echo program?

推荐答案

Pike&克尼根(Kernighan)讨论了cat程序如何增加控制参数.尽管不是该文章的某个地方,但有人评论说"cat是从伯克利挥舞旗帜回来的".这与echo开发选项的问题类似. (我在BSD(Mac OS X)手册页中找到了有关cat的相关文章的引用:Rob Pike,"UNIX风格,或cat -v被认为有害" ,USENIX夏季会议会议论文集,1983年.另请参见 http://quotes.cat-v.org/programming/ )

In their article 'Program Design in the UNIX Environment', Pike & Kernighan discuss how the cat program accreted control arguments. Somewhere, though not that article, there was a comment about 'cat came back from Berkeley waving flags'. This is a similar issue to the problem with echo developing options. (I found a reference to the relevant article in the BSD (Mac OS X) man page for cat: Rob Pike, "UNIX Style, or cat -v Considered Harmful", USENIX Summer Conference Proceedings, 1983. See also http://quotes.cat-v.org/programming/)

在Kernighan&的书"UNIX编程环境"中.派克(是的,再次是那两个)引用道格·麦克罗伊(Doug McIlroy)的主题,即回声"应该不加争论地做什么(大约1984年):

In their book 'The UNIX Programming Environment', Kernighan & Pike (yes, those two again) quote Doug McIlroy on the subject of what 'echo' should do with no arguments (circa 1984):

哲学的另一个问题是echo如果没有给出任何参数应该怎么做-具体来说,它应该打印空白行还是什么都不打印.我们知道的所有当前echo实现都空白行,但是过去的版本却没有,并且对此问题也有很多争论.道格·麦克罗伊(Doug McIlroy)在关于该主题的讨论中传达了正确的神秘感:

Another question of philosophy is what echo should do if given no arguments - specifically, should it print a blank line or nothing at all. All the current echo implementations we know print a blank line, but past versions didn't, and there were great debates on the subject. Doug McIlroy imparted the right feelings of mysticism in his discussion on the topic:

UNIX和Echo

在新泽西州新泽西州的土地上居住着一个公平的女仆,塞万特人很欣赏它.她的纯洁使她眼花azz乱,所有人都拥护她,一个是为了她的处女风度,另一个是为了她的彬彬有礼的文明,而另一个是因为她敏捷地执行严格的任务,即使在更富裕的土地上也很少能完成.她是如此的宽容和宽容,以至于UNIX采纳了她的追求者中最富有的人.很快,许多后代生长繁衍并传播到了地球的尽头.

There dwelt in the land of New Jersey the UNIX, a fair maid whom savants travelled far to admire. Dazzled by her purity, all sought to espouse her, one for her virginal grace, another for her polished civility, yet another for her agility in performing exacting tasks seldom accomplished even in much richer lands. So large of heart and accommodating of nature was she that the UNIX adopted all but the most insufferably rich of her suitors. Soon, many offspring grew and prospered and spread to the ends of the earth.

与其他凡人相比,自然界自己更热切地微笑着对UNIX做出了回应.谦卑的人,几乎不懂礼貌,对她的回声感到高兴,他们如此精确和晶莹剔透,以至于他们不敢相信她会被同样的岩石和树林所回答,而这些岩石和树林使自己的呼喊声杂乱无章地传到了旷野. .兼容的UNIX要求对她的要求进行完美的回应.

Nature herself smiled and answered to the UNIX more eagerly than to other mortal beings. Humbler folk, who knew little of more courtly manners, delighted in her echo, so precise and crystal clear they scarce believed she could be answered by the same rocks and woods that so garbled their own shouts into the wilderness. And the compliant UNIX obliged with perfect echoes of whatever she was asked.

当一个不耐烦的人问UNIX,什么也不要回声"时,UNIX不得不张开嘴,什么也没有回声,然后再次合上.

When one impatient swain asked the UNIX, 'Echo nothing', the UNIX obligingly opened her mouth, echoed nothing, and closed it again.

青年问道,你的意思是什么?"张开嘴吗?从此以后,当您应该不回声的时候,永远不要张开嘴!"而UNIX是必须的.

'Whatever do you mean,' the youth demanded, 'opening your mouth like that? Henceforth never open your mouth when you are supposed to echo nothing!' And the UNIX obliged.

但是我想要一个完美的演奏,即使你什么也没回声,"一个敏感的青年恳求道,闭着嘴也不会产生完美的回声."不希望冒犯任何一个,UNIX同意对不耐烦的年轻人和不敏感的年轻人说些不同的话.她称敏感为"\n".

'But I want a perfect performance, even when you echo nothing,' pleaded a sensitive youth, 'and no perfect echoes can come from a closed mouth.' Not wishing to offend either one, the UNIX agreed to say different nothings for the impatient youth and the insensitive youth. She called the sensitive nothing '\n'.

现在,当她说"\n"时,她并没有真正说出什么,因此不得不张开嘴两次,一次是说"\n",一次是什么也没说,所以她不喜欢敏感的年轻人马上说:"\n听起来对我来说没有什么完美的东西,但是第二个却毁了它.我要你收回其中一个.因此,无法忍受冒犯的UNIX同意撤销一些回声,并将其称为"\c".现在,敏感的年轻人可以通过一起请求'\n'和'\c'来听到一无所获的完美回声.但是他们说他在听到之前就死于多余的记号.

Yet now when she said '\n', she was not really saying nothing so she had to open her mouth twice, once to say '\n' and once to say nothing, and so she did not please the sensitive youth, who said forthwith, 'The \n sounds like a perfect nothing to me, but the second one ruins it. I want you to take back one of them.' So the UNIX, who could not abide offending, agreed to undo some echoes, and called that '\c'. Now the sensitive youth could hear a perfect echo of nothing by asking for '\n' and '\c' together. But they say that he died of a surfeit of notation before he ever heard one.


Korn shell引入了(或至少包括了)基于C语言printf()函数的printf命令,该命令使用格式字符串控制材料的显示方式.与echo相比,它是用于复杂格式的更好的工具.但是,由于引用中概述了历史记录,所以echo不再回显了;它解释了要给予回声的东西.


The Korn shell introduced (or, at least, included) a printf command that was based on the C language printf() function, and that uses a format string to control how the material should appear. It is a better tool for complicated formatting than echo. But, because of the history outlined in the quote, echo doesn't just echo any more; it interprets what it is given to echo.

并且毫无疑问地解释echo的命令行参数比不解释它们需要更多的代码.基本的回显命令是:

And interpreting the command line arguments to echo indubitably requires more code than not interpreting them. A basic echo command is:

#include <stdio.h>
int main(int argc, char **argv)
{
    const char *pad = "";
    while (*++argv)
    {
        fputs(pad, stdout);
        fputs(*argv, stdout);
        pad = " ";
    }
    fputc('\n', stdout);
    return 0;
}

还有其他方法可以实现这一目标.但是echo的更复杂版本在打印任何内容之前必须仔细检查其参数-这需要更多代码.而且不同的系统决定他们要对参数进行不同数量的解释,从而导致不同数量的代码.

There are other ways to achieve that. But the more complex versions of echo have to scrutinize their arguments before printing anything - and that takes more code. And different systems have decided that they want to do different amounts of interpretation of their arguments, leading to different amounts of code.

这篇关于膨胀的echo命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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