petervojtek / cordova-plugin-shell-exec

Apache Cordova Plugin to Execute Commands in Smartphone's Operating System Shell
Apache License 2.0
34 stars 17 forks source link

Exec shell script? #2

Open maxlinux2000 opened 8 years ago

maxlinux2000 commented 8 years ago

Hi, This plugin sound very interesting. Can it run a shell script inside the app?

Some operations are easy to do with a shell script but complex in JavaScript.

The idea is to write part of app using sh or bash, and the rest in html5.

There is an example ?

Regards MaX

petervojtek commented 8 years ago

hi @maxlinux2000, that is interesting idea, it may be also useful for someone who already has some business logic written in some shell script and this way it can be easily ported to android.

I have not tried that and don't have experience with that. If you will try that, you are welcome to contribute with your experiences :)

maxlinux2000 commented 8 years ago

HI

I'm very interesting in that, because the linux level below the java is very fast and ligther than java and javascript.

Please help me to setup a demo with your plugin in order to show in the screen the result of the command "uptime" for example.

here the lis of avalaible unix commands avalaibles in android trought the android toolbox http://www.all-things-android.com/content/android-toolbox-command-reference

then we can write down a simple script but before we need to develop it directly in the android smarthphone in order to prevent incompatibility, because android use a toolbox (is like busybox) but the commands included they have not all the flags or options of the original ones in gnu linux.

here how to connect to android shell from ubuntu or other linuxbox https://www.youtube.com/watch?v=UI70UYug_zQ

I used sshDroid (with android 5+ you don't need to root your device)

and I can connect to my phone with:

$ ssh -p 2222 root@192.168.1.100

(the defaul passwd is "admin", but you can be changed from the configuration pane)

a simple shell script that I things works in android toolbox, can be

#!/bin/bash
MY_WIFI_IP=$(ifconfig | grep -A1 wlan0 | tail -n1 | cut -d ':' -f2 | cut -d ' ' -f1 |tr -
d '')
echo "<h1>Your Wireless Card IP is:</h1>"
echo "<h2>$MY_WIFI_IP</h2>"

Please help me to setup a demo with cordova... for the moment I do:

$ cordova create cordova-plugin-shell-exec
$ cd cordova-plugin-shell-exec/
$ cordova plugin add https://github.com/petervojtek/cordova-plugin-shell-exec.git
$ cordova plugin add cordova-plugin-console
$ cordova plugin add cordova-plugin-device
$ cordova plugin add cordova-plugin-dialogs
$ cordova plugin add cordova-plugin-inappbrowser
$ cordova plugin add cordova-plugin-splashscreen
$ cordova plugin add cordova-plugin-whitelist
$ cordova platform add android

... but... and now? :)

How to show in the smarthphone display the result of a unix command?

regards MaX

maxlinux2000 commented 8 years ago

Ok, I have a partially working demo with your plugin... jquery is needed!! :-)

below the code of index.html:

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <title>Hello World</title>
        <script type="text/javascript" src="cordova.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>   
    </head>
    <body>

    <script>
    function uptimefuncion() {
        window.ShellExec.exec('uptime', function(res){
          console.log('exit status: ' + res.exitStatus)
          console.log('cmd output: ' + res.output)
//          alert(res.output)
            var div = document.getElementById("COMMAND");
            div.textContent = res.output;
            var text = div.textContent;
        })
    }    
    </script>

    <script>
    function scriptfunction() {
        window.ShellExec.exec('./script.sh', function(res){
          console.log('exit status: ' + res.exitStatus)
          console.log('cmd output: ' + res.output)
//          alert(res.output)
            var div = document.getElementById("COMMAND");
            div.textContent = res.output;
            var text = div.textContent;
        })
    }    
    </script>    

<div id="wrapper">    

   <p><a href="#" class="btn" onclick="uptimefuncion();">UPTIME</a></p>

   <p><a href="#" class="btn" onclick="scriptfunction();">Demo Script</a></p>

</div>  

<div id="COMMAND"></div>

<style>
.btn {
    background: red;
    padding: 10px;
    color: white;
    text-decoration: inherit;
    font-weight: bold;
    font-size: 30px;
    text-align: center;
    width: 250px;
    display: inline-block;
}
div#wrapper {
    text-align: center;
    margin-top: 30px;
} 
div#COMMAND {
    background: black;
    color: #02D002;
    display: flex;
    padding: 15px;
}   
</style>
    </body>
