1

I have a bash script which I want to start with arguments by an app. Currently I can generate the command, but how can I execute it? This app is for jailbroken phones only. Any solutions for this? I searched a lot, but didn't found anything.

Alex Zavatone
  • 4,106
  • 36
  • 54

1 Answers1

3

If this is a jailbreak app, you can run the command by passing it to the system() function that's part of the OS, or one of the exec functions.

So, if you decide to install your script at /Applications/MyApp.app/myscript.sh, then in your app, you could use:

int result = system("/Applications/MyApp.app/myscript.sh argument1 argument2");

You can install the script as I've shown, in your app's folder, as a resource, or in /usr/bin, or /bin, or wherever you like.

You don't mention whether or not you're installing your app in /Applications, as most jailbreak apps are, or in the normal /var/mobile/Applications/* locations.

If the above code doesn't work, please clarify where you want to install your app. Also, depending on whether or not your script requires root privileges, there may be more work necessary. There are some complications if you want to run a shell script as root.

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • so, the app is installed as a normal app and in /Application/MyApp.app/ the script does not require root I tried it with system() but it didn't worked –  Jan 20 '13 at 23:29
  • 1
    What went wrong? `system()` returns an error code result. What was the error code? Did you remember to make your sh script executable with `chmod`? Login to the phone and do an `ls -al /Applications/MyApp.app/` and let us know what the permissions look like on your script. Thanks. – Nate Jan 21 '13 at 00:06
  • the script is downloaded from cydia, as a library and can be executed everywhere, the system returned 32512 –  Jan 21 '13 at 07:58
  • Your first comment isn't quite clear. If you install an app as a *normal* application, then it will **not** be in `/Applications`. It will be under `/var/mobile/Applications`. Which one is it? Also, you didn't answer my question about the permissions on your script. Is your script **executable**, after it has been deployed to your device? Can you run it directly from the command line? – Nate Jan 21 '13 at 08:37
  • oh sorry... it is installed as a normal Application and is in /var/mobile/Applications/... The script is in /usr/bin/ and has permissions to be executed, i can run it directly with mobile terminal (terminal emulator) –  Jan 21 '13 at 09:00
  • Ok, so that's the problem. If it is installed in `/var/mobile/Applications/` as a normal app, then it's inside the normal app *sandbox*. That means, the app cannot access (read/write/execute) files outside of that sandbox. So, `/usr/bin` is not available. So, the next question is "can you install your app in `/Applications/` instead"? If not, then it's not really a true jailbreak app, even if running on a jailbroken phone. By the way, if you're developing this for other people, like Cydia users, then they will get the app installed to `/Applications`, and you should, too. – Nate Jan 21 '13 at 09:22
  • and.. how can I manage it to install to `/Applications/` instead of `/var/mobile/Applications/` Offtopic: My App crashes on firststart when I press Run from Xcode –  Jan 21 '13 at 09:56
  • 1
    There's many ways to do that. If you have terminal/shell access, you can just ssh into your device. `cd` to `/var/mobile/Applications/` and do `find . -name MyAppName.app`. This will find where your app is installed. Then just copy the whole `MyAppName.app` folder to `/Applications`. For example, after changing to the proper directory, `cp -Rp ./MyAppName.app /Applications/`. You might then want to uninstall the original version, or else you're going to see two identical icons. Reboot, or restart springboard, or use `su mobile -c uicache` to force springboard to see the new app. Done. – Nate Jan 21 '13 at 11:45
  • That gets your app out of the *sandbox* that normal 3rd-party iOS apps are in. Regarding your crash, that usually is because the app isn't properly signed, although I really don't have enough information to help with that. You should also post a new question if you have another, separate problem. – Nate Jan 21 '13 at 11:47
  • ok, thanks, i will try to move it to `/Applications` and execute the script. –  Jan 21 '13 at 11:52
  • but creating a sharing folder in `/Applications` is not possible, that destroys my concept –  Jan 21 '13 at 12:33
  • I don't know what *creating a sharing folder in /Applications* means. If you have additional requirements for solving your problem, they need to be posted in your question. If you aren't willing to install your app in /Applications, it's not really a jailbreak app. – Nate Jan 22 '13 at 02:22