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
.
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.
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.
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.
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.
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.
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
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?).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With