Bash (Bash)、Z shell (zsh)、Fish (Fish)等shell语言和上面的脚本语言之间有什么区别,使它们更适合于shell?

在使用命令行时,shell语言似乎要容易得多。例如,对我来说,使用bash比在IPython中使用shell配置文件要流畅得多,尽管报告与此相反。我想大多数人都会同意我的观点,在Python中进行大部分中型到大型的编程比在Bash中更容易。我使用Python作为我最熟悉的语言。Perl和Ruby也是如此。

我试着阐明原因,但我不能,除了假设两者对字符串的不同处理与此有关。

提出这个问题的原因是我希望开发一种两者都可用的语言。如果你知道这样的语言,也请贴出来。

正如s .洛特所解释的,这个问题需要一些澄清。我问的是shell语言和脚本语言的特性。因此,比较不是关于各种交互(REPL)环境的特征,如历史记录和命令行替换。这个问题的另一种表达方式是:

适合于复杂系统设计的编程语言是否能够同时表达可以访问文件系统或控制作业的有用的一行程序?一种编程语言能有效地扩展和缩小吗?


当前回答

由于这两种语言都是正式的编程语言,所以在一种语言中可以做的事情,在另一种语言中也可以做。实际上,这是一个设计重点问题。Shell语言是为交互使用而设计的,而脚本语言不是。

The basic difference in the design is the storage of data between commands and the scope of variables. In Bash, etc. you have to jump through hoops to store a value (for example, commands like set a='something'), while in languages like Python you simply use an assignment statement (a = 'something'). When using the values in a shell language you have to tell the language that your want the value of the variable, while in scripting languages you have to tell the language when you want the immediate value of the string. This has effects when used interactively.

在脚本语言中,ls被定义为命令

a = some_value

ls a*b  

(a是什么意思?这意味着some_value *(不管b是什么)还是你的意思 “'anystring b ?。在脚本语言中,默认值是存储在内存中的。)

ls 'a*b'  Now means what the Unix ls a*b means.

用类似bash的语言

set a=some_value

ls a*b   means what the Unix ls a*b means.

ls $a*b  uses an explicit recall of the value of a.

脚本语言使得存储和检索值变得容易,而值上的瞬态作用域却很难实现。Shell语言可以存储和调用值,但是每个命令的作用域都是短暂的。

其他回答

谁说他们不是?看看Zoidberg。REPLs (Read Eval Print Loops)会产生糟糕的shell,因为每个命令都必须在语法上正确,运行一个程序会从:

foo arg1 arg2 arg3

to

system "foo", "arg1", "arg2", "arg3"

更不要让我开始尝试重定向。

因此,您需要一个自定义shell(而不是REPL)来理解命令和重定向,以及您希望使用的将命令绑定在一起的语言。我认为zoid (Zoidberg shell)在这方面做得很好。

对于Windows用户,我还没有感觉到对PowerShell的需求,因为我仍然使用JP Software的4NT(现在是Take Command Console)。它是一个非常好的shell,具有很多编程功能。所以它结合了两个世界的优点。

例如,当您研究IRB (Ruby解释器)时,一定很有可能使用更多的一行程序来扩展它,以执行日常脚本化或大规模文件管理,以及处理分钟级任务。

这些答案激励我接管基于perl的shell Zoidberg的维护工作。经过一些修复后,它又可以使用了!

查看用户指南或使用您最喜欢的CPAN客户端安装Bundle::Zoidberg。

你在回避这个问题。并不是每个人都认为shell语言更好。首先,_为什么不呢

Not long ago a friend asked me how to recursively search his PHP scripts for a string. He had a lot of big binary files and templates in those directories that could have really bogged down a plain grep. I couldn't think of a way to use grep to make this happen, so I figured using find and grep together would be my best bet. find . -name "*.php" -exec grep 'search_string' {} \; -print Here's the above file search reworked in Ruby: Dir['**/*.php'].each do |path| File.open( path ) do |f| f.grep( /search_string/ ) do |line| puts path, ':', line end end end Your first reaction may be, "Well, that's quite a bit wordier than the original." And I just have to shrug and let it be. "It's a lot easier to extend," I say. And it works across platforms.

它的文化。伯恩的外壳已经有将近25年的历史了;它是最早的脚本语言之一,也是Unix管理员核心需求的第一个很好的解决方案。(例如,将所有其他实用程序捆绑在一起的“胶水”,可以执行典型的Unix任务,而不必每次都编译一个该死的C程序。)

By modern standards, its syntax is atrocious and its weird rules and punctuation-as-statement style (useful in the 1970s when every byte counted) make it hard for non-admins to penetrate it. But it did the job. The flaws and shortcomings were addressed by evolutionary improvements in its descendants (ksh, bash, zsh) without having to reconceive the ideas behind it. Admins stuck to the core syntax because, weird as it was, nothing else handled the simple stuff better without getting in the way.

For complex stuff, Perl came along and morphed into a sort of half-admin, half-application language. But the more complex something gets, the more it's seen as an application rather than admin work, so the business people tend to look for "programmers" rather than "admins" to do it, despite the fact that the right kind of geek tends to be both. So that's where the focus went, and the evolutionary improvements to the application capabilities of Perl resulted in...well, Python and Ruby. (That's an oversimplification, but Perl was one of several inspirations for both languages.)

结果呢?专业化。管理员们倾向于认为现代解释性语言对于他们每天的工作来说太重量级了。总的来说,他们是对的。他们不需要对象。他们不关心数据结构。他们需要命令。他们需要胶水。没有什么比Bourne shell概念更好地尝试执行命令了(除了Tcl,这里已经提到过);伯恩已经足够好了。

Programmers -- who nowadays are having to learn about devops more and more -- look at the limitations of the Bourne shell and wonder how the hell anyone could put up with it. But the tools they know, while they certainly lean towards the Unixish style of I/O and file operations, aren't better for the purpose. I've written things like backup scripts and file renaming one-offs in Ruby, because I know it better than I know bash, but any dedicated admin could do the same thing in bash -- probably in fewer lines and with less overhead, but either way, it'd work just as well.

人们经常会问“为什么每个人都用Y,而Z更好?”——但科技的进化,就像其他事物的进化一样,往往止步于足够好。“更好的”解决方案不会获胜,除非这种差异被视为一种破坏交易的挫败感。伯恩式脚本可能会让您感到沮丧,但对于一直使用它的人以及它所用于的工作来说,它总是能完成工作。