I am trying to run a bash script. Have installed bash via "opkg install bash". Can see bash and use it from command line:
bash --version
GNU bash, version 5.2.32(1)-release (x86_64-openwrt-linux-gnu)
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
While testing a script that was failing, I noticed the script interpreter wants to use "ash", even when the she-bang says to use "bash". I tested this with "echo $SHELL":
Just wondering if your script has Windows-style line endings (CRLF)? Cuz if it does then the kernel is trying to execute /bin/bash\r which fails causing a fallback to the system's default shell ash..
Everything you are trying is from an ASH shell.
The stripped down OpenWrt ash does not read the shebang, it does not need to 99.99% of the time. It never has, this is not something new and it is not a bug.
You can:
Run, one way or another, bash to start a bash shell, then run your bash script.
Make bash the default shell (editing /etc/passwd), but this is somewhat risky as one day you might sysupgrade and keep config only to find bash is not installed leaving you bricked.
Now your example:
When you open a terminal session, it is an ash terminal session (unless you changed the default in /etc/passwd).
If you just pipe the required command into bash, you have to escape the double quotes so ASH knows what you mean:
OOOH! I stand corrected!
This has not always been the case. Admittedly I have not needed to use bash on OpenWrt much since ~2018 and back then....
I just searched the wiki, and here it is:
If you want to convert bytes to human-readable units (as your code seems to suggest), we have this implemented as a function in adblock-lean, and it doesn't require bash:
You can re-use the code almost as is, provided that your work has a license compatible with GPLv2.
Details on borked chars:
The borked characters were not "\n\b" (newline, line-break). I actually don't know what it was, but I do know what I did to fix. I deleted all leading whitespace on each line (formatting whitespace), then put the whitespace back in manually.
Which explains why something like this would fail in the script, but work fine when copy pasted into the command line:
units=("B" "KiB" "MiB" "GiB" "TiB")
I don't know what those borked whitespace characters were. The code was written on a different system, then emailed to myself, and copy pasted into a terminal. Lots of places things could have gone wrong.
I don't want to be pedantic, but I don't think your conclusion could be found in the wiki, it just stated a '#!/bin/bash' she-bang wouldn't work, without telling why. I think it just stood there because by default no bash is available.
AFAIK the she-bang is not read by ash. Ash just (after forking) hands the file(path) over to the loader in libc (exec..()) which does the actual work.
No, please do be, it helps to get down to the to the possibilities.
I did not come to my conclusion just by reading the outdated wiki page, but by previous experience way back with LEDE (17x) and OpenWrt 18x on a particular backporting project that used a lot of bash scripts.
My conclusion was reinforced by the wiki page when I read it the other day.
With your pedantic hat on, do you have any suggestions for the wiki page? It is still very much an old document needing a rewrite in my opinion.
Well, the answer on the question if bash can be installed is a bit conservative. Although it won't hurt to use flash memory sparingly, many routers nowadays can install bash (or vim) on internal flash without problems. Yet the answer on the question should not be something like 'sure, go ahead!', as there are still enough routers where that's not true.
Further the line
changed your default shell to #!/bin/bash
is wrong. It should be /bin/bash here.
And last, I wonder if the section you changed shouldn't be expanded with the comment that nowadays you should consider using ucode for OpenWrt scripts. On the other hand, as far as I could find ucode is not documented in the wiki yet.
Now you've done it, mentioned the Top Secret "uc#e" project. Run and hide! No-one is supposed to know anything about it...
Seriously though, ucode is of little use when considering the huge volume of (b)ash scripts and scripting examples out there.
Maybe one day someone will write a useful wiki guide.... I wouldn't be holding my breath if I was waiting though...
The whole document should be re-written in my opinion. Any volunteers?
Beyond flash usage (and upgrade safety), there is another issue making this not advisable. bash (still) is rather slow to start, while you won't notice this on x86 worth using, it is very noticable on weak CPUs (mips and friends). Until ~15 years ago, I was still using a SUN SPARCstation 20 with a TI SuperSPARC SM50 CPU (50 MHz, sun4m, 196 MB RAM, cg6 graphics) - using bash as interactive/ login shell caused very noticable delays on getty, ssh, etc. Changing the default user shell to busybox-ash brought a massive resposiveness improvement, while having more comfort features than the (equally fast) dash.
Modern ARMv8 routers probably won't feel the pain for interactive use anymore, but non-interactive use would still noticably suffer from bash as /bin/sh.