DroidScript wiki

(was AndroidScript) unofficial documentation by the community

User Tools

Site Tools


sample_code:shell

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sample_code:shell [2015/03/20 21:32] (current)
stevegarman created
Line 1: Line 1:
 +====== shell access ======
 +This code will allow you to execute shell code as if it has been typed in a terminal application.
  
 +This should work on devices whether they have been rooted or not.
 +
 +<code JavaScript shell.js>​
 +
 +var globs={};​
 +
 +//​Called when application is started.
 +function OnStart()
 +{
 +  var lay = app.CreateLayout("​Linear","​Vertical,​FillXY"​);​
 +  var txtopts = "​MultiLine,​ReadOnly,​NoKeyboard,​NoSpell,​Left,​MonoSpace"​
 +  var txt = app.CreateTextEdit("​Please wait",​ -1,​ -1,​ txtopts);​
 +  lay.AddChild(txt);​
 +  app.AddLayout(lay);​
 +
 +  globs.sys = app.CreateSysProc( "​sh"​ );​
 +  globs.stdout = "/​sdcard/​stdout.txt"​
 +  globs.redir = "​ >>​ "​ + globs.stdout + "​ 2>&​1 \n ";​
 +
 +  sysEmpty();​
 +  //​check PATH variable
 +  sysEcho( "​$PATH",​ true );​
 +  sysOut("​SJG=Steve ;​export SJG"​);​
 +  //​list all environment variables
 +  sysOut( "​set",​ true );​
 +  sysOut( "​set | grep STOR",​ true );​
 +  //​display filesystem
 +  sysOut("​df",​ true);​
 +  //​Change directory
 +  sysOut("​cd /​sdcard"​);​
 +  //​show path
 +  sysOut("​pwd",​ true);​
 +  sysOut("​ls -l"​);​
 +  sysStamp();​
 +  //​test stderr
 +  sysOut("​badCommand"​);​
 +  sysOut("​ls -l /​data"​);​
 +  sysOut("​exit"​);​
 +  txt.SetText(app.ReadFile(globs.stdout));​
 +  app.Alert(app.GetEnv("​EXTERNAL_STORAGE"​));​
 + } 
 +
 +function sysEmpty(stamp)
 +{
 +    app.DeleteFile(globs.stdout);​
 +    if(stamp) sysStamp();​
 +}
 +
 +function sysOut (cmd,​ stamp)
 +{
 +   if(stamp) sysStamp();​
 +   globs.sys.Out(cmd + globs.redir)
 +}
 +
 +function sysEcho(msg,​ stamp)
 +{
 +    sysOut('​echo "'​ + msg +'"',​ stamp );​
 +
 +
 +function sysStamp()
 +{
 +    var d = new Date();​
 +    sysEcho("​**************************"​);​
 +    sysEcho(d.toISOString()+"​ *"​);​
 +    sysEcho("​**************************"​);​
 +}
 +
 +
 +</​code>​
sample_code/shell.txt · Last modified: 2015/03/20 21:32 by stevegarman