landley / toybox

toybox
http://landley.net/toybox
BSD Zero Clause License
2.4k stars 335 forks source link

Most scripts use /bin/bash, which not all systems have. Use /usr/bin/env instead? #494

Open xplshn opened 5 months ago

xplshn commented 5 months ago

Possible fixes:

Else, toybox can't be build on systems without bash, like Alpine, obviously it would be trivial to install bash but reducing dependencies (on non-POSIX things) would be great.

oliverkwebb commented 5 months ago

relevant

landley commented 5 months ago

If /bin/bash isn't a reliable absolute path, why would /usr/bin/env be a reliable absolute path?

Toybox has scripts/prereq/build.sh to produce prerequisite commands in a portable way (commit 3bbc31c78b41 has an example invocation used to build toybox outside homebrew on macos, for example). It doesn't include toysh yet because it's still in pending, but once that's promoted it should include that and you should be able to "sh scripts/make.sh". There's a planned rewrite/replacement of kconfig that should also allow .config file generation to run portably.

It's not finished yet, but that's the plan.

xplshn commented 5 months ago

Because not all people have installed bash at /bin/bash. And an error message that reads: make: scripts/genconfig.sh: No such file or directory when scripts/genconfig.sh DOES indeed exist is not quite useful for such people (me including).

/usr/bin/env should be used if you want to run under bash because env will lookup bash in the user's $PATH and run it.

For example, I have bash under ~/.local/bin because my package manager (bigdl) installed it there when I ran it as a user.

landley commented 5 months ago

If /bin/bash can't be trusted to be at that location, why would /usr/bin/env be any different?

oliverkwebb commented 5 months ago

If /bin/bash can't be trusted to be at that location, why would /usr/bin/env be any different?

Because env searches your path, doing a direct /bin/bash does not, doing bash scripts/make.sh either directly or telling the makefile to do it would work I think.

enh-google commented 5 months ago

Because env searches your path, doing a direct /bin/bash does not, doing

his point is "if /bin/bash isn't there, what reason do i have to assume /usr/bin/env exists?".

oliverkwebb commented 5 months ago

On Mon, Apr 15, 2024 at 14:25, enh-google @.***(mailto:On Mon, Apr 15, 2024 at 14:25, enh-google < wrote:

Because env searches your path, doing a direct /bin/bash does not, doing

his point is "if /bin/bash isn't there, what reason do i have to assume /usr/bin/env exists?".

env is a pretty standard POSIX utility that’s on most-everything that runs UNIX, bash is not

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

xplshn commented 5 months ago

env is a pretty standard POSIX utility that’s on most-everything that runs UNIX, bash is not

Absolutely! But that's not the problem, since Landley wants to use Bash syntax, the only problem is that some people may have bash installed in other directories. For example; I wouldn't install software such as Bash into my system, instead, I put such software under ~/.local/bin or /opt/compat/*

oliverkwebb commented 5 months ago

The solution is to tell your makefile to run a scriptwith bash (Searches path) instead of calling the shebang.

The following diff fixes this:

@@ -14,7 +14,7 @@ all: toybox
 KCONFIG_CONFIG ?= .config

 toybox generated/unstripped/toybox: $(KCONFIG_CONFIG) *.[ch] lib/*.[ch] toys/*/*.c scripts/*.sh Config.in
-       scripts/make.sh
+       bash scripts/make.sh

 .PHONY: clean distclean baseline bloatcheck install install_flat \
        uninstall uninstall_flat tests help change \
@@ -32,7 +32,7 @@ $(KCONFIG_CONFIG): $(KCONFIG_TOP)
 $(KCONFIG_TOP): generated/Config.in generated/Config.probed
 generated/Config.probed: generated/Config.in
 generated/Config.in: toys/*/*.c scripts/genconfig.sh
-       scripts/genconfig.sh
+       bash scripts/genconfig.sh

 # Development targets
 baseline: generated/unstripped/toybox
xplshn commented 5 months ago

Its the same. However, it is better to fix the scripts shebang instead of modifying the Makefile to call them with bash.

tpimh commented 5 months ago

See previous discussion here: https://github.com/landley/toybox/issues/77