为什么以及何时使用shell而不是Ruby

时间:2022-04-28 11:52:02

Since I am conversant in Ruby, I am about to script a few things on OSX using it. But then I thought, perhaps I am missing the boat. I know a lot of reasons to prefer Ruby over Bash (or whatever sh-compatible command language interpreter), but I don't know any reasons not to. What is the upside of programming the shell directly?

由于我熟悉Ruby,所以我打算在OSX上使用它编写一些脚本。但后来我想,也许我错过了船。我知道有很多理由更喜欢Ruby而不是Bash(或者任何兼容的命令语言解释器),但是我不知道有什么理由不这样做。直接编写shell的好处是什么?

I intend to take advantage of system commands using system whenever necessary.

我打算利用系统命令在任何必要的时候使用系统命令。

Note: I already know that Ruby won't always be there, but I'm interested in mostly technical, semantic and syntactic criteria.

注意:我已经知道Ruby并不总是存在,但我主要对技术、语义和语法标准感兴趣。

Edit: By Ruby not always being there, I mean that it is not a standard part of all *nix distributions, unlike vi.

编辑:Ruby并不总是存在,我的意思是它不是所有*nix发行版的标准部分,不像vi。

8 个解决方案

#1


19  

The shell's programming language is awful for all but one thing.

shell的编程语言非常糟糕,只有一件事除外。

Pipelines.

管道。

The shell's programming language for pipelines totally rocks.

shell为管道编写的编程语言非常出色。

