WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Supporting "bashisms" in Core  (Read 5202 times)

Offline florian

  • Full Member
  • ***
  • Posts: 116
    • Home Page
Supporting "bashisms" in Core
« on: February 16, 2012, 03:50:57 PM »
Hi! Long time no see! I spend more time away from the computer nowadays (a good thing, really!) ... but I have also been shell scripting a bit ;)

Typically I start a script with #!/bin/sh and then try to keep it portable... at least across a Debian-based distro, where /bin/sh is dash, and Core, where /bin/sh is busybox.

That's great. But sometimes some "bashisms" makes the script (and my life) easier. Busybox shell is no bash (no arrays, etc...) but it seems to support quite a few bash constructs (including useful "substitution patterns" between ${ and }, etc... ) that dash doesn't grasp. Using bashisms moderately, I'd then end up with scripts which still work well in Core (with busybox sh) and also work well in other distro, but this time with bash, no longer dash.

Ok, again, that's great. But the shebang remain an issue with those scripts: #!/bin/bash is required to run them outside of Core but this shebang doesn't work in Core (assuming bash extension isn't loaded). To correct this, I created a one-liner /bin/bash to accommodate the #!/bin/bash syntax in Core:
Quote
$ cat /bin/bash
# exec busybox sh -c "$0 $@"

Since this /bin/bash is only a few bytes, I submit it for your consideration to include it in the base. This way one could use scripts with #!/bin/bash shebangs and "moderate" bashism. Also useful as nowadays more and more people start writing any shell script with a #!/bin/bash shebang anyway.

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: Supporting "bashisms" in Core
« Reply #1 on: February 16, 2012, 04:15:10 PM »
This could lead to complaints of failing scripts with no clue to the problem.
I think it is a bad idea.

Offline maro

  • Hero Member
  • *****
  • Posts: 1228
Re: Supporting "bashisms" in Core
« Reply #2 on: February 16, 2012, 09:10:16 PM »
This could lead to complaints of failing scripts with no clue to the problem.
I think it is a bad idea.
+1

AFAIK the BusyBox '/bin/sh' is the 'ash' (AKA: "Almquist shell") and I've so far found that this man page describes it features pretty well. That includes parameter expansion (e.g. "${parameter%%word}" and "friends", but does not mention "${parameter/pattern/replace}" which at least the Core BusyBox supports as well).

Ever since I started to use more shell scripts than Perl scripts to do (simple) tasks (which kind of coincides with my discovery of TC, so both events might also be in "chicken and egg" relationship with each other) I've tried to limit myself to what is supported by BusyBox in those shell scripts. The few occasions where I felt the need of using an (associative) array I branched out to use 'awk' (typically inside the shell script). Clearly that is something I've learned by reading Robert's scripts here. So maybe that could also be an option for your attempts to create scripts that are meant to work across different distributions.