this post was submitted on 02 Sep 2023
77 points (88.9% liked)

Programming

16207 readers
572 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS
 

why?

Because bash feels clunky to write and work with for anything non-trivial, especially compared to other scripting languages.

Why not another scripting language (no compile necessary)?

Because bash and sh are installed nearly everywhere. Any other scripting language means the user is required to have that installed, and that is far less likely to be the case.

If I could write my scripts in a nice syntax, but be sure my users will be able to use it effortlessly by distributing to them compiled versions, then that would make both of our lives easier!

Thoughts? Are there any languges that do this?

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

python is usually the next step up in admin land

python is a pretty standard install on linux systems since so many things like you're talking about use it

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

Not only that it's basically everywhere, but even if it's not, you can compile it using something like nuitka and still use it.

[–] [email protected] 6 points 10 months ago (2 children)

Huh, why doesn't python just ship this? Managing python installs is annoying as hell.

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

I would guess mostly because python interpretes are just about everywhere.

Also the binaries compiled with nuitka end up being much bigger in size. A simple script of a few kb can and up in the hundreds of mb when you start compiling the dependencies, so it's not a perfect solution.

[–] [email protected] 3 points 10 months ago

This is about python packaging, like making/getting libraries/apps rather than compiling binaries, but it's pretty relevant here:

https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/

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

Python or Perl. IIRC, I've seen systems with a Perl install by default, but not Python.

[–] [email protected] 53 points 10 months ago* (last edited 9 months ago) (7 children)

I write a lot of bootstrapping scripts, and I have a solution thats probably something you and others in this thread have never seen before. You can write a single script in a full/normal language, no compilation step, and it works on systems that only have bash/sh. It doesn't compile to bash, or at least not in the way you might think/expect it to, but it should do what you want.