The |, & and ; operators, plus () and ``` form a tidy little language for describing pipelines.

|和;操作符+()和' ' ' '构成了描述管道的一种简洁的小语言。

a & b is concurrent

a和b是并发的

a ; b is sequential

一个;b是顺序

a | b is a pipeline where a feeds b

| b是a供给b的管道

That part of shell programming rocks.

这是shell编程的一部分。

Think of ( a & b & c ) | tee capture | analysis as the kind of thing that's hard to express in Python (or Ruby). You can do much of this with iterpipes, but not quite all of it.

(a & b & c) | tee捕获|分析是一种很难在Python(或Ruby)中表达的分析。对于迭代管道,您可以做很多这样的事情,但不是全部。

Much of the rest you can live without, and use Python (or Ruby) and you'll be happier and more productive.

剩下的大部分都可以不用了,使用Python(或Ruby),你会更快乐、更高效。

The biggest thing to watch out for is anything involving expr at the shell level. As soon as you start trying to do "calculations", you've crossed out of the shell's sweet spot and you should stop programming in the shell and rethink what you're doing.

最需要注意的是shell级别的expr。一旦您开始尝试进行“计算”,您就已经越过了shell的最佳位置,您应该停止在shell中编程,重新考虑您正在做什么。

#2


6  

Ruby has a massive advantage: you know Ruby and (I assume) you don't know Bash that well!

Ruby有一个巨大的优势:你知道Ruby和(我猜)你不太了解Bash !

Personally I use Ruby for complex scripts and Bash for simple ones -- the breakpoint for me is usually anything that actually has a proper set of command line parameters -- but then, I know both Bash and Ruby.

就我个人而言,我将Ruby用于复杂的脚本,将Bash用于简单的脚本——对我来说,断点通常是任何实际具有适当的命令行参数集的东西——但是,我知道Bash和Ruby。

I would advise you to use Bash for anything that is simple enough that you can work it out on the commandline beforehand, for example:

我建议您将Bash用于任何简单到可以在命令行中预先解决的东西,例如:

who |grep -i admin |cut -c10-20 

-- and use Ruby for anything else

——用Ruby做其他事情

#3


2  

The core functionality in bash is to run other command line applications. Making those programs interact with each other etc. This is not what ruby is designed for (right?).

bash的核心功能是运行其他命令行应用程序。使这些程序相互交互等等。这不是ruby设计的目的(对吧?)

#4


2  

Directly writing Posix or bash script works out well for operations that loop over lists of files. Things like

直接编写Posix或bash脚本可以很好地执行循环文件列表的操作。之类的东西

find . -name \*.htm | while read x; do
   # whatever
done

The shell can do command substitution and simple parameter transformations reasonably well. Shell procedures allow fairly complex programs to be reasonably modular.

shell可以很好地执行命令替换和简单的参数转换。Shell过程允许相当复杂的程序被合理地模块化。

The transition to something like Ruby happens when some kind of internal data structure is needed. The shell is a macro processor, so it is capable of something like "metaprogramming" where you make up variables names. Some versions of bash have arrays and all versions can "metaprogram" variable names where the index is part of the name.

当需要某种内部数据结构时,就会发生类似Ruby的转换。shell是一个宏处理器,因此它可以像“元编程”那样创建变量名。bash的一些版本具有数组,并且所有版本都可以在索引是名称的一部分的地方使用“元程序”变量名。

But it's 100% hack and even the built-in arrays are crude. Once decisions have to be made and data structures retained, it's time to switch to Ruby.

但它是100%黑客,甚至内置数组都是粗糙的。一旦必须做出决定并保留数据结构,就该切换到Ruby了。

#5


1  

I don't see any problem with Ruby. You can use the back-tick instead of system and insert things inline like

我认为Ruby没有任何问题。您可以使用后勾代替系统,并插入一些内联的东西

`cp ${source} ${dest}` 

Also, you can easily get the contents of stdout (not sure about stdin) and form your own little pipelining thing.

此外,您可以轻松获取stdout的内容(不确定stdin),并形成自己的小流水线。

I think Ruby is a win for doing scripting stuff, but less so as a general shell because of the clunky bit of always having to remember to put back-ticks to execute commands.

我认为Ruby在编写脚本方面是一种胜利,但作为一个通用shell,它的优势就不那么明显了,因为在执行命令时,总是要记住放回勾。

#6


1  

The comments about how the shell handles piping are spot on. However, if you are interested in a rubyish approach to the shell you might look at rush. There are immediate observations (outside of how piping is handled) such as paths are now handled in an entirely different way, but if what you want is the ease of ruby things like iterators and blocks you have that at your fingertips.

关于外壳如何处理管道的评论是正确的。但是,如果您对使用rubyish方法处理shell感兴趣,您可以查看rush。现在有一些即时的观察(除了如何处理管道外),比如路径,是用一种完全不同的方式来处理的,但是如果您想要的是像迭代器和阻塞这样的ruby类的易用性,那么您只需掌握它们。

Likely not a full replacement in any case, but it might serve your purpose.

在任何情况下都不可能完全替代,但它可能符合你的目的。

[Edit] A quick look around turned up ipython which looks (at a cursory glance) to give more of the natural feel of a shell environment which may or may not be an incentive for you.

[编辑]快速环顾一下四周,发现了ipython,它(粗略地看一眼)提供了更多shell环境的自然感觉,这对您来说可能是,也可能不是。

#7


1  

@OP, if you want to compare Ruby vs Shell, compare using Ruby's interpreter( without libraries/modules). ie compare its in-builts against the shell. Otherwise, they are almost the same. Why? For example, if you want to do advanced maths other than those provided by the shell, the shell can use bc/awk/dc. Those are the math "libraries" for the shell. If you want date structures such as associative arrays, you can use awk. (equivalent to hashes in ruby). In modern bash shell, there are also associative arrays. You can think of *nix external tools (eg wc,grep,sed etc and those in /usr/bin/, /usr/sbin etc) as the shell's "libraries".

@OP,如果您想比较Ruby和Shell,请使用Ruby的解释器(没有库/模块)进行比较。把它的内建物与壳体作比较。否则,它们几乎是一样的。为什么?例如,如果您想做高级数学而不是shell提供的数学,shell可以使用bc/awk/dc。这些是shell的数学“库”。如果您希望使用日期结构(如关联数组),可以使用awk。(相当于ruby中的散列)。在现代bash shell中,还有关联数组。您可以将*nix外部工具(如wc、grep、sed等)视为shell的“库”。

Lastly, if you intend to use system() a lot in Ruby, i suggest using the shell as one of them mentioned, the shell excels in pipes. etc..

最后,如果您想在Ruby中大量使用system(),我建议使用shell作为其中之一,shell在管道中表现出色。等。

#8


0  

Shell's programming languages have a very small footprint and very few dependencies. Beside that I can't see much point using it. Personnaly I prefer using Perl or python for such tasks.

Shell的编程语言占用的空间非常小,依赖关系也非常少。除此之外,我看不出使用它有什么意义。我更喜欢用Perl或python来完成这些任务。

#1


19  

The shell's programming language is awful for all but one thing.

shell的编程语言非常糟糕,只有一件事除外。

Pipelines.

管道。

The shell's programming language for pipelines totally rocks.

shell为管道编写的编程语言非常出色。

The |, & and ; operators, plus () and ``` form a tidy little language for describing pipelines.

