Android Scripting Environment: Shell Scripting and Android | Unix Linux Forums | Android

  Go Back    


Android Brand new forum for folks interesting in Linux and Java-based Android OS and multi-platform mobile computing in general.

Android Scripting Environment: Shell Scripting and Android

Android


Tags
android, android scripting environment, ase, bash for android, perl for android, sl4a

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-03-2011
Neo's Avatar
Neo Neo is offline Forum Staff  
Administrator
 
Join Date: Sep 2000
Last Activity: 21 October 2014, 4:42 PM EDT
Location: Asia pacific region
Posts: 13,023
Thanks: 522
Thanked 877 Times in 402 Posts
Android Scripting Environment: Shell Scripting and Android

I just upgraded to Android 2.2 from 2.1. The GPS issue that was troublesome in 2.1 seems to have been fixed. Some of web browsing seems faster, but it could just be my connection is better today Flash works in some browsers but not very good and it is too slow for Flash apps designed for the non-mobile web.

Afterwords, I did some digging around and found some young, but interesting none-the-less, shell script applications for Android. Naturally, these apps have a ways to go. Unix and Linux was not built "in a day" as they say (Rome was not "built in a day") so don't expect too much yet!

Android Scripting Environment

Quote:
Scripting Layer for Android (SL4A) brings scripting languages to Android by allowing you to edit and execute scripts and interactive interpreters directly on the Android device. These scripts have access to many of the APIs available to full-fledged Android applications, but with a greatly simplified interface that makes it easy to get things done.

Scripts can be run interactively in a terminal, in the background, or via Locale. Python, Perl, JRuby, Lua, BeanShell, JavaScript, Tcl, and shell are currently supported, and we're planning to add more. See the SL4A Video Help playlist on YouTube for various demonstrations of SL4A's features.

SL4A is designed for developers and is alpha quality software. Please report bugs and feature requests on the issues list.
Current downloads.

---------- Post updated at 19:44 ---------- Previous update was at 19:25 ----------

Here are some example PERL scripts on Android, for example:


Code:
use Android;
 
sub dumphash {
   my $hash = pop;
   my $key, $down;
   for $key (keys %$hash) {
      if ($key eq "result") {
         $down = $hash->{$key};
         for $key (keys %$down) {
            print "result ==> $key => $down->{$key}\n";
         }
      }
   }
}
 
my $a = Android->new();
my $r;
 
$r=$a->startSensing("SENSOR_ACCELEROMETER");
&dumphash ($r);
# ^c (Ball-C) to break
for($ii=0;$ii<2000;$ii++){
  $r=$a->readSensors("SENSOR_ACCELEROMETER");
#  printf ("r %3.0f  p %3.0f  a %3.0f  \n", $r->{'result'}->{'roll'},  $r->{'result'}->{'pitch'},  $r->{'result'}->{'azimuth'});
  printf ("x %5.2f  y %5.2f  z %5.2f  \n", $r->{'result'}->{'xmag'},  $r->{'result'}->{'ymag'},  $r->{'result'}->{'zmag'});
#  &dumphash ($r);
}
$r=$a->stopSensing("SENSOR_ACCELEROMETER");
&dumphash ($r);

---------- Post updated at 19:47 ---------- Previous update was at 19:44 ----------

Here is a very crude Android webserver written in PERL:


Code:
use Android;
my $a = Android->new();

#connect to: http://localhost:8888

use IO::Socket;
use IO::Select;

$ctrl_port = 8888;
$timeout_mins = 30; # 30 min. timeout
$selectcnt = 0;
$debug = 0;
$noisy = 0;


while ($arg = shift) {
    if ($arg =~ /help/) {
        print "Useage:\n";
        print "perl $0 [debug] [noisy] [ctrl_port=#;8888] [to=#min.]\n";
        print "If [srvr=#] is missing, report client address\n";
        exit;
    }
    if ($arg =~ /debug/) {
        $debug++;
        print "ARGS debug\n";
    }
    if ($arg =~ /noisy/) {
        $noisy = 1;
        print "ARGS noisy\n";
    }
    if ($arg =~ /to=(.+)/) {
        $timeout_mins = $1;
        print "ARGS to=$timeout_mins\n";
    }
    if ($arg =~ /ctrl_port=(.+)/) {
        $ctrl_port = $1;
        print "ARGS ctrl=$ctrl_port\n";
    }
}


$ctrl_lstn_sock = IO::Socket::INET->new (
    LocalPort => $ctrl_port,
    Listen => 5, 
    Reuse => 1
);
die "Can't create socket for listening: $!" unless $ctrl_lstn_sock;

my $readable = IO::Select->new;     # Create a new IO::Select object
$readable->add($ctrl_lstn_sock);          # Add the lstnsock to it


sub debug_info {
    print "CONN_DBG:lstnsock      $lstn_sock\n";
}


$tend = time + $timeout_mins * 60;
$tstart = time + 0;
$tickdelta = 1;
$tick = time + $tickdelta;

