this post was submitted on 27 Aug 2024
113 points (94.5% liked)

Linux

48711 readers
1146 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
 

I've been looking around for a scripting language that:

  • has a cli interpreter
  • is a "general purpose" language (yes, awk is touring complete but no way I'm using that except for manipulating text)
  • allows to write in a functional style (ie. it has functions like map, fold, etc and allows to pass functions around as arguments)
  • has a small disk footprint
  • has decent documentation (doesn't need to be great: I can figure out most things, but I don't want to have to look at the interpter source code to do so)
  • has a simple/straightforward setup (ideally, it should be a single executable that I can just copy to a remote system, use to run a script and then delete)

Do you know of something that would fit the bill?


Here's a use case (the one I run into today, but this is a recurring thing for me).

For my homelab I need (well, want) to generate a luhn mod n check digit (it's for my provisioning scripts to generate synchting device ids from their certificates).

I couldn't find ready-made utilities for this and I might actually need might a variation of the "official" algorithm (IIUC syncthing had a bug in their initial implementation and decided to run with it).

I don't have python (or even bash) available in all my systems, and so my goto language for script is usually sh (yes, posix sh), which in all honestly is quite frustrating for manipulating data.

top 50 comments
sorted by: hot top controversial new old
[–] [email protected] 102 points 4 months ago* (last edited 4 months ago) (2 children)

Why aren't python and bash be available in all your systems? Which languages would be?

I would've recommended python, otherwise perl or Haskell (maybe Haskell's too big) or something, but now I'm worried that whatever reason makes python undoable also makes perl etc. undoable

[–] WatTyler 25 points 4 months ago (1 children)

Mate, I came on here to post Haskell as a semi-ironic 'joke' and it's included in the top comment. You've made my day.

[–] [email protected] 9 points 4 months ago (1 children)

I mean, it's a functional programming language and an incredibly good one. But it probably has a far too big footprint for their use-case...

[–] WatTyler 6 points 4 months ago

Oh, I know. I adore Haskell.

[–] [email protected] 9 points 4 months ago (6 children)

Why aren’t python and bash be available in all your systems?

Among others, I run stuff on alpine and openwrt.

I don't need to run these scripts everywhere (strictly speaking, I don't need the homlab at all), but I was wondering if there's something that I can adopt as a default goto solution without having to worry about how each system is packaged/configured.

As for python, I doubt the full version would fit in my router plus as said I don't want to deal with libraries/virtualenvs/... and (in the future) with which distro comes with python3 vs pyton4 (2 vs 3 was enough). Openwrt does have smaller python packages, but then I would be using different implementations on different systems: again something I'd rather not deal with.

As for perl, it would be small enough, but I find it a bit archaic/esoteric (prejudice, I know), plus again I don't want to deal with how every distro decides to package the different things (eg. openwrt has some 40+ packages for perl - if I were doing serious development that would be ok, but I don't want to worry about that for just some scripts).

[–] [email protected] 17 points 4 months ago

Sounds like you want MicroPython. It’s definitely available on OpenWrt and AlpineLinux and has a very small footprint.

If you don’t like Python, have a look at Lua/luajit.

[–] [email protected] 15 points 4 months ago

You've defined yourself into an impossible bind: you want something extremely portable, universal but with a small disk imprint, and you want it to be general purpose and versatile.

The problem is that to be universal and general purpose, you need a lot of libraries to interact with whatever type of systems you might have it on (and the peculiarities of each), and you need libraries that do whatever type of interactions with those systems that you specify.

E.g. under-the-hood, python's open("<filename>", 'r') is a systemcall to the kernel. But is that Linux? BSD? Windows NT? Android? Mach?

What if you want your script to run a CLI command in a subshell? Should it call "cmd"? or "sh"? or "powershell"? Okay, okay, now all you need it to do is show the contents of a file... But is the command "cat" or "type" or "Get-FileContents"?

Or maybe you want to do more than simple read/write to files and string operations. Want to have graphics? That's a library. Want serialization for data? That's a library. Want to read from spreadsheets? That's a library. Want to parse XML? That's a library.

So you're looking at a single binary that's several GBs in size, either as a standalone or a self-extracting installer.

