Live data from Hacker News

Ruby: A great language for shell scripts

lucasoshiro.github.io

151–160 of 368 posts

Re: Ruby: A great language for shell scripts

#151
post #119

It seems like a waste of precious syntax to dedicate backticks to running shell commands. What's between the backticks is not even portable; the commands rely on an operating-system-specific command interpreter. > puts `ls`.lines.map { |name| name.strip.length } # prints the lengths of the filenames Fantastic example, except for the commandment violation: "thou shalt not parse the output of 'ls'"! You really want to…

They have inherited the second biggest mistake of shellscripts; Requiring the user to manually check $? after each command. No thanks. Anything that doesn't have error handling enabled by default goes straight in the trash bin.

I couldn't spot any way to make something like `ls -j` (j is an illegal option) throw an exception in ruby (as opposed to simply outputting the system error message).

The closest I could find is what you suggest (checking $?), or using something like this [1], which would require changing syntax:

  system('ls -j', exception: true) 
Would be great to know if there's some easy callback or, ideally, a global setting one can make so a ruby exception is thrown if there's an error running system commands using the backtick syntax.

[1] https://stackoverflow.com/a/54132729/5783745

Re: Ruby: A great language for shell scripts

#152
post #131

If you never coded in ruby before, but use macOS, you already have ruby installed. Just open terminal and type irb to bring up the ruby interpreter and try out the code in the article.

I think the built in one is deprecated though right?

Are there distributions where this doesn't happen? I feel like I'm often installing or compiling new versions of packages and languages because they're outdated on Ubuntu

Re: Ruby: A great language for shell scripts

#153
post #141

Earlier quoted context omitted.

This is indeed why I use Perl over Ruby. As long as it's not for a Window machine, a Perl script is deployed by copying it over and that's it.

That was the reason Perl was what I switched too from bash when I was working on Solaris boxes; it was miles ahead of what was possible with bash AND it was already present. If I remember an older version of Python was also installed but by then Perl had already got me reeled in and I felt Python to be too "verbose" compared to Perl (I eventually changed my opinion when I got a bit more experience under my belt).

Interesting! I still find Python too verbose to stand in for shell scripts when Perl is available, with what I think is a decent chunk of experience.

Re: Ruby: A great language for shell scripts

#154
post #73

Earlier quoted context omitted.

Bundle it into a pex and distribute that. Its still way large but its easy to distribute.

Pex was also the solution I landed on after evaluating several non-container options for distributing a Python project to arbitrary Linux hosts. It works well but with one huge caveat: although you bring the stuff required to reconstitute the venv with you, you’re actually still using the system’s python executable and stdlib!! So for example if you want to make a project targeting all supported Ubuntu LTS versions,…

Nuitka has worked for me for everything Ive tried (in house dev tools). I didnt end up using it for work because I can rely on a pristine system Python with the right version so pex makes more sense.

There are other options I didnt look too much into, e.g. Beeware

Re: Ruby: A great language for shell scripts

#155
post #9

I sometimes wonder why we don't see ruby used for shell stuff more often. It inherited most of the good stuff for shell scripting from Perl, and Perl took a lot of it's syntax from sh and sed and awk, so almost anything you can do in shell script you can do in ruby, but with an option of making it gradually less terse and more readable, while having sane variables and data handling from the start. Also ruby is great…

> why we don't see ruby used for shell stuff more often Simple, ruby is not installed by default. Even Python, while it is on (almost?) all modern Linux distributions, is not installed on the BSDs.

Python's also not installed by default in (most?) official docker images.

Re: Ruby: A great language for shell scripts

#157
post #141

Earlier quoted context omitted.

I think the quality of a language for shell scripting is often secondary. What’s of greater significance is where it is at. I.e., does it have it already installed? The answer with Linux and Bash is almost always “yes”. Not so with ruby. The moment you start asking the user to install things, you’ve opened up the possibility for writing a program rather than a shell script. The lifecycle of a piece of software is alm…

This is indeed why I use Perl over Ruby. As long as it's not for a Window machine, a Perl script is deployed by copying it over and that's it.

One usually needs modules to easily do something more advanced, but yes, Perl is almost always installed. Although I find Ruby much more ergonomic, I still reach for Perl as well because I know it better and don’t have to open the documentation so often.

Re: Ruby: A great language for shell scripts

#158
For me, the problem with shell scripting is that I do it only occasionally. I’ve always been tempted by higher-level languages. The arrival of tools like ChatGPT has made me much more comfortable writing scripts of intermediate complexity. I find shell scripting more interesting now.
Post reply on HN