[Toybox] turning a shell script to an executable in a new toybox command?

Rob Landley rob at landley.net
Mon Aug 15 00:52:23 PDT 2022


On 8/15/22 00:38, scsijon wrote:
> Been looking around for a while now for simple shell compiler that I 
> could turn a number of shell scripts into small compiled packages to 
> deal with varying systems and versions, and I can't seem to find 
> something with a 'small' output.

*blink* *blink*

Conceptually a shell script calls other commands out of the $PATH... Are you
just trying to link the script into the shell binary, or are you trying to link
all the _other_ commands into one big binary? (In which case you're basically
reinventing flatpack, or whatever they call those "I'm told static linking is
bad so let's loopback mount a filesystem archive and run a container in it"
things...)

> BarryK's recently tried with BaCon, Nim, V, using helloworld and the 
> smallest is 19K and up to 689K. I tried a few others late last year and 
> rarely got below 350K with all the library addatives they seem they must 
> add inside the 'command'.

A shell hello world? As in "echo 'hello world'"? Or some other hello world?

(In some shells echo is a builtin, so maybe that would work...?)

> So I thought that ?maybe it could be worth adding one into the toybox 
> set,

No thanks.

> after all it surely doesn't have to be that complex, it's only 
> dealing with scripts?

In theory sh -c 'big long thingy' can be megabytes of argument, so what' you're
asking for isn't TECHNICALLY that hard... it's just unless you've got everything
else in the $PATH for it to call it's not gonna work very well. (Even when toysh
can xexec() itself it won't _always_ do that, for reasons ranging from stack
depth to suid permission reacquisition. So it still needs a $PATH at least some
of the time...)

Seriously, static linking in C exists for a reason.

> I was thinking / hopping about a maximum of 16K 
> and down to 8K or smaller for a simple script and inbuilt library.

$ make sh
...
$ ls -l sh
-r-xr-xr-x 1 landley landley 94440 Aug 15 02:47 sh

Seems unlikely? Ok, that's pulling in most of lib/ because standalone, but:

$ ls -l sed
-r-xr-xr-x 1 landley landley 39296 Aug 15 02:48 sed
$ ls -l cp
-r-xr-xr-x 1 landley landley 31168 Aug 15 02:49 cp

A good 2/3 of that is actually shell plumbing. (Bash does a lot!)

> Not being a package builder, just a user and simple scripter, I wondered 
> if it's possible or even practical to do?

Possible yes, practical no.

> Your thoughts please?

Maybe if you want a compiled package you should use a compiled language?

Scripting languages have a lot of advantages, but this isn't really their domain.

Rob


More information about the Toybox mailing list