Linux Scripting with Elixir

Hello. I would like to build a shell script to automate certain tasks. For example, when I reinstall Linux I would like to have a post-installation script which automatically installs the prefered applications. Something like calling System.cmd with “sudo apt-get install app1, app2, app3, etc…” and configuring some GNOME settings by calling "gsettings set ". I was wondering should I invest time in learning bash scripting or Elixir can do that for me. Any suggestion appreciated. Thank you!

1 Like

I would go with sh. Because to run Elixir code you need to install tools anyway, sh is out of the box. Additionally it is easier to find admins that know sh rather than finding one that knows Elixir.

4 Likes

If it’s about running a few simple commands then bash would be the best option. If you need to have more control, and a more complex algorithm, I’d recommend python instead.

1 Like

Posix shell scripting is fast and concise. Bash offers a number of neat tricks on top of that (for example this wonderful specimen) - if you love pipes from Elixir, shell is all about pipes. The binaries installed on a system are your standard library, and man is the function docstring. In most languages, to execute a shell command you’d have to call something like system("cat $name"), whereas the shell languages are the only place where you can cat $name directly. I’m not a big fan of using /bin/[ instead of /bin/test, realizing this really helped me embrace the shell. Also like any modern language, there’s shfmt (autoformat) and shellcheck (lint), both impressive well-developed tools.

On a side note, I was playing with an idea of running an elixir node on the machine for use in scripting. Seems like the only ways to trigger the scripts from the outside without spinning up another node to send the message would be D-Bus and Websockets. Standalone Elixir scripts aren’t even that slow to start up, 0.300 for a hello world. Which is not 0.001 for a Dash hello world or 0.004 for Bash one, but I’m sure you could come up with worse (I’m not going to point fingers here).

1 Like

Yeah, but in bash you can install Erlang and Elixir for current user only using asdf, so such bash script would be limited to few commands + finally run Elixir script which could be even included as binary into bash script.

The problem with BASH is that it’s much more hard language. There is no need to learn it if you can ask community for example environment script and do your main work on Elixir only.

Yeah, especially those words: few and simple. Compare Elixir maps with lots of helpful functions with what BASH offers. It has really weird syntax for me. Of course BASH is not a problem for me, but I need to spend at least 5 more time on BASH script rather than on Elixir script.

2 Likes

Thank you. I’m new on this forum and I’m impressed by the responses and the passion behind them. I appreciate all of responses.

I’m only familiar with JavaScript right now and I discovered that Node.js is quite capable when it comes shell scripting. The only drawback is that Node doesn’t come preinstalled with most operating systems, but installing it separately it won’t be an issue for me.

If it’s only post-OS-installation of packages you’re after, Ansible seems all the rage these days (before that were Chef, Puppet… you get the idea).

If you’re into doing fancy things… well, yeah, JS, Python, pretty much every language that can call OS commands from the its runtime is what you’re looking for. Choose the one you’re most comfortable with.

As other said though, to do basic CLI manipulation… welll, BashIS the default, battle-tested, been-here-longer-than-time-itself solution.
And it hasn’t been easily replaced for a reason, you know.


TL;DR - if JS is you poison, enjoy, installing Node is a breeze. Otherwise, just go with the default Bash.


P.S. Whichever solution you choose, don’t forget to source-control it or you’ve done all the work for nothing!

1 Like

Use Ansible for this kind of task, no need to learn or use Elixir for that. Ansible is worth learning, it is system agnostic - for many tasks you don’t need to write any Bash as the tasks are implemented internally in Ansible to be used across all popular linux distros. Also you can save your configuration - called playbook as a separate repo and reuse that accross all of your machines/servers if needed. It is also easy to use and the only thing you need to set are the ssh keys.