while(1) {
    if (time >= $tend) {
        print STDERR "\n\n=== $timeout_mins minutes timeout occured, stop\n\n";
        last;
    }
    if (time >= $tick) {
        $tick = time + $tickdelta;
        $now_string = localtime;
        print STDERR "\r$now_string: PID $$ up ", time - $tstart, " S $selectcnt connections \r";
    }

    # Get a list of sockets that are ready to talk to us.
    my ($ready) = IO::Select->select($readable, undef, undef, $tickdelta);
    foreach my $curr_socket (@$ready) {
        # Is it a new connection?
        &debug_info, if ($debug);
        $selectcnt++;
        if($curr_socket == $ctrl_lstn_sock) {
            print "============================ SELECT Control READY $selectcnt =========================\n$now_string\n", if ($noisy);
#---------------------- Form Control Page handler  -----------------------
            $now_string = localtime;
            $ctrl_sock = $ctrl_lstn_sock->accept;
            $addr = $ctrl_sock->peeraddr ();
            if (defined $addr) {
                $client_ip = inet_ntoa ($addr);
            } else {
                $client_ip = "unknown";
            }
            $commands = "";
            $ctrlportcnt++;
            print "---------------------------- CTRL HTML $ctrlportcnt begin ----------------------------\n", if ($noisy);
            while (<$ctrl_sock>) {
                if (/^ *\n*\r*$/) {
                    last;
                }
                print "CTRL $_", if ($noisy);
                if (/GET \/portfwrd_ctrlport\.pl\?([^ ]+) HTTP/) {
                    $commands = $1;
                }
            }
            print "---------------------------- CTRL HTML $ctrlportcnt   end----------------------------\n", if ($noisy);
            print "CTRL parameters: >$commands<\n", if ($noisy);
            print "CTRL $now_string: $client_ip connected to control port\n", if ($noisy);

            # ------------------- parameters input processing
            @cmd_param_pairs = split ('&', $commands);
            undef %params;
            foreach $cmd_param_pair (@cmd_param_pairs) {
                ($name, $param) = split ('=', $cmd_param_pair);
                print "CTRL_CMD $name=$param\n", if ($noisy);
                $params{$name} = $param;
            }


            if ($params{'debug'} eq "on") {
                $debug = 1;
            } else {
                $debug = 0;
            }
            if ($params{'noisy'} eq "on") {
                $noisy = 1;
            } else {
                $noisy = 0;
            }


            print $ctrl_sock "HTTP/1.0 200 OK\r\n\r\n<html><body>\n";
            print $ctrl_sock "$now_string: you have connected to the control port from $client_ip\n";


            print $ctrl_sock "<form action=\"/portfwrd_ctrlport.pl\" method=\"PUT\">\n";
            print $ctrl_sock "    <table border=\"1\" cellpadding=\"5\" cellspacing=\"3\">\n";

            print $ctrl_sock "        <tr>\n";
            print $ctrl_sock "            <td>Parameters</td>\n";
            print $ctrl_sock "            <td>Descriptions</td>\n";
            print $ctrl_sock "        </tr>\n";


            if ($debug) {
                $buf = "checked";
            } else {
                $buf = "";
            }
            print $ctrl_sock "        <tr>\n";
            print $ctrl_sock "            <td><input type=\"checkbox\" $buf name=\"debug\">debug</td>\n";
            print $ctrl_sock "            <td>debug, prints to console</td>\n";
            print $ctrl_sock "        </tr>\n";

            if ($noisy) {
                $buf = "checked";
            } else {
                $buf = "";
            }
            print $ctrl_sock "        <tr>\n";
            print $ctrl_sock "            <td><input type=\"checkbox\" $buf name=\"noisy\">noisy</td>\n";
            print $ctrl_sock "            <td>noisy, prints to console</td>\n";
            print $ctrl_sock "        </tr>\n";


            print $ctrl_sock "        <tr>\n";
            print $ctrl_sock "            <td><input type=\"submit\" value=\"Set Config\"></td>\n";
            print $ctrl_sock "            <td>$selectcnt connections</td>\n";
            print $ctrl_sock "        </tr>\n";


            print $ctrl_sock "    </table>\n";
            print $ctrl_sock "</form>\n";
            print $ctrl_sock "</body></html>";

            $ctrl_sock->close;
            $ctrl_sock = 0;
        } else {
            # multiple connection?
            print STDERR "ERROR  s $curr_socket\n";
        }
    }
}

---------- Post updated at 19:50 ---------- Previous update was at 19:47 ----------

More details here:

Android: A micro HTTP applet server V0.23 - 2010/05/23

---------- Post updated at 19:53 ---------- Previous update was at 19:50 ----------

On a related note, here is an example bash shell for Android.....

---------- Post updated at 20:10 ---------- Previous update was at 19:53 ----------

"Hello World" in Perl for ASE:


Code:
use Android;
my $a = Android->new();
$a->makeToast("Hello, Android!");

The Following User Says Thank You to Neo For This Useful Post:
al0x (07-15-2011)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Any Android Developers in the Forums? Neo Android 10 02-22-2011 05:16 PM
android bring linux to end user programAngel Android 1 01-02-2011 03:17 AM
Android is Linux (and Java) Neo Android 5 10-20-2010 11:33 AM
Example Linux Commands on Android Neo Android 0 09-27-2010 02:04 PM



All times are GMT -4. The time now is 05:35 PM.