|和;操作符+()和' ' ' '构成了描述管道的一种简洁的小语言。

a & b is concurrent

a和b是并发的

a ; b is sequential

一个;b是顺序

a | b is a pipeline where a feeds b

| b是a供给b的管道

That part of shell programming rocks.

这是shell编程的一部分。

Think of ( a & b & c ) | tee capture | analysis as the kind of thing that's hard to express in Python (or Ruby). You can do much of this with iterpipes, but not quite all of it.

(a & b & c) | tee捕获|分析是一种很难在Python(或Ruby)中表达的分析。对于迭代管道,您可以做很多这样的事情,但不是全部。

Much of the rest you can live without, and use Python (or Ruby) and you'll be happier and more productive.

剩下的大部分都可以不用了,使用Python(或Ruby),你会更快乐、更高效。

The biggest thing to watch out for is anything involving expr at the shell level. As soon as you start trying to do "calculations", you've crossed out of the shell's sweet spot and you should stop programming in the shell and rethink what you're doing.

最需要注意的是shell级别的expr。一旦您开始尝试进行“计算”,您就已经越过了shell的最佳位置,您应该停止在shell中编程,重新考虑您正在做什么。

#2


6  

Ruby has a massive advantage: you know Ruby and (I assume) you don't know Bash that well!

Ruby有一个巨大的优势:你知道Ruby和(我猜)你不太了解Bash !

Personally I use Ruby for complex scripts and Bash for simple ones -- the breakpoint for me is usually anything that actually has a proper set of command line parameters -- but then, I know both Bash and Ruby.

就我个人而言,我将Ruby用于复杂的脚本,将Bash用于简单的脚本——对我来说,断点通常是任何实际具有适当的命令行参数集的东西——但是,我知道Bash和Ruby。

I would advise you to use Bash for anything that is simple enough that you can work it out on the commandline beforehand, for example:

我建议您将Bash用于任何简单到可以在命令行中预先解决的东西,例如:

who |grep -i admin |cut -c10-20 

-- and use Ruby for anything else

——用Ruby做其他事情

#3


2  

The core functionality in bash is to run other command line applications. Making those programs interact with each other etc. This is not what ruby is designed for (right?).

bash的核心功能是运行其他命令行应用程序。使这些程序相互交互等等。这不是ruby设计的目的(对吧?)

#4


2  

Directly writing Posix or bash script works out well for operations that loop over lists of files. Things like

直接编写Posix或bash脚本可以很好地执行循环文件列表的操作。之类的东西

find . -name \*.htm | while read x; do
   # whatever
done

The shell can do command substitution and simple parameter transformations reasonably well. Shell procedures allow fairly complex programs to be reasonably modular.

shell可以很好地执行命令替换和简单的参数转换。Shell过程允许相当复杂的程序被合理地模块化。

The transition to something like Ruby happens when some kind of internal data structure is needed. The shell is a macro processor, so it is capable of something like "metaprogramming" where you make up variables names. Some versions of bash have arrays and all versions can "metaprogram" variable names where the index is part of the name.