Okay, maybe you'll only ever need a small subset of libraries (basic arithmetic, string manipulation, and file ops, all on standard glibc gnu systems ofc), so it's not really "general purpose" anymore. So you find one that's small, but it doesn't completely fit your use case (for example, it can't parse uci config files); you find another that does what you need it to, but also way too much and has a huge footprint; you find that perfect medium and it has a small, niche userbase... so the documentation is meager and it's not easy to learn.

At this point you realize that any language that's both easy to learn and powerful enough to manage all instances of some vague notion of "computer" will necessarily evolve to being general purpose. And being general purpose requires dependencies. And dependencies reduce portability.

At this point your options are: make your own language and interpreter that does exactly what you want and nothing more (so all the dependencies can be compiled in), or decide which criteria you are willing to compromise on.

[–] [email protected] 9 points 4 months ago

Is compiling scripts an option? Aka compiling them in C, C++, Rust, whatever for your router on another machine, and copying and executing those binaries on your router?

load more comments (3 replies)
[–] [email protected] 33 points 4 months ago

luajit is small, fast(well, it can jit), and has a small but complete standard library and can do FFI pretty easily, should be ideal for most homelab usecase

ldd $(which luajit)                                                                                
        linux-vdso.so.1 (0x00007ffee9dc7000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb4db618000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fb4db613000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fb4db5f3000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb4db3ca000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fb4db799000)
[–] possiblylinux127 21 points 4 months ago (10 children)

Python is what you want. You can install it on just about any system.

Other than that maybe Lua but that will be hell.

load more comments (10 replies)
[–] [email protected] 17 points 4 months ago

Perl is already installed on most linux machines and unless you start delving into module usage, you won't need to install anything else.

Python is more fashionable, but needs installing on the host and environments can get complicated. I don't think it scales as well as Perl, if that's a concern of yours.

[–] [email protected] 17 points 4 months ago (1 children)

What about Lua/Luajit?

In most scripting languages you have the interpreter binary and the (standard) libraries as separate files. But creating self-extracting executables, that clean up after themselves can easily be done by wrapping them in a shell script.

IMO, if low dependencies and small size is really important, you could also just write your script in a low level compiled language (C, Rust, Zig, ...), link it statically (e.g. with musl) and execute that.

load more comments (1 replies)
[–] [email protected] 15 points 4 months ago (1 children)
[–] [email protected] 4 points 4 months ago* (last edited 4 months ago)

That does seem to be just one, maybe two small files, and no dependencies. And a built in map() function.

[–] [email protected] 13 points 4 months ago (6 children)

I can’t really think of anything that’s less frustrating than sh and ticks all your boxes. You can try TCL but it’s bound to be a shit show. It was painful to use two decades ago.

Perl is a step up in terms of developer comfort, but it’s at the same time too big and too awkward to use.

Maybe a statically linked Python?

load more comments (6 replies)
[–] [email protected] 12 points 4 months ago (2 children)

I honestly love Powershell, but haven't tried the Linux version yet. I only use Bash on linux but it has a load of odd quirks that make it unpleasant to use imo. Can't comment on anything else.

[–] [email protected] 6 points 4 months ago

Pwsh 7.x works very well in Linux. Haven't got any snags.

load more comments (1 replies)
[–] [email protected] 12 points 4 months ago (7 children)

Only 5 years ago, everybody would be singing and shouting "perl".

Nowadays it is python that has taken this position (even though Perl is still there and can do so much more).

[–] [email protected] 8 points 4 months ago (1 children)

More like

20 years ago - perl

10 years ago - python

Nowadays - go

[–] possiblylinux127 9 points 4 months ago (3 children)

Go isn't a scripting language

load more comments (3 replies)
load more comments (6 replies)
[–] [email protected] 12 points 4 months ago

I don't have python (or even bash) available in all my systems, and so my goto language for script is usually sh (yes, posix sh), which in all honestly is quite frustrating for manipulating data.

Why are you making it hard on yourself? apt/dnf install a language to use and use it.

[–] [email protected] 12 points 4 months ago

another vote for Lua – lua5.4 is available for all 8 Alpine architectures, tiny installed size (120–200 kB) (and Alpine package only installs two files)

[–] [email protected] 12 points 4 months ago (1 children)

Realistically whatever problems you see in python will be there for any other language. Python is the most ubiquitously available thing after bash for a reason.

Also you mentioned provisioning scripts, is that Ansible? If so python is already there, if you mean really just bash scripts I can tell you that does not scale well. Also if you already have some scriptsz what language are they on? Why not write the function there?

Also you're running syncthing on these machines, I don't think python is larger than that (but I might be wrong).

load more comments (1 replies)
[–] [email protected] 10 points 4 months ago* (last edited 4 months ago)

The smallest footprint for an actual scripting probably will be posix sh - since you already have it ready.

A slightly bigger footprint would be Python or Lua.

If you can drop your requirement for actual scripting and are willing to add a compile step, Go and it's ecosystem is pretty dang powerful and it's really easy to learn for small automation tasks.

Personally, with the requirement of not adding too much space for runtimes, I'd write it in go. You don't need a runtime, you can compile it to a really small zero dependency lib and you have clean and readable code that you can extend, test and maintain easily.

[–] [email protected] 8 points 4 months ago (4 children)

I still love bash. I'm able to accomplish quite a lot with it. I vote bash.

[–] [email protected] 4 points 4 months ago

Also vote bash, but I don't love it..more of a tolerate.

load more comments (3 replies)
[–] [email protected] 8 points 4 months ago (4 children)

No one has mentioned PHP yet? Man, times really have changed.

load more comments (4 replies)
[–] [email protected] 8 points 4 months ago (1 children)

i use mostly python and lua.

load more comments (1 replies)
[–] [email protected] 7 points 4 months ago

I don't know if it matches your desire for easy install of small disk space, but it might make up for it in other arenas - Ruby is my new-found love when making simple scripts. Being able to mostly emulate the shell integration that bash has by just using backticks to call a shell command is the killer feature in my book.

[–] [email protected] 7 points 4 months ago

Technically, you could bundle a Perl script with the interpreter on another system using pp and run the packed version on systems with no installed Perl, but at that point you might as well just use a compiled language.

[–] [email protected] 6 points 4 months ago (1 children)

If you can, just use Perl. Probably installed on your systems, even the ones without python.

load more comments (1 replies)
[–] [email protected] 6 points 4 months ago

I've looked into this a lot actually. There see many options. I'll highlight the pros and cons of each option.

Lua: extremely lightweight, but standard library is lacking, and doesn't include stuff like map or fold. But that would be easy to fix.

Python: thicc standard library, but is not lightweight by any means. There are modifications made to be more shell like, such as xonsh

Rash: based on scheme, very much functional but if you're not used to lisp style, might take a bit to get used to it. This is actually my favorite option. It has a cli interpreter, and really pleasant to use. Cons is... Well it's not very common

You can honestly use any language. Even most compiled languages have a way to run immediately.

[–] [email protected] 6 points 4 months ago* (last edited 4 months ago)

Maybe something like Elvish or Nushell could be worth a look. They have a lot of similarities to classic shells like bash, but an improved syntax and more powerful features. Basically something in between bash and Python. Not sure about disk footprint or general availability/portability though

[–] [email protected] 6 points 4 months ago (7 children)

Powershell, yeah I said it!

load more comments (7 replies)
[–] [email protected] 6 points 4 months ago* (last edited 3 months ago)

Not quite a scripting language, but I highly recommend you check out cosmo for your usecase. Cosmopolitan, and/or Actually Portable Executable (APE for short) is a project to compile a single binary in such a way that is is extremely portable, and that single binary can be copied across multiple operating systems and it will still just run. It supports, windows, linux, mac, and a few BSD's.

https://cosmo.zip/pub/cosmos/bin/ — this is where you can download precompiled binaries of certain things using cosmo.

From my testing, the APE version of python works great, and is only 34 megabytes, + 12 kilobytes for the ape elf interpreter.

In addition to python, cosmopolitan also has precompiled binaries of:

  • Janet 2.5 MB
  • Berry 4.0 MB
  • Python 34 MB
  • Php 11 MB
  • Lua 2.1 MB
  • Bash 5.1 MB

And a few more, like tclsh, zsh, dash or emacs (53 MB), which I'm pretty sure can be used as an emacs lisp intepreter.

And it should be noted these may require the ape elf interpeter, which is 12 kilobytes, or the ape assimilate program, which is 476 kilobytes.

EDIT: It also looks like there is an APE version of perl, and the full executable is 24 MB.

EDIT again: I found even more APE/cosmo binaries:

[–] [email protected] 5 points 4 months ago* (last edited 4 months ago) (2 children)

JavaScript through Node.js, or TypeScript through Deno if you like typed languages. They both check all your boxes (just check the size of the executables to make sure that it's what you would consider "small footprint").

Both languages and runtimes are quite popular, so you will find any answers on StackOverflow.

They are both single-executable with little dependencies, and Deno can also compile your scripts to self-contained executables.

As a bonus, both support the vast and extensive NPM package repository where you can find all sort of libraries for even the most complex tasks.

And they work with your favourite IDE or editor, not just syntax highlighting, but also contextual suggestions.

[–] [email protected] 4 points 4 months ago (1 children)

Installing node uses some 60MB (according to zypper on my current desktop). I'd rather have something small and possibly that consists of a single executable.

As a bonus, both support the vast and extensive NPM package repository

That's not necessarily a feature :) Package repos are great if you are a developer (I am one) working primarily with that language, but are frustrating if you just want to run things.

load more comments (1 replies)
load more comments (1 replies)
[–] [email protected] 5 points 4 months ago

A scripting language written in Rust would certainly fulfill you requirement of only needing to copy one file since they are always statically linked and you can even statically compile against musl so it will work on any Linux system without needing a correct libc. Maybe check out rhai.

[–] [email protected] 4 points 4 months ago (1 children)

Tried bash, Make, and awk/sed. All hit brick walls. Finally landed on pyinvoke. Two dependencies to install on any new machine. Never had problems. Also, easy to debug and modify as projects evolve.

load more comments (1 replies)
[–] [email protected] 4 points 4 months ago (2 children)

Elixir checks most of those boxes. If you want a good functional scriptibg language, Elixir soynds like the go to. Some lisp language like guile should also be sufficient, and probably have a lighter footprint.

This requirement stands out though:

has a simple/straightforward setup (ideally, it should be a single executable that I can just copy to a remote system, use to run a script and then delete)

Thats basically what ansible does. If you plan on doing this to multiple machines you should just use ansible. Also how do you plan on ensuring the scripting interpreter is installed on the machines?

load more comments (2 replies)
[–] [email protected] 4 points 4 months ago

You might enjoy lua or lune.

[–] [email protected] 4 points 4 months ago

There's always nushell. It's fairly new, not quite to 1.0 yet (0.96.1 at time of writing), but the constant breaking changes seemed to have stopped. It hits all your points and it's quite fun to use when writing scripts. Bonus that it's also pretty much tailor-made to manipulate data.

[–] [email protected] 4 points 4 months ago

@gomp I like TypeScript.

I used Python for 15 years or so until they changed from v2 to v3. At that point I realised I couldn't understand my old code because it lacked types, so I got discouraged with that. So rather than learn v3 I stopped using it.

Perl is a disaster. sh is good for shell scripts but let's not stretch it.

TypeScript can use all the JS libraries and runs on node which is supported by all sorts of platforms. Yes there are a few holes in the type safety, so don't do that.

The internet is full of "how to do X in JS". You can read them and add the types you need.

[–] [email protected] 4 points 4 months ago (1 children)
load more comments (1 replies)
[–] [email protected] 4 points 4 months ago* (last edited 4 months ago) (1 children)

I would go with Guile, because it is built-in to the Guix Package Manager which is a really good general-purpose package manager.

It ticks several of your boxes:

  • has a CLI interpreter
  • is a general purpose language, Scheme, amd compliant with revisions 5, 6, and 7 of the language standard
  • allows writing in a functional style (it is one of the original functional programming languages)
  • small disk footprint, but still large enough to be "batteries included"
  • decent documentation, especially if you use Emacs
  • simple setup: not so much, unless you are using Guix to begin with. The standard distribution ships with lots of pre-built bytecode files, you need an installer script to install everything.

It also has pretty good libraries for system maintenance and reporting:

load more comments (1 replies)
[–] [email protected] 4 points 4 months ago (10 children)

Perl or python for things likely to already be there. Maybe ruby or PHP if you must. I used to work in groovy a lot but I think it requires the JVM

load more comments (10 replies)
load more comments
view more: next ›