Android Scripting Environment: Shell Scripting and Android


 
Thread Tools Search this Thread
Operating Systems Linux Android Android Scripting Environment: Shell Scripting and Android
# 1  
Old 01-03-2011
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 Smilie 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!");

This User Gave Thanks to Neo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Android Device ID Changer shell script

this is worked "ANDROID NOUGAT" how can i use it for "ANDROID OREO" -plz help me... ------------------------------------------- echo " Ã-~-DEVICE ID CHANGINGÃ-~-" sleep 2 echo " " COUNT=1 while do ; echo "settings put secure android_id " | tr -d '\n' > X1... (4 Replies)
Discussion started by: f4is4l
4 Replies

2. UNIX for Dummies Questions & Answers

Noob scripting question with android ADB commands

Hi I'm pretty new to scripting and I've been googling around looking for an answer but have yet to come up with a proper solution. I work with multiple android devices at a time and I'm looking to simplify my life with a script. Basically I'm looking for a script that takes the device ID's and then... (2 Replies)
Discussion started by: Onyoursix
2 Replies

3. Web Development

Perl scripting or shell scripting?

i am going to study any one of the scripting languages mentioned above(shell 0r perl scripting) . Which is having more scope for a fresher? (1 Reply)
Discussion started by: Anna Hussie
1 Replies

4. What is on Your Mind?

Shell Scripting vs Perl scripting

Gents, I have been working in a Solaris/Unix environment for about 9 months. I took some linux classses online before getting the job. But, I am not very good at scripting. I want to learn how to script. Do you think that I should start with Shell scripting or Perl? I wanted to continue with... (2 Replies)
Discussion started by: Pouchie1
2 Replies

5. What is on Your Mind?

Shell scripting vs Perl scripting

Hi all, I would like to start developping some good scripting skills. Do you think it would be best to start with shell scripting or Perl? I already got a fundation, really basics, in perl. but I am wondering what would be best to be good at first. Can you please help me determine which one to... (14 Replies)
Discussion started by: Pouchie1
14 Replies

6. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

7. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies
Login or Register to Ask a Question