当需要某种内部数据结构时,就会发生类似Ruby的转换。shell是一个宏处理器,因此它可以像“元编程”那样创建变量名。bash的一些版本具有数组,并且所有版本都可以在索引是名称的一部分的地方使用“元程序”变量名。

But it's 100% hack and even the built-in arrays are crude. Once decisions have to be made and data structures retained, it's time to switch to Ruby.

但它是100%黑客,甚至内置数组都是粗糙的。一旦必须做出决定并保留数据结构,就该切换到Ruby了。

#5


1  

I don't see any problem with Ruby. You can use the back-tick instead of system and insert things inline like

我认为Ruby没有任何问题。您可以使用后勾代替系统,并插入一些内联的东西

`cp ${source} ${dest}` 

Also, you can easily get the contents of stdout (not sure about stdin) and form your own little pipelining thing.

此外,您可以轻松获取stdout的内容(不确定stdin),并形成自己的小流水线。

I think Ruby is a win for doing scripting stuff, but less so as a general shell because of the clunky bit of always having to remember to put back-ticks to execute commands.

我认为Ruby在编写脚本方面是一种胜利,但作为一个通用shell,它的优势就不那么明显了,因为在执行命令时,总是要记住放回勾。

#6


1  

The comments about how the shell handles piping are spot on. However, if you are interested in a rubyish approach to the shell you might look at rush. There are immediate observations (outside of how piping is handled) such as paths are now handled in an entirely different way, but if what you want is the ease of ruby things like iterators and blocks you have that at your fingertips.

关于外壳如何处理管道的评论是正确的。但是,如果您对使用rubyish方法处理shell感兴趣,您可以查看rush。现在有一些即时的观察(除了如何处理管道外),比如路径,是用一种完全不同的方式来处理的,但是如果您想要的是像迭代器和阻塞这样的ruby类的易用性,那么您只需掌握它们。

Likely not a full replacement in any case, but it might serve your purpose.

在任何情况下都不可能完全替代,但它可能符合你的目的。

[Edit] A quick look around turned up ipython which looks (at a cursory glance) to give more of the natural feel of a shell environment which may or may not be an incentive for you.

[编辑]快速环顾一下四周,发现了ipython,它(粗略地看一眼)提供了更多shell环境的自然感觉,这对您来说可能是,也可能不是。

#7


1  

@OP, if you want to compare Ruby vs Shell, compare using Ruby's interpreter( without libraries/modules). ie compare its in-builts against the shell. Otherwise, they are almost the same. Why? For example, if you want to do advanced maths other than those provided by the shell, the shell can use bc/awk/dc. Those are the math "libraries" for the shell. If you want date structures such as associative arrays, you can use awk. (equivalent to hashes in ruby). In modern bash shell, there are also associative arrays. You can think of *nix external tools (eg wc,grep,sed etc and those in /usr/bin/, /usr/sbin etc) as the shell's "libraries".

@OP,如果您想比较Ruby和Shell,请使用Ruby的解释器(没有库/模块)进行比较。把它的内建物与壳体作比较。否则,它们几乎是一样的。为什么?例如,如果您想做高级数学而不是shell提供的数学,shell可以使用bc/awk/dc。这些是shell的数学“库”。如果您希望使用日期结构(如关联数组),可以使用awk。(相当于ruby中的散列)。在现代bash shell中,还有关联数组。您可以将*nix外部工具(如wc、grep、sed等)视为shell的“库”。

Lastly, if you intend to use system() a lot in Ruby, i suggest using the shell as one of them mentioned, the shell excels in pipes. etc..

最后,如果您想在Ruby中大量使用system(),我建议使用shell作为其中之一,shell在管道中表现出色。等。

#8


0  

Shell's programming languages have a very small footprint and very few dependencies. Beside that I can't see much point using it. Personnaly I prefer using Perl or python for such tasks.

Shell的编程语言占用的空间非常小,依赖关系也非常少。除此之外,我看不出使用它有什么意义。我更喜欢用Perl或python来完成这些任务。