为什么以及何时使用 shell 而不是 Ruby [英] Why and when to use the shell instead of Ruby

查看:44
本文介绍了为什么以及何时使用 shell 而不是 Ruby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我熟悉 Ruby,因此我将要使用它在 OS X 上编写一些脚本.但后来我想,也许我错过了这条船.我知道有很多理由更喜欢 Ruby 而不是 Bash(或任何与 sh 兼容的命令语言解释器),但我不知道有任何理由不这样做.直接对 shell 编程有什么好处?

Since I am conversant in Ruby, I am about to script a few things on OS X 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?

我打算在必要时使用 system 来利用系统命令.

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

注意:我已经知道 Ruby 不会一直存在,但我主要对技术、语义和句法标准感兴趣.

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

Ruby 并不总是存在,我的意思是它不是所有 *nix 发行版的标准部分,与 vi 不同.

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

推荐答案

shell 的编程语言除了一件事之外,其他方面都很糟糕.

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

管道.

用于管道的 shell 编程语言非常棒.

The shell's programming language for pipelines totally rocks.

|&; 运算符,加上 () 和 ``` 形成一个整洁的用于描述管道的小语言.

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

a &b 是并发的

a ;b 是连续的

<代码>a |b 是一个管道,其中 a 提供给 b

a | b is a pipeline where a feeds b

shell 编程的那部分很摇滚.

That part of shell programming rocks.

想想( a & b & c ) |发球捕获|分析 是一种很难用 Python(或 Ruby)表达的东西.您可以使用 iterpipes 完成大部分工作,但不是全部.

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.

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

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

需要注意的最重要的事情是在 shell 级别涉及 expr 的任何事情.一旦您开始尝试进行计算",您就超出了 shell 的最佳位置,您应该停止在 shell 中编程并重新思考您在做什么.

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 而不是 Ruby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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