←back to thread

185 points thunderbong | 2 comments | | HN request time: 0.001s | source
Show context
Imustaskforhelp ◴[] No.43647155[source]
Read your article, it's really nice. I really feel much less demystified by this.

But can you / somebody please explain what this means

According to the official Kernel Admin Guide:

This Kernel feature allows you to invoke almost (for restrictions see below) every program by simply typing its name in the shell. This includes for example compiled Java(TM), Python or Emacs programs. To achieve this you must tell binfmt_misc which interpreter has to be invoked with which binary. Binfmt_misc recognises the binary-type by matching some bytes at the beginning of the file with a magic byte sequence (masking out specified bits) you have supplied. Binfmt_misc can also recognise a filename extension aka .com or .exe.

It’s another way to tell the Kernel what interpreter to run when invoking a program that’s not native (ELF). For scripts (text files) we mostly use a shebang, but for byte-coded binaries, such as Java’s JAR or Mono EXE files, it’s the way to go!

Like, can you give me an example by what you mean. What are its use cases, if any. I read it many times and always with some sort of enthusiasm because of this sentence ending in exclamation point making me feel like it's huge yet I just can't understand it's significance.

Does it mean we can have .jar files which can then run shebang like, so we don't need #! , can this also be used for main.go or every other language which has some issues with #! ,

I see there being some interpreter for golang, rust etc. which just compiles it but it was just too complex. I am just imagining something like a simple go file which is valid golang but can be run by linux simply by ./ And it autocompiles it...

replies(4): >>43647278 #>>43647280 #>>43649406 #>>43649840 #
1. pkaye ◴[] No.43647280[source]
Yes you can use binfmt_misc to allow arbitrary executable file format to be passed to an interpreter matched either by a filename extension or a magic number at a specific offset within the executable.

https://en.wikipedia.org/wiki/Binfmt_misc

replies(1): >>43651343 #
2. kreetx ◴[] No.43651343[source]
Yup, this way you can skip the shebang line for your scripts in some arbitrary language.

I have it set up for Haskell (it's somewhat hackish): there is preconfigured haskell project in a certain location with desired dependencies, imports, etc., so when executing a .hs file this file is copied into that as Main and ran. A similar setup will work for any language.

Edit: `cabal` and `stack` both have script commands now, so these would be an alternative to the above, downside being that every such script would need the shebang intro with dependencies, etc.