a-shell icon indicating copy to clipboard operation
a-shell copied to clipboard

Is it possible to run shell scripts in A-shell?

Open YousufSSyed opened this issue 1 year ago • 18 comments

I want to have bash commands in a .sh file and run them by entering in ./shell-script.sh

YousufSSyed avatar Nov 10 '22 17:11 YousufSSyed

The good news is: you can run shell scripts in a-Shell. If you create a file, and it begins with #! sh or #! /bin/sh (to tell the system that it's a shell script, not a Lua, Python or Perl script), and set the executable bit with chmod -x file, then it will be executed when you type ./file.

The bad news is: it's a sh script (more precisely, dash), not a bash script, because bash is covered by the GPL. Bash advances features are not available, but a lot of things are present.

holzschu avatar Nov 10 '22 17:11 holzschu

So I got a shell script working by doing sh ~/path/to/script.sh in case anyone else is reading this.

And here are some other things I started thinking about:

  • I searched github issues for bash, for why Bash can't be used for A-Shell, and you said in another issue it's because of the GPL license (you mean GPLv3 right?).
  • You said here that Zsh is nearly impossible with system limitations. What are system limitations that prevent including Zsh? If it was possible, would there be licensing issues with Zsh? I know that macOS Catalina switched from Bash to Zsh since Bash uses GPLv3.
  • What made you pick Dash?
  • Have you thought of making an FAQ section in either the readme or make a wiki with that and other stuff, for those three points above?
  • Maybe you probably did, but have you tried compiling binaries of Bash and Zsh for iOS to run in A-shell? If so, does that work? And could you give instructions for how you did it, even if it may not have worked?

YousufSSyed avatar Nov 19 '22 14:11 YousufSSyed

  • the issue with the GPL is ancient, and goes back to the GPLv2. To the best of my knowledge (and according to what is published on the FSF web site) both GPLv2 and GPLv3 are incompatible with AppStore distribution.
  • About zsh and dash: cross-compiling a program is always difficult. Cross-compiling a shell program even more so, because there are so many places where you have to fine-tune things to make it work. Also, Apple refuses binaries that contains references to the curses library. So I picked up dash because it is the simplest shell program that does not have a GPL license and does not require curses.
  • There is a Discord server, whose address you can find in the "help" command inside a-Shell, where we discuss these things.
  • I have tried cross-compiling many programs, and failed very often. Assuming compiling the program uses the classical ./configure ; make, cross-compiling looks like:
IOS_SDKROOT=$(xcrun --sdk iphoneos --show-sdk-path)
./configure CC=clang CXX=clang++ CFLAGS="-arch arm64 -O2 -miphoneos-version-min=14.0 -isysroot $IOS_SDKROOT -fembed-bitcode" CPPFLAGS="-arch arm64 -O2 -miphoneos-version-min=14.0 -isysroot $IOS_SDKROOT" CXXFLAGS="-arch arm64 -O2 -miphoneos-version-min=14.0 -isysroot $IOS_SDKROOT -fembed-bitcode" LDFLAGS="-shared -arch arm64 -O2 -miphoneos-version-min=14.0 -isysroot $IOS_SDKROOT"  --build=x86_64-apple-darwin --host=armv7-apple-darwin 
make

(options may vary from program to program). There will usually be lots of compilation errors, which you have to fix by understanding what is going wrong and how to replace it. Once it compiles, you have to adapt the program to iOS: make sure it releases memory when leaving, re-initialize variables when re-entering, send output to a different channel because stdout does not exist, edit places where it uses fork() and exec(), replace them with ios_fork(), link with the ios_system framework. Once you have that, you are ready to test, and fix more bugs.

holzschu avatar Nov 19 '22 15:11 holzschu

@holzschu Oh can you see that I mentioned at the NewTerm repo? Well I'm wondering how Zsh and Bash can work in NewTerm but not A-shell. Maybe it has to do with NewTerm running on jailbroken devices?

YousufSSyed avatar Nov 19 '22 19:11 YousufSSyed

