Need help to execute a command in a .bat file

Discussion in 'Android Hacks and Help' started by chrstdvd, Nov 26, 2010.

  1. chrstdvd
    Offline

    chrstdvd New Member

    Joined:
    Sep 30, 2010
    Messages:
    577
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    McKenney Va
    For the fun of it and because studying gets old I decided to do a DOS batch file that would Root and Unroot my phone using the commands that are posted on the forums.

    I have a problem with this command group:
    adb shell # Prompt is $

    cd data/local/tmp

    chmod 0755 rageagainstthecage-arm5.bin

    ./rageagainstthecage-arm5.bin #(then WAIT!!)

    That group of commands works just fine when I root manually. For my script I have to change it to this:

    adb shell "chmod 0755 /data/local/tmp/rageagainstthecage-arm5.bin"
    adb shell "cd /data/local/tmp/;./rageagainstthecage-arm5.bin"

    The rage program runs, but it does not work. I know this because the mount command:
    adb shell "mount -o remount,rw -t ext3 /dev/block/mmcblk1p21 /system"

    receives an "Operation not permitted" output and all subsequent commands fail because the system is not RW.

    It looks like rage runs, because I get the print out lines from it as when doing it manually. It runs pretty quickly compared to when I execute it manually.

    I am thinking that I do not really get into "/data/local/tmp" literally. I think I am just running Rage from the root directory which causes it to fail because it is designed to run within "/data/local/tmp".

    I thought it was working until I decided to manually remove the four files
    to unroot using Root Explorer. It looks like that if you just remove the su, superuser and busybox files as instructed for unroot and leave the rage in "/data/local/tmp", it has already run once and we have not undone what it did the first time it was run. So to get root back all you really need to do is put the three files back into the phone and reboot without running rage.



    Any ideas?
  2. Taegost
    Offline

    Taegost New Member

    Joined:
    May 16, 2010
    Messages:
    688
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    Tampa Bay, Fl
    When you run a batch file in Windows, it doesn't execute the next command in line until it receives notification that the previous line is completed. When it calls adb shell, out won't run the next line until the shell is exited, hence why you have to make each call starting with the adb shell command.

    My Droid Does Tapatalk...
  3. chrstdvd
    Offline

    chrstdvd New Member

    Joined:
    Sep 30, 2010
    Messages:
    577
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    McKenney Va
    Yes and it is executing that way. I have confirmed that I am not literally changing directory with the cd command. I broke the long command into two calls and then between the cd and execute rage I inserted and pwd call and it returns "/" which confirms that I am not in the right directory to run rage. Rage does run but from the wrong place.

    Here is what the code looks like:
    adb push app/Superuser.apk /sdcard/Superuser.apk
    adb push app/su /sdcard/su
    adb push app/busybox /sdcard/busybox
    adb push app/rageagainstthecage-arm5.bin /data/local/tmp/rageagainstthecage-arm5.bin
    adb shell "chmod 0755 /data/local/tmp/rageagainstthecage-arm5.bin"
    adb shell "cd /data/local/tmp/"
    adb shell "pwd" ------> gets "/"
    adb shell "./rageagainstthecage-arm5.bin" ------->gets "file not found"

    whereas the original line gets the rage file to at least run. I put a ;pwd; command between the original commands and get output "/" then rage runs but from the wrong directory.

    I am studying the "chroot" command now and my tests are receiving "Permissin Denied" My linix help files say you have to be a su to use it so that makes sense.

    This statement in the help file gives me hope.

    ---------- Footnotes ----------

    (1) However, some systems (e.g., FreeBSD) can be configured to allow
    certain regular users to use the `chroot' system call, and hence to run
    this program. Also, on Cygwin, anyone can run the `chroot' command,
    because the underlying function is non-privileged due to lack of
    support in MS-Windows.
  4. brando56894
    Offline

    brando56894 New Member

    Joined:
    Mar 9, 2010
    Messages:
    1,217
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    New Jersey
    Just a quick question: did you call su while in adb so you are granted root privileges?

    From my experience batch files are a huge P.I.T.A., I used to do unattended windows installs since i would reinstall windows about every 3-6 months and to save time I would script the installs using a batch file and even after a year of working on it I never got it to work correctly. Shell scripting is 100x more powerful and I believe you can use a unix(like) shell in windows via windows power shell.
  5. chrstdvd
    Offline

    chrstdvd New Member

    Joined:
    Sep 30, 2010
    Messages:
    577
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    McKenney Va
    As far as I know, ya can not execute su if you are not rooted. Yep, batch files are a huge P.I.T.A., but is so much fun when they work. Ha!
    Besides, I like being in a constant state of confusion, it makes the victories so much more powerful and fulfilling.

    I rooted my phone manually and watched what came up on the screen this afternoon. Shortly after I run rage the dollar sigh comes back and you wait. I think as soon as that $ shows up the "Wait for Device" command thinks rage is done and executes the next command.

    Tomorrow, I will see what can be done about that.
    Thanks to all who reply for your interest and help.
  6. kevdog
    Offline

    kevdog New Member

    Joined:
    Nov 14, 2009
    Messages:
    722
    Likes Received:
    0
    Trophy Points:
    0
    Just run a shell script on the phone and call the shell script via the bat file on the computer. If your rooted you obviously have busybox on the phone and this will provide a basic set of executables.
  7. chrstdvd
    Offline

    chrstdvd New Member

    Joined:
    Sep 30, 2010
    Messages:
    577
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    McKenney Va
    I have been trying to do that today. I wrote a small script:
    --------------------------------------------------------------------
    #!/system/bin/sh
    #
    #Scirpt to install Rage to root cell phone
    #
    #
    cd /data/local/tmp
    chmod 0755 rageagainstthecage-arm5.bin
    ./rageagainstthecage-arm5.bin
    ------------------------------------------------------------------------------
    when I call it I get "can't cd to data.local/tmp" and then next two commands fail. (in OP example above)

    I have learned that "./" opens new shell but I do not know if it is on the computer or the phone. (in OP example above)

    Then I learned to use "." or "source" to execute in same shell, but I get "permission denied".

    Then I wrote a script with one line : echo "It Works!" pushed it to /data/local/tmp, called it from the .bat file and IT DID WORK
    with this line:
    adb shell "cd /data/local/tmp/;sh install.sh"

    but "sh rage..." gets "unexpected ..... expected ")"" or something close to that.

    I will figure it out someday. Got a feeling I will be on the Linux forums a lot more often. It seems "cd" in a script has been giving folks a hard time for 30 years. I found one workaround but the link to it is gone off the net. So I started reading about "source" because the original letter from 97 that had the link, hinted the solution somehow deals with "source".
  8. kevdog
    Offline

    kevdog New Member

    Joined:
    Nov 14, 2009
    Messages:
    722
    Likes Received:
    0
    Trophy Points:
    0
    Ok -- you are close but a few things you need to check.

    Make sure the sh executable on the phone is actually located in the /system/bin directory. It most likely is, however just verify.

    This script is going to run on the phone. It needs to be run in a linux/unix environment or in an emulated environment. You need to ensure that the script on the phone is capable of running as an executable file. Hence the script file itself needs to be able to be executed or have rwx permissions (actually on r-x permissions). Also if you are going to run the script as root you might want to put su in the script.

    Lastly lets back up for a minute. The bin file you are trying to copy. Are you trying to copy this from within a directory on the phone, or from a computer (running what OS) to the phone?
  9. chrstdvd
    Offline

    chrstdvd New Member

    Joined:
    Sep 30, 2010
    Messages:
    577
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    McKenney Va
    Yes sh is in system/bin.

    I am confused about the question on the "bin file I am trying to copy".
    It is rageagainstthecage-arm5.bin.

    I have a folder on my Windows 7 desktop "Root" and inside that is a folder called app in which I copied su, superuser.apk, busybox and rage... from my Android/tools folder. In the root of the Root folder, I have adb.exe, AdbWinapi.dll, AdbWinUsbapi.dll. and my Boot.bat file that has the commands to execute similar to a script in cygwin.

    I move the files from the app folder onto the Sdcard using the same commands I would if android/app running the adb commands manually. Works fine. Then I get to the part where manually you would type "adb shell" and then execute commands in the shell to cd data/local/tmp chmod... and ./ rage...

    As far as the script I wrote to cd /data/local/tmp, I wrote it in note pad and saved it to my Root/app folder,n then moved it to /data/local/tmp just like I moved rageagainst the cage there. (Which was dump, I know.) I tried to get into system/bin but that is read only, because I am not rooted.

    The only other folder I could move it to was in /tmp but when I try to run it from there, I get "permission denied".

    So, I have abandoned the script file approach. I have been goggling the cd in a bash shell thing for the past few days.

    Right now I am trying to figure out this statement:
    adb shell "$(/data/local/tmp/ "$(type -p "$0")")"
    adb shell "pwd"
    pause

    I do not understand the type -p and as far as I know $0 is the name of the script that is running and you use it to test "stuff".

    I was reading about the type command when I got ur reply.

    "If the -p option is used, type either returns the name of the disk file that would be executed if name were specified as a command name, or nothing if ``type -t name'' would not return file."
    and the syntax is:
    type [-aftpP] name [name ...]
    I am trying to figure out if I am supposed to replace $0 with rageagainst..

    When I run the above command I get this output:

    /data/local/tmp/: permission denied
    /
    Press any key to continue . . .

    I have a test.bat that I am using to figure this out it looks like this:
    -----------------------------------------------------------------------------
    @echo off
    adb devices


    adb shell "cd /data/local/tmp/;pwd;chmod 777 rageagainstthecage-arm5.bin;ls -l;sh rageagainstthecage-arm5.bin"
    adb shell "pwd"
    pause

    adb shell "cd /data/local/tmp/;pwd;chmod 777 install.sh;ls -l;sh install.sh"
    adb shell "pwd"
    pause

    adb shell "$(/data/local/tmp/ "$(type -p "$0")")"
    adb shell "pwd"
    Pause
    ----------------------------------------------------------------------
    and has the output:
    List of devices attached
    015D941904013015 device

    /data/local/tmp
    -rwxrwxrwx shell shell 5392 2010-08-25 03:42 rageagainstthecage-arm5.bin
    -rwxrwxrwx shell shell 106 2010-11-27 07:04 install.sh
    rageagainstthecage-arm5.bin: 1: Syntax error: word unexpected (expecting ")")
    /
    Press any key to continue . . .
    /data/local/tmp
    -rwxrwxrwx shell shell 5392 2010-08-25 03:42 rageagainstthecage-arm5.bin
    -rwxrwxrwx shell shell 106 2010-11-27 07:04 install.sh
    It Works
    : No such file or directory
    /
    Press any key to continue . . .
    /data/local/tmp/: permission denied
    /
    Press any key to continue . . .

    More than you probably wanted to know, but... I will figure this out, cause it is FUN being brain dead. Ha.
    Last edited: Nov 28, 2010
  10. kevdog
    Offline

    kevdog New Member

    Joined:
    Nov 14, 2009
    Messages:
    722
    Likes Received:
    0
    Trophy Points:
    0
    OK ---

    What is this statement exactly doing?:
    sh rageagainstthecage-arm5.bin

    I take it that you want to execute the file rageagainstthecage-arm5.bin similar to the shell command:
    ./rageagainstthecage-arm5.bin

    Have you tried just running it from within your script like:
    . ./rageagainstthecage-arm5.bin

    or using the nohup command or exec rageagainstthecage-arm5.bin?
    Last edited: Nov 28, 2010
  11. chrstdvd
    Offline

    chrstdvd New Member

    Joined:
    Sep 30, 2010
    Messages:
    577
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    McKenney Va
    Good eyes! I caught that shortly after posting and corrected it in the bat file. 'Course it did not change the result.

    I have been reading the results of a google search " changing directory from inside a script.

    I saw one that said to set a variable cwd=/data/local/tmp and then you cd to $cwd.

    I tried that in the emulator on the phone then I typed in pad and I was there. Then I typed in. /ragebla bla and it worked even to the point of resetting the phone.

    Heck, I thought that was great.

    Then I used z4root to root so I can clean up the stuff I have done to it and start over tommorrow. I have not heard of the nohup command. Will check it out tomorrow.
  12. kevdog
    Offline

    kevdog New Member

    Joined:
    Nov 14, 2009
    Messages:
    722
    Likes Received:
    0
    Trophy Points:
    0
    I have no idea what you're referring to in most of your post however it sounds like some things are working. Great.
  13. chrstdvd
    Offline

    chrstdvd New Member

    Joined:
    Sep 30, 2010
    Messages:
    577
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    McKenney Va
    I finally figured it out!!!

    Got it today:

    adb shell "./data/local/tmp/rageagainstthecage-arm5.bin & sleep 300;kill $!"
    echo Reconnecting in 20 sec...
    adb kill-server

    adb start-server

    echo.
    echo.

    adb wait-for-device
    adb devices

    Now I can root and un root my phone with this. I will attach the entire thing here if anyone wants to play with it at their own risk or just use it to learn about batch files and adb in general.
    Last edited: Nov 29, 2010
Search tags for this page

adb shell commands

,
android run bat file
,
execute bat file android
,

how to open .bat files on android

,

how to run .bat file on android

,

how to run .bat files on android

,
how to run .bat on android
,

how to run a bat file on android

,

how to run bat file in android

,

how to run bat files on android

,

how to run batch file on android

,
how to run batch files on android
,
open bat files on android
,

run .bat on android

,

run batch file on android