(guillotine because it's a universal executor) https://github.com/jeff-hykin/deno-guillotine

This^ one uses Deno/JavaScript, but in principle it might be possible to do with other languages. It definitely requires some explanation, so I'll try to give that here;

As another person said, shells are not nearly as standardized as we need them to be. Mac uses zsh, Ubuntu uses dash, neither store a posix bash exectuable in the same place, and both have ls and grep differences that are big enough to crash common scripts. Even if you're super strict on POSIX compliance, common things will still break if you write a big script (or trying to compile a big program to bash).

I hate JS as much as the next guy, but it is possible to write a single text file that is valid bash/dash/zsh/powershell and valid JavaScript all at the same time. It sounds impossible, but there is enough overlapping syntax that actually any javascript program can be converted into a valid bash script without mangling the JS code. It might be possible to do for python as well.

POSIX is good enough for making a small, carefully-crafted well-tested OS-detecting caveat-handling script. So that's exactly what we do; use a small shell script at the top to ensure that the JS runtime you want is installed (auto install if missing). Then the script executes itself again using the JS runtime. It wasn't easy but I a made a library that explains how it's possible and gives a cli tool that automates it for the Deno runtime (the link I posted above).

After that, I just recreated tools that feel like bash, but this time they are actually cross platform. Ex:

let argWithSpaces = "some thing"
run`echo hello ${argWithSpaces}`

I picked Deno because it auto installs libraries (imports directly from URL so users don't have to install anything)

[–] [email protected] 44 points 10 months ago (2 children)

its possible to write a single text file that is valid bash/dash/zsh/powershell and valid JavaScript all at the same time. It sounds impossible, but there is enough overlapping syntax that actually any javascript program can be converted into a valid bash script without mangling the JS code.

I'm both impressed and horrified

[–] [email protected] 25 points 10 months ago* (last edited 10 months ago) (1 children)

It'd honestly the funniest thing I've read on this instance. Puts programmer humour to shame. Love it when developers finds the jankiest/unconventional way to solve problems.

[–] [email protected] 2 points 10 months ago* (last edited 10 months ago)

It's one of those tools that can both be used on a resume or as a diagnosis. I love it!

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

Okay at first I was pretty convinced that this was just the wrong way to accomplish what I thought your goal was. But now, after reading the StackOverflow post and your README, I think this is fascinating and frankly really awesome. What a clever and strange thing, using multiline comments that way, and string no-ops. I think just knowing this exists will cause me to find reason to use it.

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

Not only do you solve the problem op is trying to solve, but you also made the most horrifying and hilariously ingenious thing at the same time.

[–] [email protected] 3 points 10 months ago

Polyglot runtime lmao

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

Bro, make a video and put it up on peertube please then link it in the README. I need to see this shit in action. It sounds awesome, but it's 10am and my eyes are just opening, so reading through everything and testing it isn't happening on my phone rn.

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

Actually I've been thinking of starting a Youtube/Peertube channel for a while so this will be a good place for me to start!

I'll come back and post a response once I've uploaded it! It'll probably take a week or two.

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

Can this thing run complied WASM? Because compilers to WASM from other languages exists already

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

It can run compiled wasm! So you could write a bootstrap script in rust, compile it to wasm, embed that wasm into a deno installer script using https://deno.land/x/binaryify and then ship that as a universal executor.

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

Do I understand it right that what the tool does is include install scripts in all of the other languages, that simply download a portable Deno runtime and then run the rest of the file (which is the original Javascript code) as Javascript?

So, you basically still have an install step, but it was just automated to work cross-platform though what's basically a polyglot install script. Meaning that this could probably be done with almost any other language, assuming it has a portable runtime - such as portable python and similar, is that correct?

[–] [email protected] 3 points 10 months ago* (last edited 10 months ago)

Almost, but you bring up an important point about other language support.

The code includes an install script for one language, and the second part about "any language" isn't quite right. There is an alternative way to get any-language support but the current approach requires a language to have a syntax that is compatible with bash/powershell. For example I abuse the hell out of multi-line strings and multi line comments in javascript to make it be interpreted as a do-nothing bash/powershell script.

Python specifically might be possible because of its triple-quote strings, I haven't spent a long time trying but I did try a bit. However in general I don't think languages, like Haskell or Elixr, can work in this form because their syntax is incompatible.

However, if you don't care about being able to edit the script, it should be possible to mangle code from other languages, like converting Haskell code to hex or some other escaped format (can't be binary because that's not valid bash/powershell). We'd need to handle unpacking that hex with shell/powershell, but it could be done. And in that case, yes it would work with any portable language. (And many are more portable than Deno, which struggles to run on old stuff like Ubuntu 16.04!)

If you're interested in the hex unpacking let me know. I'm working on an offline bootstrapping script for deno, which involves embedding the runtime binaries of all OS's as hex into the script itself. Once I make it, it should be a lot easier to get this kind of thing working for other portable runtimes.

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

There is Batsh, a simple language with c-style syntax that compiles to bash and batch.

[–] [email protected] 28 points 10 months ago* (last edited 10 months ago) (3 children)

Unfortunately shell script is not as portable as you might be anticipating. Different distro run different shells, with different settings, and also different tools. Think BSD grep vs GNU grep.

[–] [email protected] 8 points 10 months ago* (last edited 10 months ago)

I've seen multi distro scripts that are also able to bootstrap their own assets for each distro/architecture. Don't see why you wouldn't be able to check that considering /etc/os-release exists in pretty much every unix like environment.

And having it run on a specific shell type could also be an option.

[–] [email protected] 2 points 10 months ago

I discovered a fun one the other day: there is literally no way to represent word-boundary anchors that's valid in both GNU sed and BSD sed. https://unix.stackexchange.com/a/393968/38050

[–] [email protected] 2 points 10 months ago* (last edited 10 months ago)

That's what the POSIX spec is for. BSD and GNU commands may differ, but they both support what's specified by POSIX. By limiting your calls to it, you can write portable script with no problem (I've been doing that for the last few years without issue).

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

This thread reminds me of stackoverflow, most people are just convincing you in something else and it is obvious they have never been in your (and mine) situation.

Just answer question if you have some idea, yes we know python exist, that's nice, but not an answer to this question.

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

Just use Python. If you absolutely need it on every system use go and compile it down to a binary.

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

Why not use a compiled language that compiles to fat binaries (rust, go etc)?

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

It's worth noting any compiled language can make a "fat" binary (e.g., C++), you just need to use static linking.

[–] dack 13 points 10 months ago (1 children)

I don't see why bash would be used at all here. If you want something that doesn't need another interpreter, then just compile a binary.

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

Possible use case: scripts that are found in a codebase for doing... Codebase things. Like setting up dev environment for example.

[–] dack 5 points 10 months ago

But then building it still requires whatever scripting tool you use. Including the bash-ified version would not for practice, as it wouldn't be very human readable and would have to be kept in sync with the source script. It's much cleaner and simpler to just require python for your build environment.

[–] [email protected] 3 points 10 months ago

look into devenv.sh for that constrained use-case

[–] [email protected] 2 points 10 months ago

That's a use case for Docker

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

Giga brain move: make a bash script that compiles some c code then runs it

[–] [email protected] 3 points 10 months ago

Still requires the compiler though.

I don't have a good answer, but I've embedded python in bash scripts before after having the bash script install python. Take that as you will.

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

Not exactly what you asked for, but take a look at Nushell

[–] [email protected] 5 points 10 months ago (2 children)

I'm familiar with NuShell and looks very nice. But unfortunately yeah, not what I'm looking for. It would require installation by user.

load more comments (2 replies)
[–] [email protected] 7 points 10 months ago (3 children)

Why not another scripting language (no compile necessary)?

But you're describing compiling that new language to bash...

[–] [email protected] 5 points 10 months ago

Yes, I'm answering why I am not taking a completely other scripting language

In other words, I am making the case for a compiled language by answering the question of why i am not considering a language that doesn't need it.

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

The way I used to solve this problem back in the day was with a statically compiled Perl binary.

[–] theherk 5 points 10 months ago

Although it doesn’t crosspile to bash, I think a good middle ground is bitfield/script. Basically you can do many things you would normally script very simply with nice syntax and distribute a binary.

[–] [email protected] 5 points 10 months ago

https://github.com/tdenniston/bish is one such language.

I'd also recommend Shellcheck which helps prevent many problems with shell scripts.

[–] [email protected] 3 points 10 months ago

I'm gonna pile in with yet another option that isn't a language that compiles to bash...

Consider Ruby for easier shell scripting. With its back tick syntax for executing shell commands, it's quick to use when you want to glue together a series of commands. ...and then you get to use a sane syntax for your script's logic.

Ex:

# Check my history for all usages of the xsv command
# and extract the filename
csv_files = `grep ~/.zsh_history "xsv" | awk '{print $3}'`.
                     split("\n").
                     map(&:chomp)

# list any csv with my phone number
csv_files.select { | filename|
  `grep #{filename} "555-1234"`.chomp != ""
}.each {|filename| puts filename}
[–] [email protected] 2 points 10 months ago (3 children)

Doesn't powershell do this? I've been learning powershell, and they keep making a tech agnostic claim along these lines, but I haven't tested it on Linux yet.

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

Not quite - even in PowerShell 7 there are some features that only work on Windows and Windows only comes with PowerShell 5.1 by default.

[–] alokir 2 points 10 months ago

I used it on a Mac and on Windows, for me it feels very modern when compared to bash (although I never was a bash expert).

However, the problem is that it's not installed by default on Linux (at least on most distros as far as I know) and Mac, and Windows machines might have an outdated version which you'll have to take into account.

So unfortunately it doesn't meet OP's criteria that it should "just work" without installing an interpreter.

load more comments
view more: next ›