If you're running on a jailbroken device, then you're not distributing on the AppStore, so the restriction from the FSF does not apply (their problem is specifically with the distribution on the AppStore). Also, on a jailbroken device, you have access to fork() and exec() and all the other functions, so you can actually compile on the device and distribute the binaries. It's much easier. I started this project specifically because I wanted to have the same kind of functionality without having to jailbreak. As you have seen, there are still differences, but it's getting there: one year ago, there were no shells at all, now we have dash.

holzschu avatar Nov 19 '22 19:11 holzschu

Do you mean compile on or for the device? Can you elaborate?

YousufSSyed avatar Nov 20 '22 16:11 YousufSSyed

With a jailbroken device, you have a compiler like gcc installed on your device, so you can actually compile new commands on the device, for the device.

holzschu avatar Nov 20 '22 17:11 holzschu

Couldn't you use the same flags and commands to compile Bash and Zsh for iOS on Mac, as you would on a jailbroken iOS device? Is compiling for iOS devices device specific, so if it compiles on one iOS device it doesn't mean it can compile on another, for whatever reasons?

YousufSSyed avatar Nov 20 '22 18:11 YousufSSyed

I don't understand your question. Cross-compiling (compiling on one platform for another) is always difficult. There are no shortcuts. It's not the same platform, it's not the same flags. Many compile scripts assume that you can run the binaries that you just created, which is impossible here. On top of that, there are all the specificities of iOS to consider: since there is no fork/exec, I need to clear up memory when the program leaves. There is no stdout, so I have to rewrite output functions. I also have to rewrite parts of the command that call other commands and so on.

holzschu avatar Dec 02 '22 07:12 holzschu

Ok. I took a look at the Discord server but didn't find it in the readme, I had to search "Discord" in the whole repo to get the server link since I never saw it mentioned elsewhere before that. Why don't you add an FAQ section in the readme or make a repo wiki?

Or at least put the link to the Discord in the readme?

YousufSSyed avatar Dec 03 '22 00:12 YousufSSyed

  • There is a Discord server, whose address you can find in the "help" command inside a-Shell, where we discuss these things.

I don’t think it can be more clear than that.

miron avatar Jan 05 '23 14:01 miron

I want to ask that is it possible to compile some applications and distribute them in https://github.com/holzschu/a-Shell-commands ?

CarlGao4 avatar May 22 '23 07:05 CarlGao4

Absolutely!

holzschu avatar May 22 '23 11:05 holzschu

Sorry that my spell checker replaced the wrong word for me ... I wanted to ask that is it possible to compile bash and distribute it using https://github.com/holzschu/a-Shell-commands ?

CarlGao4 avatar May 23 '23 00:05 CarlGao4

a-Shell-commands is for commands that have been compiled into WebAssembly (commands compiled into Arm64 binaries, such as dash, have to be distributed with the app itself). As of today, WebAssembly can only be used for simple commands. A fully working shell like bash is way too complicated for it.

holzschu avatar May 23 '23 05:05 holzschu

commands compiled into Arm64 binaries, such as dash, have to be distributed with the app itself

But why?

CarlGao4 avatar May 31 '23 04:05 CarlGao4

That is the entire principle of Apple's AppStore: to protect users, all the binary content of the app must be submitted to the review process. It is not possible to add more binary content to an app after it has been approved for distribution. This is both a legal interdiction, and a physical interdiction: binaries are stored in a separate place and encrypted.

The good news is: once an app has been approved, as a user you're pretty sure that it won't do something forbidden, like steal your personal data. The bad news is: it's very hard to add functionalities to an existing app. WebAssembly binaries get an exception, just like x86 binaries for iSh, because technically they aren't "binaries", in the sense that they cannot be executed by the phone.

holzschu avatar May 31 '23 05:05 holzschu

Thanks for your explanation! Just like what I met when binding macOS .app applications, after I sign it, the program will refuse to run if I add more files into it

CarlGao4 avatar May 31 '23 06:05 CarlGao4