</html>

Here the demo script code in the script.sh file

#!/bin/bash
MY_WIFI_IP=$(ifconfig | grep -A1 wlan0 | tail -n1 | cut -d ':' -f2 | cut -d ' ' -f1 |tr -
d '')
echo "<h1>Your Wireless Card IP is:</h1>"
echo "<h2>$MY_WIFI_IP</h2>"

x shell

but obviously the script.sh is not found from the javascript so is not working this part.

How to do to solve it?

maybe add the app PATH to the plugin, so any files present in the app tree, can be run?

petervojtek commented 8 years ago

maybe you can try to run window.ShellExec.exec('pwd') to see which is the default directory where exec is executed and this will help you to find out position of the script.sh.

Other hack (only for bash) would be to actually keep the bash script code in javascript and pass is to window.ShellExec.exec as a single string.

maxlinux2000 commented 8 years ago

as I know (not much), Javascript doesn't have access to the user's filesystem for security reasons. a fileReader can only read files manually selected by the user. :(

but the BIG problem is that the plugin can't accept the pipe | unix command :o :(

    <script>
    var a = 'ifconfig | grep -A1 wlan0';    

    function testfuncion() {
        window.ShellExec.exec( a , function(res){
          console.log('exit status: ' + res.exitStatus)
          console.log('cmd output: ' + res.output)
//          alert(res.output)
            var div = document.getElementById("COMMAND");
            div.textContent = res.output;
            var text = div.textContent;
        })
    } 
    </script>  

With this simple command ('ifconfig | grep -A1 wlan0'), the answer is allways "Precess han not yet terminated: #####"

A shell script without the pipe command, is useless

:/

gardner commented 8 years ago

Execute sh and pass the commands to it using -c. It should pipe for you.

maxlinux2000 commented 8 years ago

no, sorry, but doesn't works the pipe (is ignored), please try yourself :

sh -c echo Hello | sed "s/Hello/HI/"

it doesn't works using a command with arguments, like 99.9% of unix commands

echo Hello | sed "s/Hello/HI/"

echo display the entire command instead the logic result "HI"

echo Hello | sh -c sed "s/Hello/HI/"

  ...still ignored...

2016-05-31 11:45 GMT+02:00, Gardner Bickford notifications@github.com:

Execute sh and pass the commands to it using -c. It should pipe for you.


You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub: https://github.com/petervojtek/cordova-plugin-shell-exec/issues/2#issuecomment-222641742

ciao, MaX

gardner commented 8 years ago

Try enclosing the commands in single quotes: sh -c 'echo Hello | sed "s/Hello/HI/"'

maxlinux2000 commented 8 years ago

so?

window.ShellExec.exec('sh -c 'echo Hello | sed "s/Hello/HI/"'' , function(res){

no, sorry, but doesn't works at all... :(

I have tried inverting the ' with " too:

window.ShellExec.exec('sh -c "echo Hello | sed 's/Hello/HI/'"' , function(res){

without success. :(

2016-06-01 0:38 GMT+02:00, Gardner Bickford notifications@github.com:

Try enclosing the commands in single quotes: sh -c 'echo Hello | sed "s/Hello/HI/"'


You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub: https://github.com/petervojtek/cordova-plugin-shell-exec/issues/2#issuecomment-222842634

ciao, MaX

gardner commented 8 years ago

You would need to escape redundant quotes like that. "without success" does not sound very intelligent. Try the good ol' "expected behavior vs observed behavior" compare and contrast.

gardner commented 8 years ago

I get "Process has not yet terminated: 2112" when I try this.

gardner commented 8 years ago

I don't have sed on my emulator

$ adb shell
root@generic_x86:/ # sh -c 'echo Hello | sed s/Hello/HI/'
sh: sed: not found
127|root@generic_x86:/ # 
maxlinux2000 commented 7 years ago

Hi, my phone (motog2 with android 6.0) have the sed unix command.... and a lot of others unix commands. I thinks that is a busybox or similar tool, because the options are not the same of a linuxbox, but this cab be turn around.

try to connect you smarthphone via usb to computer