Shell command execution via Ruby; Fury the first line of CLI defence

DAR
4 min readMay 22, 2020

--

We all have dabbed command line once or twice. Using terminal to run commands makes you feels like super human when there is no GUI.

When working with multiple tools you might not find support for each tool in the shape of a library, SDK or API for the language you want to work in. Each language has built its niche such as Python is known for Data Science, Ruby for scripting and web with Rails or Java for enterprise. They all are equally capable to do any job i.e take input and provide output. You will find alot of the tools are written and their interface on the Unix/Linux environment is shared as Command Line, one such popular example is cURL. Did you know cURL is based on libcurl which is written in C ? Now how do you use a library written in C with Ruby ? Create A Ruby C extension , map all the features and maintain it ? Or just use the command line interface of the tool and execute commands from your code. This just ain’t needed only for executing some 3rd party tools, you might want to monitor your system , manipulate files, check for running porcesses and handle them. The Unix/Linux environment has tools that are rock solid when it comes to doing their job. They are battle tested and there are no better alternatives to tools such as grep tail sed awk less ps | and many more.

Over time you come in to situations where using CLI tools to accessing services are better suited as there might not be an officially supported library or a feature complete library available. As an example at a certain time AWS and Google Cloud didn’t have great ruby support for its cloud libraries and many others follow the same route as Ruby is now more favoured as a web language due to the popularity of Rails.

I got the idea to script the usage of executing shell commands via Ruby when I had to work with Git and Xcode build tools. In Ruby there are a few ways to execute shell comands such as exec, system and %x() or Backticksc but the simplest is to use backticks. The purpose was to run multiple shell commands and get its output. It would allow you to setup chain of commands need to be executed and also give a standard interface to run shell commands without worrying about different ruby syntax to execute command and capture the output and use them repeatedly or extend them with more commands. so how to use this Ruby Gem a.k.a Fury.

Usage

fury = Fury.new('echo Hell knows no Fury')
fury.execute # executes command and can be used again
fury.reset_cmd! # Used to clear command queuefury = Fury.new('echo gonna have to ask you to exit the donut')
fury.execute! # Resets the command queue after execution to use for new command
#commands can be added to the queue for execution ,
#commands are combined using ';' for shell execution

fury = Fury.new('echo Hell knows no Fury')
fury.execute
fury << 'echo gonna have to ask you to exit the donut'
fury << 'echo Director Fury is no longer in command. Override order 7-Alpha-1-1.'
fury.execute!
#commands can be added to the queue for execution ,
#commands can be combined using desired separator for
#shell execution

fury = Fury.new('echo Hell knows no Fury')
fury.execute
fury.join('&')
fury.queue 'echo gonna have to ask you to exit the donut'
fury.join('&')
fury.queue 'echo Director Fury is no longer in command. Override order 7-Alpha-1-1.'
fury.execute!

So here is some basic usage for the Gem, it can be used to run any tool that is available on you command line. I know there are dime a dozen gems available that do command line magic and are more advanced and probably have superb features but the purpose of this gem isn’t to replace any of them but to have a simple interface to running shell commands and capture output to do further prcessing without any overhead of dependencies. That is why I named it Fury in the world of Avengers someone with no super powers delivers what’s needed.

--

--

DAR
DAR

Written by DAR

Coder during the day, squash player in the evening and cricketer over the weekends. Doubts are the ants in the pants, that keep faith moving

No responses yet