Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why and when to use the shell instead of Ruby [closed]

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?

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.

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

like image 584
Dan Rosenstark Avatar asked Feb 26 '10 16:02

Dan Rosenstark


People also ask

What is the purpose of the shell Why program with a shell?

A shell is a computer program that presents a command line interface which allows you to control your computer using commands entered with a keyboard instead of controlling graphical user interfaces (GUIs) with a mouse/keyboard/touchscreen combination.

When should you use bash?

People use Bash when they want to control their computer or OS without having to navigate menus, options, and windows within a GUI. As we pointed out earlier, with CLIs like Bash, you usually don't even need to use your mouse. You can navigate through your computer's OS without your fingers ever leaving your keyboard.

When would you use bash scripts and when would you use Python scripts?

Python is highly efficient programming language used for general-purpose programming. Bash is not a programming language, it is a command-line interpreter. Bash is a software replacement for the original Bourne shell. Python is easy, simple and powerful language.

Should I use Python or bash?

Performance-wise bash outperforms python in the process startup time. This shows a huge difference however bash execution time degrades quickly if it has to do anything sensible since it usually must call external processes. If you care about performance use bash only for: really simple and frequently called scripts.


4 Answers

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

Pipelines.

The shell's programming language for pipelines totally rocks.

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

a & b is concurrent

a ; b is sequential

a | b is a pipeline where a feeds b

That part of shell programming rocks.

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.

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

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.

like image 176
S.Lott Avatar answered Sep 28 '22 15:09

S.Lott


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

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.

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

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

-- and use Ruby for anything else

like image 41
Shadowfirebird Avatar answered Sep 28 '22 15:09

Shadowfirebird


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?).

like image 2
Joakim Elofsson Avatar answered Sep 28 '22 14:09

Joakim Elofsson


Directly writing a POSIX or Bash script works out well for operations that loop over lists of files. Things like

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.

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.

But it's a 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.

like image 2
DigitalRoss Avatar answered Sep 28 '22 15:09

DigitalRoss