Is running .sh files allowed (without Rooting Phone)

giga

Well-Known Member
Licensed User
Longtime User
Help Please.

When i try this

B4X:
Sub manualstart_Click()

Dim i As Intent    
i.Initialize(i.ACTION_CALL, "file:///sdcard/user/config.sh")    
i.SetType("bin/sh")    
Try       
StartActivity(i)    
Catch        
ToastMessageShow("Cannot start file.", True)
Log(i)
End Try

End Sub
I get the error
(Intent) Intent { act=android.intent.action.CALL dat=file:///sdcard/user/config.sh typ=bin/sh flg=0x20000 }

Thanks for any help.

I tried Phone.Shell

and got this error

java.io.IOException: Error running exec(). Command: [config.sh, /sdcard/user/config.sh] Working Directory: null Environment: null
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code works correctly:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim p As Phone
   File.WriteString(File.DirRootExternal, "1.sh", "#!/bin/sh" & CRLF & "echo test")
   Dim out, err As StringBuilder
   out.Initialize : err.Initialize
   p.Shell("sh", Array As String(File.Combine(File.DirRootExternal, "1.sh")), out, err)
   Log("out: " & out)
   Log("err: " & err)
End Sub

Note that you should post the full error message from the logs (you can copy with right click).
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User

Thanks Erel,

Does it require ROOT?
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
Erel,

i used the code you recommended and got permissin denied. So i tried the su code http://www.b4x.com/forum/basic4andr...nning-shell-commands-superuser.html#post39820

B4X:
Sub manualstart_Click()

Dim prs As Phone
Dim Command, Runner As String
Dim out, err As StringBuilder
Dim Result As Int
out.Initialize : err.Initialize
Runner = File.Combine(File.DirInternalCache, "runner")
Command = File.Combine(File.DirRootExternal & "/config", "config.sh")
File.WriteString(File.DirInternalCache, "runner", "su < " & Command)
Result = prs.Shell("sh", Array As String(Runner), out, err)
Log("out: " & out)
Log("err: " & err)


End Sub

and i am getting

err: su: uid 10044 not allowed to su

i also tried root,user and get

err: root: permission denied

Any Ideas would be great. Thanks again to all.
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
Can you post the exact message?

'su' requires root.

I've tested the code I posted of Galaxy Nexus and it works fine. Though such shell commands may not run properly on all devices.


This is the exact error message in the log.

err: su: uid 10044 not allowed to su
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
Erel,

As far as I can tell, I got this one to work without the su .

B4X:
Sub manualstart_Click()

Dim prs As Phone
Dim out, err As StringBuilder
out.Initialize : err.Initialize
prs.Shell("sh", Array As String(File.Combine(File.DirRootExternal & "/config", "config.sh")), out, err)
Log("out: " & out)
Log("err: " & err)

End Sub
The problem was in the config.sh I used writeline instead on write to create the sh which caused new lines for each word vertically.

I am not getting the error now, this is the log now.

out:
err:

But I can't see if the sh(script) is running.
Is there a way of showing a DOS Shell or Android Terminal to display the progress????

Thanks for sticking with me on this.
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
You could install something like a Terminal Emulator to see it running.

Thanks NJDude
Good Idea, At this stage I will try that at least for testing.

I was hoping there was something included in Android that I could use as GetApplicationIntent.
I could send a script to and show the progress on the screen.
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
You could install something like a Terminal Emulator to see it running.

Thanks NJDude for the suggestion.

I tried Terminal Emulator per their instructions here: https://github.com/jackpal/Android-...ng-Android-Terminal-Emulator-from-another-App

But I cant get it to work with jackpal.androidterm.RUN_SCRIPT

It does come up jackpal.androidterm (without RUN_SCRIPT) :sign0148: but nothing but SOM(start of message)

B4X:
Sub manualstart_Click()
Try 
 Dim Intent1 As Intent
 Dim pm As PackageManager
 Intent1 = pm.GetApplicationIntent("jackpal.androidterm.RUN_SCRIPT")
 Intent1.AddCategory("DEFAULT")
 Intent1.PutExtra("jackpal.androidterm.iInitialCommand", "echo 'Hi there!'")
 If Intent1.IsInitialized Then
StartActivity (Intent1)
 End If
   Catch
  ToastMessageShow ("Failed to launch app! Is it installed?", True)
 End Try 

End Sub

I also added to the manifest
B4X:
<uses-permission android:name="jackpal.androidterm.permission.RUN_SCRIPT" />
No luck. Any help, ideas are appreciated.
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
Erel,

I believe the problem is this:

Android does not have the rsync binary by default. I can't put the binary in because it is permission denied on the system/xbin directory.

The script echos entirely in the log, when I put echo in front of the rsync command but nothing happens beyond that.

Thanks, If you have any ideas i would appreciate your expertise. :sign0188:

Sample Code of config.sh

#!/bin/sh
echo rsync -e 192.168.1.10 etc....

Is there another location I can run a binary from??
Can I reference the binary another way in the app??
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…